无法接收通过post方法发送的值

时间:2013-07-15 13:51:41

标签: php javascript ajax

我正在尝试发送ID并从ajax_form.php发送到ajax_test.php()

我的ajax_form.php是:

<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
    <meta content="utf-8" http-equiv="encoding" />
    <script type="text/javascript">
    function showUser(form, e) {
        e.preventDefault();
        e.returnValue=false;
        var xmlhttp;
        var submit = form.getElementsByClassName('submit')[0];
        //var sent = document.getElementsByName('sent')[0].value || '';
        //var id = document.getElementsByName('id')[0].value || '';
        var sent = form.elements['sent'].value;
    var id = form.elements['id'].value;

        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function(e) {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open(form.method, form.action, true);
        xmlhttp.send('sent=' + sent + '&id=' + id + '&' + submit.name + '=' + submit.value);
    }
    </script>
</head>
<body>
    <form action="ajax_test.php" method="POST" onsubmit="showUser(this, event)">
        <label>Enter the sentence: <input type="text" name="sent"></label><br />
        <input type="submit" class="submit" name="insert" value="submit"  />
        <input type="" name="id" style="display: none"/>
    </form>

    <h4>UPDATE</h4>
    <form action="ajax_test.php" method="POST" onsubmit="showUser(this, event)">
        <pre>
            <label>Enter the ID:</label><input type="text" name="id"><br>
                <label>Enter the sentence:<input type="text" name="sent"></label><br />
        </pre>
        <input type="submit" class="submit" value="submit" name="update"/>
    </form>

    <br />
    <div id="txtHint">
        <b>Person info will be listed here.</b>
    </div>
</body>
</html>

和ajax_test.php是:

    <html><head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    </head> <body > 
    <?php
    $s = $_POST['sent'];
    echo "Entered sentence : $s";

    if (isset($_POST['insert']) && $_POST['insert'] !== '') {
    echo "Operation: Insert","<br>";
    $s = $_POST['sent'];
    $flag = 0;
    echo "Entered sentence : $s";
    //database stuff

    mysqli_close($con);
    }

    // -------------------------------UPDATE --------------------------
     if (isset($_POST['update']) && $_POST['update'] !== '') {
    echo "Operation: update", "<br>";
     // you say update but you are actually inserting below

    $s    = $_POST['sent'];
    $flag = 1;

    echo "Entered sentence : $s";
//database stuff

         mysqli_close($con);
    }
    ?></html > </body >

if()以外的内容都没有正确执行,if也没有 我收到错误:

Notice: Undefined index: sent in /opt/lampp/htdocs/test/ajax_test.php on line 6 

只需Entered sentence :即可打印。

帖子中的问题在哪里?理想情况下,我应该可以抓取idsent

1 个答案:

答案 0 :(得分:1)

您需要在请求中设置Content-Type,然后再将其发送给application/x-www-form-urlencoded。请参阅documentation