Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将POST数据发送到PHP脚本-jQuery、AJAX和;PHP_Javascript_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Javascript 将POST数据发送到PHP脚本-jQuery、AJAX和;PHP

Javascript 将POST数据发送到PHP脚本-jQuery、AJAX和;PHP,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我似乎很难将POST数据发送到我的PHP脚本 我的AJAX将数据(博客文章的ID)发送到我的PHP脚本,然后PHP脚本从数据库中查找包含匹配ID的行 然后,脚本以数组的形式发回博客文章的标题和文章的内容,AJAX将其拾取并插入DOM中的表单中 我可以成功地: 插入示例数据(例如,如果我只是将字符串存储到传递回AJAX的数组中,它将成功地将这些字符串插入表单);及 当指定静态ID时,从数据库中插入正确的数据(例如,如果我将$\u POST['editpostid']切换为非整数5,则查询将成功找

我似乎很难将POST数据发送到我的PHP脚本

我的AJAX将数据(博客文章的ID)发送到我的PHP脚本,然后PHP脚本从数据库中查找包含匹配ID的行

然后,脚本以数组的形式发回博客文章的标题和文章的内容,AJAX将其拾取并插入DOM中的表单中

我可以成功地:

  • 插入示例数据(例如,如果我只是将字符串存储到传递回AJAX的数组中,它将成功地将这些字符串插入表单);及
  • 当指定静态ID时,从数据库中插入正确的数据(例如,如果我将$\u POST['editpostid']切换为非整数5,则查询将成功找到ID=5的行,AJAX将此数据插入表单)
因此,从我的观点来看,问题在于ID永远不会到达PHP脚本,或者我的脚本无法看到JSON对象中的ID

请看一下我的代码,让我知道你的想法。我对这一切都是新手,所以我非常感谢你的反馈——不管它是否解决了问题

Javascript/jQuery:

// When edit button is clicked
$('li.edit').click(function() {

    // Get class (postid inserted with PHP) of edit button, excluding edit class
    var oldpostid = $(this).attr('class').split(' ')[1];

    alert(oldpostid); // Returns the correct postid, for example 5

    var jsonObj = { 'postid': oldpostid };

    alert(jsonObj); // Returns 'object Object'

    // Send postid to PHP script
    $.ajax({
        type: 'POST',
        url: '../scripts/fetchpost.php',
        dataType: 'json',
        data: { 'editpostid': jsonObj },
        success: function() {

            // Fetch post data back from script
            $.getJSON('../scripts/fetchpost.php', function(data) {

                alert(data.title); // Returns null
                alert(data.content); // Returns null

                // All of the below code works if the PHP script returns sample text,
                // or if an ID is specified in the PHP script itself

                var title = data.title;
                var content = data.content;

                // Insert data into editor
                $('#titlehead').text(title);
                $('#edittitle').val(title);
                var editor = 'editpost-content';
                tinymce.get(editor).setContent(content);
            });
        },
        error: function( e ) {
        console.log(e.message);
    }
    });
});
PHP:

PHP脚本保持不变

非常感谢你的帮助!
MrPupper

我认为您错过了索引“postid”,需要替换它

$postid = $_POST['editpostid'];
这一行:

$postid = $_POST['editpostid']['postid'];
或者不发送

data: { 'editpostid': jsonObj },
寄这个

data: { 'editpostid': oldpostid },

我认为您错过了索引“postid”,需要替换它

$postid = $_POST['editpostid'];
这一行:

$postid = $_POST['editpostid']['postid'];
或者不发送

data: { 'editpostid': jsonObj },
寄这个

data: { 'editpostid': oldpostid },

查看您的代码,您似乎得到了null,因为您两次请求
fetchpost.php
脚本。当您通过
$.ajax(…)与脚本联系时$.getJSON(…)。当您通过
$.getJSON(…)联系时,但是,您没有发布数据,而且您的脚本似乎没有正确定义的方法来处理
GET
请求,因此脚本不知道如何反应,并且返回空信息

我将JavaScript/jQuery更改为以下内容:

// When edit button is clicked
$('li.edit').click(function() {

    // Get class (postid inserted with PHP) of edit button, excluding edit class
    var oldpostid = $(this).attr('class').split(' ')[1];

    alert(oldpostid); // Returns the correct postid, for example 5

    var jsonObj = { 'postid': oldpostid };

    alert(jsonObj); // Returns 'object Object'

    // Send postid to PHP script
    $.ajax({
        type: 'POST',
        url: '../scripts/fetchpost.php',
        dataType: 'json',
        data: {'editpostid': jsonObj },
        success: function(sData) {
            var data = JSON.parse(sData);
            alert(data.title); // Returns null
            alert(data.content); // Returns null

            // All of the below code works if the PHP script returns sample text,
            // or if an ID is specified in the PHP script itself

            var title = data.title;
            var content = data.content;

            // Insert data into editor
            $('#titlehead').text(title);
            $('#edittitle').val(title);
            var editor = 'editpost-content';
            tinymce.get(editor).setContent(content);
        },
        error: function( e ) {
            console.log(e.message);
        }
    });
});
此外,PHP希望
应用程序/x-www-form-urlencoded
值能够与
$\u POST[…]进行交互。因此,如果您想将其输入JSON,那么在PHP中,您需要实现一个解决方案,例如:
$postedData=JSON\u decode(file\u get\u contents('php://input'));(请参阅中的更多信息;有关
json\u解码的更多信息,请参阅)



注意:虽然超出了您的问题范围,您可能已经知道这一点,但我发现有必要指出,您的MySQL不安全,容易受到SQL注入的攻击,因为它只是盲目地相信
postId
没有被篡改。您需要通过说
$postid=$dbconnect->real\u escape\u string($postid)来清理它
在初始化
$dbconnect
并连接到数据库之后,但在将
$postid
放入SQL查询字符串之前。

查看您的代码时,由于您两次请求
fetchpost.php
脚本,因此您的代码似乎为空。当您通过
$.ajax(…)与脚本联系时$.getJSON(…)。当您通过
$.getJSON(…)联系时,但是,您没有发布数据,而且您的脚本似乎没有正确定义的方法来处理
GET
请求,因此脚本不知道如何反应,并且返回空信息

我将JavaScript/jQuery更改为以下内容:

// When edit button is clicked
$('li.edit').click(function() {

    // Get class (postid inserted with PHP) of edit button, excluding edit class
    var oldpostid = $(this).attr('class').split(' ')[1];

    alert(oldpostid); // Returns the correct postid, for example 5

    var jsonObj = { 'postid': oldpostid };

    alert(jsonObj); // Returns 'object Object'

    // Send postid to PHP script
    $.ajax({
        type: 'POST',
        url: '../scripts/fetchpost.php',
        dataType: 'json',
        data: {'editpostid': jsonObj },
        success: function(sData) {
            var data = JSON.parse(sData);
            alert(data.title); // Returns null
            alert(data.content); // Returns null

            // All of the below code works if the PHP script returns sample text,
            // or if an ID is specified in the PHP script itself

            var title = data.title;
            var content = data.content;

            // Insert data into editor
            $('#titlehead').text(title);
            $('#edittitle').val(title);
            var editor = 'editpost-content';
            tinymce.get(editor).setContent(content);
        },
        error: function( e ) {
            console.log(e.message);
        }
    });
});
此外,PHP希望
应用程序/x-www-form-urlencoded
值能够与
$\u POST[…]进行交互。因此,如果您想将其输入JSON,那么在PHP中,您需要实现一个解决方案,例如:
$postedData=JSON\u decode(file\u get\u contents('php://input'));(请参阅中的更多信息;有关
json\u解码的更多信息,请参阅)



注意:虽然超出了您的问题范围,您可能已经知道这一点,但我发现有必要指出,您的MySQL不安全,容易受到SQL注入的攻击,因为它只是盲目地相信
postId
没有被篡改。您需要通过说
$postid=$dbconnect->real\u escape\u string($postid)来清理它
在初始化
$dbconnect
并连接到数据库之后,但在将
$postid
放入SQL查询字符串之前。

感谢您的回复Farzad。我的代码现在正在运行,这是由于斯宾塞和你自己帮助我发现了一些错误。我发现你的第二个建议对我很有效,于是我彻底放弃了这个阵列。谢谢你的帮助!:)@帕珀先生非常乐意帮忙:)谢谢你的回复法扎德。我的代码现在正在运行,这是由于斯宾塞和你自己帮助我发现了一些错误。我发现你的第二个建议对我很有效,于是我彻底放弃了这个阵列。谢谢你的帮助!:)@非常感谢斯宾塞,我的代码现在可以工作了!代码本身在一些地方被破坏,Farzad和你自己都帮助修复了这些地方。我还发现,删除包含JSON.parse()的行完全可以让代码正常工作。关于您关于SQL安全性的说明,我完全了解,并感谢您的提醒。此代码用于大学作业项目,不会作为生产代码发布,因此我们不希望涵盖这些安全方面。不过,我注意到了您对未来项目的评论,我感谢您给我的反馈!:)@Pupper先生,啊,我不确定是否需要
JSON.parse
,或者jQuery库是否会自动运行