简体   繁体   English

PHP JQUERY AJAX发布

[英]PHP JQUERY AJAX Post

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. 我正在尝试使用AJAX从表单发布数据,但是数据根本不发布。

NOTE: I have 3 files [Home.php, Post_Idea.php, Insert_Post.php] 注意:我有3个文件[Home.php,Post_Idea.php,Insert_Post.php]

Home.php loads in post_idea.php VIA AJAX. Home.php通过AJAX加载到post_idea.php中。 I keep this AJAX code in Home.php: 我将这个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: 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: 问题是,此代码根本不会发布,但是警报仍然有效,我只是从Google Chromes Console上获得了此消息:

Error 错误

Thank you for your time. 感谢您的时间。

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: 我建议通过添加以下代码在您的php脚本中打开错误报告:

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. 然后要测试脚本,我建议使用Chrome等邮递员之类的应用,并尝试发送正确的数据。

Then you should see the error on the PHP script and on which line the error is in the body tab of postman. 然后,您应该在PHP脚本上看到错误,并且该错误在邮递员的正文选项卡的哪一行上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM