0
我有点失误。PHP JQUERY AJAX Post
我想使用AJAX从表单发布数据,但数据根本不发布。
注:我有3个文件的[Home.php,Post_Idea.php,Insert_Post.php]
Home.php载荷post_idea.php通过Ajax。 我保持这个AJAX代码Home.php:
$("form").submit(function() {
var title = $('#title').val();
var body = $('#body').val();
alert(title + " " + " " + body);
$.ajax({
url: "insert_post.php",
type: "POST",
data: {title: title, body: body},
success: function (result) {
console.log(result);
}
});
});
Insert_Post.php:
<?php
session_start();
require_once("include/database.php");
$user_id = $_SESSION['user_id'];
$title = $_POST['title'];
$body = $_POST['body'];
$img = "cancer_resarch_.jpg";
$votes = 0;
$stm2 = $db->prepare("INSERT INTO posts (user_id, post_title, post_img, post_body, post_votes) VALUES (:user_id ,:title, :body, :img, :votes)");
$stm2->bindParam(':user_id', $user_id);
$stm2->bindParam(':title', $title);
$stm2->bindParam(':body', $body);
$stm2->bindParam(':img', $img);
$stm2->bindParam(':votes', $votes);
$stm2->execute();
的问题是,这种后置代号不可言,但警报的作品,我只是得到这个从谷歌铬合金控制台:
谢谢您的时间。
出现此错误是因为php文件试图执行一个未定义值的语句。 – ThatHashCodeGuy
解决。 谷歌Chrome浏览器已将其设置为会话结束。 – ThatHashCodeGuy
不要忘记调用event.preventDefault()来停止正常的表单提交。 – Barmar