0

I have a form post.php where the user inputs data and receivepost.php where the data entered is transferred through Ajax post. I want to achieve the following in the receivepost.php:

if (button save is pressed) {
    save to database
} else if (button retrieve is pressed) {
    retrieve from database
}

I tried using isset($_POST['submit']) on the reveivepost.php but it does not detect the button which is pressed. The Ajax post is working and all the data is available on the receivepost page. I have another solution that is to create 2 different PHP files and run them according to the button pressed but I think there is a better solution to it.

Here is my ajax call :

$("document").ready(function () {

  $("#submit").click(function () {

        var xmlhttp;

        // test browsers
        if(window.XMLHttpRequest) {
          
          xmlhttp = new XMLHttpRequest();
          
        }else {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        // get the values of textboxes
        var desc_text = $("#desc").val();
        var speed_text = $("#speed").val();
        var tarea_text = $("#tarea").val();

        // variable to hold value of textboxes
        var content = "desc=" + desc_text + "&speed=" + speed_text + "&tarea=" + tarea_text;

        // open the request
        xmlhttp.open("POST", "receivepost.php", true);

        // set header
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        // check xmlhttp state and status and display text in div
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            
           document.getElementById('para').innerHTML = xmlhttp.responseText;
          }

        }
        // send the content of the textboxes
        xmlhttp.send(content);

       
      });
   });

Here is my post.php form:

<form id="myform">
    
    <label for="description">Description:</label>
    <input type="text" id="desc" name="desc" /><br/>


    <label for="speed">Speed: </label>
    <input type="text" id="speed" name="speed" /><br/>

    <textarea id="tarea" cols="10" rows="10" name="tarea"></textarea><br/>
    
    <input type="button" id="submit" name = "submit" value="Save">
    <input type="button" id="retrieve" name="retrieve"  value="Retrieve">

</form>
<div id="para"></div>
2
  • Why not trying instead of if(isset($_POST['submit'])){code here} something different, like if(!empty($_POST['submit'])){code here} .. it checks if there is data in it, instead of checking if it's set. This way you can avoid the set, and just see if the button you pressed has data send. I think thats a tat more reliable. Commented Oct 16, 2012 at 10:16
  • I tried if(!empty($_POST['submit'])) { echo 'it is not empty'; } and posting it back on the div it does not work Commented Oct 16, 2012 at 10:21

2 Answers 2

1

Try to add to the content an extra parameter called button-pressed which you can later use in receivepost.php as below:

if($_POST['button-pressed']=="save"){  
    //savetodb code  
}
else if($_POST['button-pressed']=="retrieve"){  
    //retreivefromdb code  
}

Perhaps you have two JavaScript functions, one on each button:

$("#submit").click(function () {
    var content = "button-pressed=save" + "&desc=" + desc_text + "&speed=" + speed_text + "&tarea=" + tarea_text;

$("#retrieve").click(function () {
    var content = "button-pressed=retrieve" + "&desc=" + desc_text + "&speed=" + speed_text + "&tarea=" + tarea_text;

Hope this helps?

Sign up to request clarification or add additional context in comments.

1 Comment

Seems to be working RoManiac i just tested for the submit button only but should be fine with the retrieve as well.gdoron was right but your explanation is clearer. thank you :)
0
<input type="button" id="submit" name ="submit" value="save" onClick="fnc(this.id)">
<input type="button" id="retrieve" name="retrieve"  value="retrieve" onClick="fnc(this.id)">
<script type="text/javascript">
    function fnc(myid) {
    
    var val=document.getElementById(myid).value;
    alert(val);
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.