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

标签 javascript php jquery mysql ajax

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

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

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

我可以成功:

  • 插入示例数据(例如,如果我只是将字符串存储到要传回 AJAX 的数组中,它将成功地将这些字符串插入到表单中);和
  • 指定静态 ID 时从数据库插入正确的数据(例如,如果我切换 $_POST['editpostid'] 并指定整数 5,查询会成功找到 ID = 5 和 AJAX 的行将此数据插入到表单中)。

因此,从我的 Angular 来看,问题在于 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

// Specifies connection details
include('../scripts/config.php');

// Fetch data from AJAX
$postid = $_POST['editpostid']; // Where I think the problem lies. Returns null.
// Again, this works if I switch out $_POST with an integer, such as 5

// Find rows in database that match postid
$postedit_qry = mysqli_query( $dbconnect, "SELECT * FROM posts WHERE postid='$postid'" );

// Store results in an associative array
$row = mysqli_fetch_assoc( $postedit_qry );

// Split array into variables
$title = $row['title'];
$content = $row['content'];

// Organise data into an array for json
$postedit = array(
    'title' => $title,
    'content' => $content
);

// Return array as json object for ajax to pick up
echo json_encode( $postedit );

// Close connection
mysqli_close( $dbconnect );

?>
<小时/>

更新 - 解决方案:

修复了 jQuery/Javascript:

// Snip

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

    // Send postid to PHP script
    $.ajax({
        type: 'POST',
        url: '../scripts/fetchpost.php',
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded',
        data: { "editpostid": oldpostid },
        success: function(data) {

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

// Snip

PHP 脚本保持不变。

非常感谢您的帮助! 帕珀先生

最佳答案

我认为您错过了索引'postid'并且需要替换它

$postid = $_POST['editpostid'];

用这一行:

$postid = $_POST['editpostid']['postid'];

或者代替发送

data: { 'editpostid': jsonObj },

发送此内容

data: { 'editpostid': oldpostid },

关于javascript - 将 POST 数据发送到 PHP 脚本 - jQuery、AJAX 和 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030313/

相关文章:

php - 从数据库中的两个输入框中搜索数据在PHP

javascript - 如何在页面上查找字符串然后使用 jQuery 将样式添加到特定的 div?

php - 警告 : mt_rand(): max(-1) is smaller than min(1)

javascript - 使用 "if $(element).is(...)"检查数组元素

php - 更新图像数组

javascript - 调用时函数未运行

javascript - Facebook用户发送邀请请求后调用ajax调用

javascript - 选择 XML AJAX 文档作为变量

javascript - 是/否按钮选择

javascript - HTML/JS 隐藏基于另一个选定值的选择选项