Ajax,PHP中的未定义索引$ _POST错误 [英] Undefined Index $_POST Error in Ajax, PHP
本文介绍了Ajax,PHP中的未定义索引$ _POST错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Ajax中获取未定义的索引postBody错误
我不知道该如何解决.
我已经检查了很多帖子,但这没有帮助.
index.php中的HTML代码:
Getting Undefined Index postBody Error in Ajax
I don't know how to fix this.
I have already checked many posts but that didn't help.
HTML CODE in index.php:
<form method="post">
<textarea name="postBody" type="text" id="postBody" placeholder="What's on your mind?"></textarea>
<input type="submit" name="postButton" id="postButton" value="POST" class="cg nq w-50">
</form>
在jquery cdn之后,我的index.php中是Ajax代码.
Here is Ajax Code in my index.php at the end after jquery cdn.
$("#postButton").click(function(e) {
e.preventDefault();
var body = $("#postBody").val();
var dataString = 'postBody='+body;
$.ajax({
type:'POST',
data:dataString,
url:'submitPost.php',
success:function(data) {
alert(data);
}
});
});
这是SubmitPost.php代码.
And this is submitPost.php Code.
include("config.php");
global $con;
$body = $_POST['postBody'];
$date_added = date("F j, Y, g:i a");
$query = mysqli_query($con,"INSERT INTO posts VALUES('','$body','$date_added')");
if($query == 1){
echo "Post Submitted";
else{
echo "Error";
}
此代码实际上返回警报中的已提交帖子",并插入$ date_add,而不是$ body. 错误:3上的postSubmit.php中的未定义索引postBody.
This code actually Return Post Submitted in alert and Insert the $date_added and but not $body. Error: undefined Index postBody in postSubmit.php on 3.
推荐答案
您犯的一些错误是:
- 拼写错误的
form
标记 -
data
错误地通过ajax
- mis-spelled
form
tag - wrong passing of
data
throughajax
$("#postButton").click(function(e) {
e.preventDefault();
var body = $("#postBody").val();
$.ajax({
type: 'POST',
data: {'postBody': body},
url: 'submitPost.php',
success: function(data) {
alert(data);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
<textarea name="postBody" type="text" id="postBody" placeholder="What's on your mind?"></textarea>
<input type="submit" name="postButton" id="postButton" value="POST" class="cg nq w-50">
</form>
这篇关于Ajax,PHP中的未定义索引$ _POST错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文