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.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来