How can I "read" the response from a php file that I call here using ajax?

I am very new to ajax and jquery, but have stumbled upon code on the Internet that I manipulate according to my needs.

The only problem is that I want to be able to respond to ajax from PHP.

This is ajax POSTS to php page (email.php).

How can I return an email.php response if a message is sent or the message limit is exceeded (I limit the number of messages sent per user)?

In other words, I want ajax to take 1 or 0 from php code and for example:

if(response==1){ alert("message sent"); } else { alert("Limit exceeded"); } 

Here is the last part of the code: (If you need the full code, just let me know)

 var data_string = $('form#ajax_form').serialize(); $.ajax({ type: "POST", url: "email.php", data: data_string, success: function() { $('form#ajax_form').slideUp('slow').before(''); $('#success').html('<h3>Success</h3>Your email is has been sent.'); }//end success function }) //end ajax call return false; }) 

thanks

+4
source share
6 answers

You must pass an argument to your success function.

 success: function(data) { if(data == '1') { $('form#ajax_form').slideUp('slow').before(''); $('#success').html('<h3>Success</h3>Your email is has been sent.'); } } 

And in your php file you should just repeat the required answer

 if(mail()) { echo '1'; } else { echo '0'; } 
+2
source

The success function of calling $.ajax receives a parameter, usually called data , although up to you containing the answer, like this:

 success: function(data) { // Use the data } 

(It also gets a few other parameters, if you want them, more in the docs .)

The type of the data parameter will vary depending on the type of response content sent by your PHP page. If it sends HTML, data will be a string containing HTML markup; if your page sends JSON , the data parameter will be a decoded JSON object; if it is XML, data will be an instance of an XML document.

You can use 1 or 0 if you want (if you do, I would probably set the content type to "text / plain"), so:

 success: function(data) { if (data === "1") { // success } else if (data === "0") { // failure } else { // App error, expected "0" or "1" } } 

... but when I answer Ajax requests nine times out of ten, I send JSON back (so I set the Content-Type header to application/json ), because then if I use a library like jQuery that understands JSON, I I will return to a good ordered object that is easy to work with. I'm not a PHP guy, but I believe that you set the content type through setContentType and use json_encode to encode the data to send back.

In your case, I would answer:

 {"success": "true"} 

or

 {"success": "false", "errMessage": "You reached the limit."} 

so that the server code can determine which error message I will show the user. Then your success function will look like this:

 success: function(data) { var msg; if (typeof data !== "object") { // Strange, we should have gotten back an object msg = "Application error"; } else if (!data.success) { // `success` is false or missing, grab the error message // or a fallback if it missing msg = data.errMessage || "Request failed, no error given"; } if (msg) { // Show the message -- you can use `alert` or whatever } } 
+6
source

Anything you echo or return to the php file will be sent back to your jquery post. You should check this page http://api.jquery.com/jQuery.post/ and think about using formatted JSON variables to return as if it were in your email script:

echo '{"reposonse": "1"}';

This will pass a variable called response with a value of 1 back to you jquery script. Then you can use the if statement as you described.

+1
source

just enter email.php echo a 0 or 1, and then capture the data in the success event of the ajax object as follows:

 $.ajax({ url: 'email.php', success: function(data) { if (data=="1"){ ... }else{ ... } } }); 
0
source

what you do is you let your ajax file (email.php) print 1 if successful and 0 if not (or something else you want)

Then in your success function, you do something like this:

function (data) {

 $('form#ajax_form').slideUp('slow').before(''); if(data==1){ alert("message sent"); } else { alert("Limit exceeded"); } $('#success').html('<h3>Success</h3>Your email is has been sent.'); } 

So, you capture the answer in the var function data. If you have more variety in your release, you can set the dataType to "json" and your php file prints the json_encoded line so that you can access your variables in the response, for example, data.success, etc.

0
source

PHP can only return to AJAX calls, according to its output. Calling AJAX on a PHP page essentially matches the browser requesting the page.

If your PHP file was something like

 <?php echo "1"; ?> 

You would get "1" in your JavaScript callback,

those.

 success: function(data) { // here data is "1" } 

As an added note, usually AJAX responses are usually executed in JSON format . Therefore, you should format your PHP responses in JSON notation.

0
source

All Articles