2013-07-15 92 views
0

我試圖發送ID和ajax_form.php發送到ajax_test.php值(無法接收)通過POST方法發送

我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

您需要在請求上設置Content-Type,然後將其發送到application/x-www-form-urlencoded。見documentation

+0

謝謝哥們,我看了文檔。 '$ headers [] ='Accept:application/ajax'; $ headers [] ='Content-Type:application/ajax'; curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);'?但這是背後問題的問題嗎? – user123

+0

_Before_'xmlhttp.open(form.method,form.action,true);',嘗試添加'xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');' – marekful

+0

非常感謝,我爲這個問題在過去的兩天裏做了大量的修改!希望我可以提供10多張選票! – user123