Php 无法接收通过post方法发送的值
我正在尝试发送ID,并从ajax_form.php发送到ajax_test.php() 我的ajax_form.php是:Php 无法接收通过post方法发送的值,php,javascript,ajax,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/
<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>
刚刚输入了一句话:打印出来
问题在哪里?理想情况下,我应该能够获取
id
和发送的 在将请求发送到应用程序/x-www-form-urlencoded
之前,您需要对请求设置内容类型。请参阅。谢谢,伙计,我看了文档<代码>$headers[]='Accept:application/ajax'$标题[]='内容类型:应用程序/ajax';curl_setopt($ch,CURLOPT_HTTPHEADER,$headers)代码>?但这是问题背后的问题吗?在xmlhttp.open(form.method,form.action,true)行之前
,尝试添加xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')代码>非常感谢,在过去的两天里,我对这个问题做了大量的修改!但愿我能投10多张赞成票!
<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 >
Notice: Undefined index: sent in /opt/lampp/htdocs/test/ajax_test.php on line 6