2016-12-16 145 views
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(); 

的问题是,这种后置代号不可言,但警报的作品,我只是得到这个从谷歌铬合金控制台:

Error

谢谢您的时间。

+0

出现此错误是因为php文件试图执行一个未定义值的语句。 – ThatHashCodeGuy

+0

解决。 谷歌Chrome浏览器已将其设置为会话结束。 – ThatHashCodeGuy

+0

不要忘记调用event.preventDefault()来停止正常的表单提交。 – Barmar

回答

0

根据你的错误,问题似乎是在服务器端代码。 我建议加入这开启了错误报告在PHP脚本:

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

再进行试验,我建议使用一个应用程序脚本,例如postman铬并尝试发送正确的数据。

然后,您应该看到PHP脚本上的错误以及邮件正文标签中的错误。