无法接收通过post方法发送的值
Unable to receive values sent through post method
我正在尝试发送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 :
。
帖子中的问题在哪里?理想情况下,我应该能够获取id
和sent
!
在将请求发送到application/x-www-form-urlencoded
之前,需要在请求上设置Content-Type
。请参阅文档。
相关文章:
- 如何使用post方法从方法上传文件
- AJAX不会在文件上传后重定向到网页-POST方法
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- 使用Ajax的问题's发送多个值的Post方法
- 使用javascript ajax post方法的未定义偏移PHP错误
- 如何使用get或post方法连接和执行ajax
- jQuery/Ajax-试图通过Ajax创建POST方法并获得对HTML的响应
- 如何在不更改URL的情况下使用HTTP方法POST
- HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC)
- 对象 #<传入消息> 没有方法“post”
- Jquery Ajax 方法 POST 在远程服务器上不起作用
- 方法POST,状态(已取消)错误消息
- 方法POST在向REST服务发送请求时不起作用
- 如何修复这个js脚本发送一个值的方法post
- 表单方法post和jquery
- 当我想用方法post(javascript)重定向到动作时,控制器方法不重定向并返回到上一页,为什么
- 如何获取POST方法“$.POST()”的结果
- 如何通过Jquery中的方法post传递数组和参数
- 覆盖jQuery方法$.POST
- Extjs4:使用方法 POST 的树存储