通过jquery检查PHP脚本返回的数据

checking returned data from PHP script through jquery

本文关键字:返回 数据 脚本 PHP jquery 检查 通过      更新时间:2023-09-26

我写了一个php脚本

cch.php

$stmtcheck = $mysqli->prepare("SELECT id FROM members WHERE email=? AND unlock_code=?");
$stmtcheck->bind_param("si", $_SESSION['unlockemail'], $_POST['code']);
$stmtcheck->execute();
$stmtcheck->bind_result($id);
$stmtcheck->fetch();
$stmtcheck->close();

提交表单的jquery是

recover.php

$("#formUnlock").submit(function(e)
{
   e.preventDefault();
   $.ajax(
   {
        url: '../scripts/cch.php',
        method: 'POST',
        data: $(#formUnlock).serialize(),
        success: function()
        {
        alert("unlocked");
        }
   }); 
});

现在我要检查$id是否有值!我将如何获取$id变量到我的主脚本?

在php中,如果你只想传递一个id给javascript,你可以打印id

echo $id;

ajax响应接收到的任何数据都将作为参数传递给成功回调函数。要么你必须在成功回调中执行成功操作,要么你必须编写一个函数,在ajax成功时执行并调用该函数并传递参数

$("#formUnlock").submit(function(e)
{
   e.preventDefault();
   $.ajax(
   {
        url: '../scripts/cch.php',
        method: 'POST',
        data: $(#formUnlock).serialize(),
        success: function(responseData)
        {
            if (responseData == '' )
            {
                 alert("Sorry failed");
            }
            else
            {
                alert("Success");
            }
        }
   }); 
});