weixin_33674437 2016-12-16 01:55 采纳率: 0%
浏览 10

PHP JQUERY AJAX发布

I'm in a bit of a blunder.

I am trying to POST data from a form using AJAX, but the data doesn't post at all.

NOTE: I have 3 files [Home.php, Post_Idea.php, Insert_Post.php]

Home.php loads in post_idea.php VIA AJAX. I keep this AJAX code in 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();

The problem is, this code does NOT post at all, yet the alert works and I just get this from Google Chromes Console:

Error

Thank you for your time.

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2016-12-16 02:15
    关注

    According to your error, the problem seems to be in the server side code. I would recommend turning on the error reporting in your php script by adding this:

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

    then to test the script I recommend using an app such as postman for chrome and try to send the correct data.

    Then you should see the error on the PHP script and on which line the error is in the body tab of postman.

    评论

报告相同问题?