0

我有一个表单post.php,用户在其中输入数据,输入receivepost.php的数据通过 Ajax 发布传输。我想在以下方面实现receivepost.php

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

我尝试在上使用isset($_POST['submit'])reveivepost.php但它没有检测到按下的按钮。Ajax 帖子正在运行,所有数据都可以在receivepost page. 我有另一个解决方案是创建 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>
4

2 回答 2

1

尝试在内容中添加一个名为 button-pressed 的额外参数,您稍后可以在 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;

希望这可以帮助?

于 2012-10-16T10:17:21.480 回答
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>
于 2012-10-16T10:30:00.273 回答