Ajax,PHP中的未定义索引$

编程入门 行业动态 更新时间:2024-10-27 11:28:02
本文介绍了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 through ajax

$("#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="ajax.googleapis/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中的未定义索引$

本文发布于:2023-10-11 17:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   未定义   Ajax   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!