JQuery 当数据从 .post 返回时执行某些操作


JQuery Do something when data returned from .post

>我有一个网站正在使用JQuery中的.post函数发送以运行php脚本。此脚本将一些数据添加到数据库中,但如果数据库中已存在数据,则不会添加数据。根据是否添加了数据,我希望javascript做不同的事情。我不知道如何让 php 返回一个值,上面写着"输入的数据"或"未输入的数据",以便 javascript 可以使用该值来决定下一步操作。

这是javascript,我只希望在php返回数据已输入数据库时才发生追加。

$('#addpios').click(function() {
  var scopesheetidvalue = $("#scopesheetid").val();
  var piovalue = $("#pioselected").val();
$.post("addpio.php", { scopesheetid: scopesheetidvalue, pionumber: piovalue },function(data){
$('#pioslist').append("<li><input class='removepio' type='submit' value='"+piovalue+"' /><span class='listitem'>PIO "+piovalue+"</span></li>");} 
    );   
}); 

这是 PHP

$scopesheetid = $_POST['scopesheetid'];
$pionumber = $_POST['pionumber'];
$alreadyexisitssql = "SELECT * FROM [ScopesheetPIO] WHERE [ScopesheetID]='$scopesheetid' AND [PIONumber]='$pionumber'";
$alreadyexisits=odbc_exec($connection,$alreadyexisitssql);
if(odbc_fetch_row($alreadyexisits)){
//retrun a value that says the data already exists
}
else{
    $addpiosql = "INSERT INTO [ScopesheetPIO] ([ScopesheetID], [PIONumber]) VALUES ('$scopesheetid', '$pionumber')";
    $addpioresult=odbc_exec($connection,$addpiosql);
//retrun a vlaue that the data was added
}

我真正想要的是一种将值从PHP脚本传递回Jquery的方法。

我会将您的jQuery修改为以下内容:

$.post('addpio.php', { ... }, function(data) {
    if (data.result) {
        $('#pioslist').append(...);
    }
}, 'json');

在 PHP 文件中,在插入数据时使用此选项:

echo json_encode(array(
    'result' => TRUE
));

当数据已存在时,请使用此选项:

echo json_encode(array(
    'result' => FALSE
));

在 PHP 中构建一个数组并将其输出为 JSON。然后在脚本中检查返回的 JSON。

if(odbc_fetch_row($alreadyexists)){
    // Return a value that says the data already exists
    $result = array(
        "error" => "Data already exists"
    );
} else {
    // Database stuff goes here...
    // Return a value that the data was added
    $result = array(
        "success" => 1
    );
}
echo json_encode($result);

JavaScript $.post回调:

function(data) {
    if(data.success) {
        // Append element to HTML
    } else {
        // An error occurred, inform the user
        // Don't actually use alert(), this is just for demonstrating purposes
        alert(data.error);
    }
}

稍后,您可以使用额外的数据创建更复杂的响应,只需将它们添加到 $result 数组并从 JavaScript 中的data读取它们即可。

在 PHP 中编写任何将在回调函数中测试的内容:

.PHP

if(odbc_fetch_row($alreadyexisits)){
 echo "ko";
}
else{
    $addpiosql = "INSERT INTO [ScopesheetPIO] ([ScopesheetID], [PIONumber]) VALUES ('$scopesheetid', '$pionumber')";
    $addpioresult=odbc_exec($connection,$addpiosql);
    echo "ok";
}

.JS

$.post("addpio.php", { scopesheetid: scopesheetidvalue, pionumber: piovalue },function(data){
    if( data == 'ok' ) {
        $('#pioslist').append("<li><input class='removepio' type='submit' value='"+piovalue+"' /><span class='listitem'>PIO "+piovalue+"</span></li>");
    }
});  

您可以轻松地将数据从脚本传递到 jQuery。IMO 我认为最好使用 JSON 因为它更整洁。

PHP (for example):
$arrayOfData=array("val1"=>"value","val2"=>"value");
//encode it
$arrayOfData=json_encode($arrayOfData);
//print it on screen
echo $arrayOfData;

然后使用 .getJSON .

$.getJSON("url_of_php_fle",function(myData){
//now use myData as the array:
alert(myData.val1);//which will give you "value" as what you set in PHP
}):

真的就是这么简单。