Robert Granlund
asked on
AJAX Call with two returns
If I have an AJAX call that calls a php file that has two different queries and two different returns. How do I split those 2 returns and show them?
Is there an example page someplace?
Is there an example page someplace?
You should use JSON for return values. Also you could put the return values into an array and then convert it to JSON.
Here is a simple example
Server (test.php)
Server (test.php)
<?php
$return = new stdClass;
$return->name = 'John Smith';
$return->email = 'jsmith@somewhere-else.com';
die(json_encode($return));
Client<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
$.ajax({
url: 'test.php',
'type': 'POST',
'dataType': 'JSON'
}).then(function(ret) {
alert('User: [' + ret.name + '] has email address [' + ret.email +']');
});
});
</script>
Working sample here
Please see this link, which contains a similar question and a complete example.
https://www.experts-exchange.com/questions/28959110/How-to-Populate-the-SAME-Html-Form-using-php-js-jquery-and-ajax-Without-Refreshing-the-Screen.html?anchorAnswerId=41728421#a41728421
Executive summary: There are a lot of ways to skin this cat. More information on how to use jQuery is here:
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
https://www.experts-exchange.com/articles/14519/A-jQuery-Progress-Bar.html
And more information on JSON is here:
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
https://www.experts-exchange.com/questions/28959110/How-to-Populate-the-SAME-Html-Form-using-php-js-jquery-and-ajax-Without-Refreshing-the-Screen.html?anchorAnswerId=41728421#a41728421
Executive summary: There are a lot of ways to skin this cat. More information on how to use jQuery is here:
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
https://www.experts-exchange.com/articles/14519/A-jQuery-Progress-Bar.html
And more information on JSON is here:
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
ASKER
@ Julian & Ray, thank you, that is very helpful. Let me code it and test it and then post it to see if I need to clean it up. I think I understand now, more clearly.
ASKER
@julian or Ray, How would I add an IF statement to that return?
An IF statement? What would be the conditions that would indicate the use of IF?
ASKER
on my php page I have a success message or a failure message:
$success->success = "Success";
$success->fail = "Fail";
echo json_encode($success);
Depending upon if the query is successful or not, one of those two will be filled.
Then my AJAX would look something like:
$success->success = "Success";
$success->fail = "Fail";
echo json_encode($success);
Depending upon if the query is successful or not, one of those two will be filled.
Then my AJAX would look something like:
<script>
$(function () {
$("#newsjoin").on('submit', function (e) {
e.preventDefault();
});
$("#submit").click(function () {
$.ajax({
url: 'http://MYSITE.com/functions.php',
data: $("#newsjoin").serialize(),
method: 'POST',
success: function (d) {
if (d.success) {
$( "#successmessage").html(function() {
var successmessage = d.success;
return successmessage;
});
if (d.fail) {
$( "#failmessage").html(function() {
var failmessage = d.success;
return failmessage;
});
$("#newsjoin")[0].reset();
}
}
});
});
});
</script>
Depending upon if the query is successful or not, one of those two will be filled.Are you sure of that? Might want to check the definition of "success" and "failure." These pertain to the success or failure of the HTTP request, and not to the query that might be run in the PHP script that was started by the HTTP request. The promise methods you want are probably .done(), .fail(), .always(), and .then().
http://api.jquery.com/jquery.ajax/
Rather do
$return->success = true;
//OR
$return->success = false;
Then in your AJAX.then(function(result) {
if (result.success) {
...
}
else {
...
}
});
Just noticed your success function is returning values - what are you hoping to achieve with that?
The AJAX call is async - it makes no sense to return a value. Your success callcack has to do something with the returned data - either that or you have to return a promise from your .ajax call and then act on the resolution of that promise.
What do you want to do with the returned values?
The AJAX call is async - it makes no sense to return a value. Your success callcack has to do something with the returned data - either that or you have to return a promise from your .ajax call and then act on the resolution of that promise.
What do you want to do with the returned values?
ASKER
I wrote my reply in haste, walking out the door. I should have waited and been more clear. The PHP searches the database for a specific entry. If it finds that entry that matches the form submission a message is returned, "That data is already in the database" (Which I simply called "Fail")
If the data submitted is not in the DB it is inserted into the table and a message is returned "Data has been entered. (Which I called success) So I am returning one of two echoed statements. A success statement or a Fail Statement.
$success->success = "Data has been entered";
OR
$success->fail = "That data is already in the database";
So, what I am trying to figure out is in the AJAX success I want to determine if the JSON returned ->success OR ->fail .
If the data submitted is not in the DB it is inserted into the table and a message is returned "Data has been entered. (Which I called success) So I am returning one of two echoed statements. A success statement or a Fail Statement.
$success->success = "Data has been entered";
OR
$success->fail = "That data is already in the database";
So, what I am trying to figure out is in the AJAX success I want to determine if the JSON returned ->success OR ->fail .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For Success,
$response = new stdClass;
$response->success = true;
$response->message = "Data has been entered";
die(json_encode($response));
For failure$response = new stdClass;
$response->success = false;
$response->message = "That data is already in the database";
die(json_encode($response));
$.ajax({
....
}).then(function(resp) {
if (resp.success) {
alert('Success! ' + d.message);
}
else {
alert('Oops! ' + d.message);
}
});
ASKER
Instead of using IF and ELSE, is there a way to just access the content of "Success" or "Message"?
$response = new stdClass;
$response->success = "Hi, today is Friday";
$response->message = "That data is already in the database, Cause it is Friday";
$response = new stdClass;
$response->success = "Hi, today is Friday";
$response->message = "That data is already in the database, Cause it is Friday";
Yes ... but you need an if statement otherwise how do you know which one succeeded failed?
What is wrong with using the if may I ask?
Not understanding why the method presented above does not work for you - it is a pretty standard way of dealing with this situation.
What is wrong with using the if may I ask?
Not understanding why the method presented above does not work for you - it is a pretty standard way of dealing with this situation.
Both Success and Message are available in the return
in your success if you did this
in your success if you did this
alert(d.success);
alert(d.message);
Both would show - trying to understand the use case here though?
... is there a way to just access the content of "Success" or "Message"?Yes, of course. You would need to access the content before you an use it in an if() statement! Please see line 23-25 in the client-side script example here.
You can send a lot of different information back from the server-side script, but the more information you send, the more tightly coupled the client and server scripts become. Cohesion is good; tight coupling is not.
Please note that the method given in the accepted answer was first posted here