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
I would modify your jQuery to the following:
In your PHP file, use this when the data is inserted:
Use this when the data already exists: