成功删除后显示消息

时间:2018-08-13 13:49:11

标签: javascript php jquery

我想在成功删除记录后显示一条消息。我尝试了一切,但没有成功。

我有一个外部js文件,我从该文件中调用一个PHP文件,该文件删除了记录。

JS文件:-

        // Delete records
    function DeleteGoal(ids) {
        $.post("ajax/deletegoal.php", {
                userid: ids.user_id,
                goalid: ids.goal_id
            },
            function (data, status) {
                // reload goals 
                ShowGoals();
            }
        );
        var jsuccess = $("#myPhpValue").val();
        console.log(jsuccess);
}

PHP文件:-

<?php
if(isset($_POST['userid']) && isset($_POST['goalid']))
    {
        // include Database connection file 
        require('../../login_system/db.php');

        $userid = $_POST['userid'];
        $goal_id = $_POST['goalid'];

       // $query = "DELETE from goals where user_id = '$userid' AND goal_id='$goal_id'";

      //  if (!$result = $mysqli->query($query)) {
           // exit(mysqli_error($mysqli));
   //     }

        $success = "true";
        echo '<form><input type="hidden" id="myPhpValue" value="'.$success.'"/></form>';

        mysqli_close($mysqli);
    }  
?>

我尝试通过几种方法将$ success变量发送回js文件,但没有一种方法。似乎任何输出命令(echo,alert,console.log)都不显示任何内容,我也不知道为什么。

我将不胜感激!

2 个答案:

答案 0 :(得分:1)

您可以简单地使用echo返回并直接显示响应:

a.php:

<?php
    if(isset($_POST['userid']) && isset($_POST['goalid']))
    {
        ...
        echo true;
    }  
?>

A.js:

function DeleteGoal(ids) {
    $.post("ajax/deletegoal.php", {
            userid: ids.user_id,
            goalid: ids.goal_id
        },
        function (response) {
            if( response ){
                // reload goals 
                ShowGoals();
                console.log("Success message");
            }
        }
    );
}

您可以返回json格式,这使您能够返回多个信息,例如:

A.js:

function DeleteGoal(ids) {
    $.post("ajax/deletegoal.php", {
            userid: ids.user_id,
            goalid: ids.goal_id
        },
        function (data) {
            var response = $.parseJSON(data);

            if( response.success ){
               // reload goals 
                ShowGoals();

                console.log(response.message);
            }
        }
    );
}

a.php:

<?php
    if(isset($_POST['userid']) && isset($_POST['goalid']))
    {
        echo jdon_encode(['success'=>true,"message"=>"Deleted Successfully"]);
    }  
?>

答案 1 :(得分:0)

最后我做到了!

PHP文件

<?php
 if(isset($_POST['userid']) && isset($_POST['goalid']))
     {
        // include Database connection file 
        require('../../login_system/db.php');

        $userid = $_POST['userid'];
        $goalid = $_POST['goalid'];

        $query = 'DELETE from goals where userid ='.$userid.' AND goal_id='.$goalid;

        if (!$result = $mysqli->query($query)) {
           exit(mysqli_error($mysqli));
        }      

        $data = true;
        echo($data);

        mysqli_close($mysqli);
    }  
?>

JS文件

// Delete records
function DeleteGoal(ids) {
    $.post("ajax/deletegoal.php", {
            userid: ids.user_id,
            goalid: ids.goal_id
        },
        function (data, status) {
            // reload goals 
            if (data==true && status =="success")
                ShowGoals();
            else
                alert("Problem");
        }
    );
} 

所以,我返回使用 data 变量发生的事情。