AJAX没有向PHP提供数据

时间:2014-09-13 14:59:15

标签: php ajax

我很遗憾在这里发帖,但我尝试过很多东西,但没有解决我当前的问题。 问题是我有 AJAX部分 deletion_id ,通过AJAX,deletion_id需要到单独的PHP脚本。然后它会被准备好的声明删除。

但我没有得到任何回报,没有通过控制台错误 error_reporting

这是AJAX部分:

$('.btn.btn-xs.btn-danger').click(function(){
    var delete_id = $(this).attr('id');
    var delete_id_url = 'action=deleting&id='+delete_id;

        $.ajax({
            type: "POST",
            url:  "/panel/includes/handlers/404.handler.php",
            data: delete_id_url,
            succes: function responseText()
                    {
                      $.parseJSON(responseText);

                        if(responsetext.indexOf(1) > -1)
                        {
                            alert('Er is helaas geen overeenkomst in onze database gekomen.');
                        }
                        else if(responsetext.indexOf(2) > -1)
                        {
                            alert('Het opgegeven ID nummer is helaas geen geldig getal.');
                        }
                        else if(responsetext.IndexOf(100) > -1)
                        {
                            alert('De opgegeven rij is verwijderd.');
                            $(delete_id).hide();
                        }
                        else
                        {
                            alert('or nah');
                        }
                    }
        });
});

404.handler.php部分:

if(isset($_POST['action']) && isset($_POST['action']) == 'deleting')
    {
        $intDelete = trim($_POST['id']);
        $errors    = array();

            $stmt = $mysqli->prepare("SELECT id FROM error WHERE id = ?");
            $stmt->bind_param('i', $intDelete);
            $stmt->execute();
            $intSelect = $stmt->num_rows();
            $stmt->close();

                // Controle part.
                if($intSelect == 0)
                {
                    $errors[] = 1;
                    $bolean   = true;
                }
                if(!ctype_digit($intDelete))
                {
                    $errors[] = 2;
                    $bolean   = true;
                }

                    if($bolean == false)
                    {
                        $errors[] = 100;

                            $stmt = $mysqli->prepare("DELETE FROM error WHERE id = ?");
                            $stmt->bind_param('i', $intDelete);
                            $stmt->execute();
                            $stmt->close();
                    }

                header('Content-Type: application/json');
                echo json_encode($errors);
                console.log($errors);
    }

我很抱歉,如果我不清楚或什么,英语不是我的母语,所以我遇到了麻烦。感谢您提前帮助!

3 个答案:

答案 0 :(得分:2)

if(isset($_POST['action']) && isset($_POST['action']) == 'deleting')

isset返回一个布尔值,所以这永远不会通过。 你需要:

if(isset($_POST['action']) && $_POST['action'] == 'deleting')

修改

另外,正如评论和其他答案所述,console.log是javascript,而不是php,所以删除它。最后,虽然与您的问题无关,为什么要将ajax发布到名为404.handler.php的文件中?该名称表明该文件具有不同的用途。

答案 1 :(得分:1)

使用此

success: function responseText(response)
                {
                  $.parseJSON(response);

                    if(response.indexOf(1) > -1)
                    {
                        alert('Er is helaas geen overeenkomst in onze database gekomen.');
                    }
                    else if(response.indexOf(2) > -1)
                    {
                        alert('Het opgegeven ID nummer is helaas geen geldig getal.');
                    }
                    else if(response.IndexOf(100) > -1)
                    {
                        alert('De opgegeven rij is verwijderd.');
                        $(delete_id).hide();
                    }
                    else
                    {
                        alert('or nah');
                    }
                }
    });

同样删除PHP文件

更改第一行
if(isset($_POST['action']) && isset($_POST['action']) == 'deleting')

if(isset($_POST['action']) && $_POST['action'] == 'deleting')

并删除此行

console.log($errors);

答案 2 :(得分:1)

你知道你拼错了单词" succes"在你的AJAX success函数中?

您也没有向您的成功函数发送teh responseText变量:

success: function(responseText){

在解决此类问题时,我通常会在代码中插入一些反馈测试。

第一项检查确保AJAX代码已到达PHP处理程序。如果我看到到这里01 ,那么我知道我至少到目前为止。

接下来,我检查代码可能失败的每个点 - 在if语句中,在数据库查找之后等等。这有助于我精确缩小代码的哪个部分失败。我从来没有立即发现问题,通常需要再添加一些测试,然后再添加一些,然后 " A ha!缺少分号!"

例如:

echo 'Got to here 01 / ';
if(isset($_POST['action']) && isset($_POST['action']) == 'deleting'){
echo 'Got to here 02 / ';
    $intDelete = trim($_POST['id']);
    $errors    = array();

        $stmt = $mysqli->prepare("SELECT id FROM error WHERE id = ?");
        $stmt->bind_param('i', $intDelete);
        $stmt->execute();
        $intSelect = $stmt->num_rows();
        $stmt->close();
echo 'Got to here 03 / ';

            // Controle part.
            if($intSelect == 0)
            {
echo 'Got to here 04 / ';
                $errors[] = 1;
                $bolean   = true;
            }
            if(!ctype_digit($intDelete))
            {
echo 'Got to here 05 / ';
                $errors[] = 2;
                $bolean   = true;
            }

                if($bolean == false)
                {
echo 'Got to here 06 / ';
                    $errors[] = 100;

                        $stmt = $mysqli->prepare("DELETE FROM error WHERE id = ?");
                        $stmt->bind_param('i', $intDelete);
                        $stmt->execute();
                        $stmt->close();
                }

echo 'Got to here 07 / ';
            header('Content-Type: application/json');
            echo json_encode($errors);
            console.log($errors);
}

<强>的javascript / jQuery的:

   $.ajax({
        type: "POST",
        url:  "/panel/includes/handlers/404.handler.php",
        data: delete_id_url,
        success: function(responseText){
           alert( responseText ); 

           //the rest of your code follows
        }

现在,您可以在不猜测的情况下查看从服务器返回的内容。