簡體   English   中英

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的方法

在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中讀取它們來創建更復雜的響應和額外的數據。

我會將你的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中編寫您將在回調函數中測試的任何內容:

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;

然后使用它在jQuery中獲取它。 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

}):

它真的很容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM