如何使用ajax调用检查在php文件中单击了哪个按钮

时间:2012-10-16 10:01:07

标签: php javascript ajax

我有一个表单post.php,其中用户输入数据和receivepost.php,其中输入的数据通过ajax post传输。我想在receivepost.php中实现以下功能:

        if(button save is pressed) {
            save to database

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

我尝试在reveivepost.php上使用isset($ _ POST ['submit']),但它没有检测到按下的按钮。 ajax帖子正在运行,所有数据都在receivepost页面上提供。我有另一种解决方案,即创建2个不同的php文件并根据按下的按钮运行它们,但我认为有更好的解决方案。

这是我的ajax电话:

$("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);


      });
   });

这是我的post.php表单:

<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 个答案:

答案 0 :(得分:1)

尝试在内容中添加一个名为按下按钮的额外参数,稍后您可以在receivepost.php中使用该参数,如下所示:

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

也许你有两个JavaScript函数,每个按钮一个:

$("#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;

希望这有帮助吗?

答案 1 :(得分: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>