JQuery Do something when data returned from .post
Domanda
I have a site that is sending off using the .post function in JQuery to run a php script. This script adds some data to a database but if the data already exists in the database it doesn't add the data. Depending on whether the data is added or not I want the javascript to do different things. I don't know how to get the php to return a value that says "data entered" or "data not entered" so the javascript can use that value to decided upon it next action.
Here is the javascript, I only want the append to happen if the php returns that the data was entered into the database.
$('#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>");}
);
});
Here is the 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
}
What I really want is a way to pass a value from the PHP script back into the Jquery
Soluzione
I would modify your jQuery to the following:
$.post('addpio.php', { ... }, function(data) {
if (data.result) {
$('#pioslist').append(...);
}
}, 'json');
In your PHP file, use this when the data is inserted:
echo json_encode(array(
'result' => TRUE
));
Use this when the data already exists:
echo json_encode(array(
'result' => FALSE
));
Altri suggerimenti
Build an array in PHP and output it as JSON. Then inspect the returned JSON in your script.
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
callback:
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);
}
}
Later on, you can then create more complex responses with extra data by simply adding them to the $result
array and reading them from data
in your JavaScript.
Write anything in your PHP that you'll test in your callback function :
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>");
}
});
You can easily pass data from a script to jQuery. IMO I think its best to use JSON as its much more neater.
PHP (for example):
$arrayOfData=array("val1"=>"value","val2"=>"value");
//encode it
$arrayOfData=json_encode($arrayOfData);
//print it on screen
echo $arrayOfData;
And then get it in jQuery using the .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
}):
It really is that easy.