0

I need help in Ajax. I got this code online. This function is to check the contact.php

I have some few question so someone could assist me.
My questions :
1. Is this code good and possible to run ?
2. Can someone explain me what does the function in line 4 and line 5 does.It seems it send data to the contact.php but what is it returning?

Ajax:

var validateEmailForm = {
dataType: 'json',
        submit: function(form) {
        var redirect = false;
         $.ajax('contact.php', {data:{'email':form.email.value}}).done(function(data) {
       if ( typeof(data) == 'object' ) {
                    if ( data.status == 'valid') {
                        form.submit();
                    } else if(data.status !=='valid' {     
               alert('The e-mail address entered is wrong.');
                      }
                } else {
                    alert('Failed to connect to the server.');
                    }
            }
        }
    }

Contact.php:

<?php
error_reporting(0);

$email = $_POST['email'];
if (isset($_$POST['email']))
{
  // How to return valid to the ajax
} else {
  // How to return invalid to the ajax.
}

?>
  • Re: your questions: 1) "Good" is subjective. 2) Try it and see. – Celeo Sep 24 '14 at 23:16
  • @Celeo , my problem is with the PHP .. I'm wondering how to send data.status == valid to the ajax.. I kept on trying and fail that's why i'm posting on stackoverflow. – user3671699 Sep 24 '14 at 23:17
  • Have you tried adding debug statements to actually see what it returns? – Anthony Forloney Sep 24 '14 at 23:17
  • [Documentation for the AJAX call can be found here](http://api.jquery.com/jquery.ajax/). – Celeo Sep 24 '14 at 23:17
  • Has your question been answered satisfactorily? *If yes, please choose an accepted answer (or write your own and accept that) in order to close the question.* If not, please post additional information so that we can try to help. – cssyphus Sep 29 '14 at 16:10
  • @gibberish , haven't yet sir. – user3671699 Sep 30 '14 at 02:31

2 Answers2

0

AJAX is much easier than it sounds. You just need to see a few good examples.

Try these:

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1

https://stackoverflow.com/questions/25945137/php-fetch-content-from-one-form-and-update-it-in-other-form/25954450#25954450


The above examples demonstrate a few things:

(1) There are four formats for an AJAX request - the full $.ajax() structure, and three shortcut structures ($.post(), $.get(), and $.load() )

Until you are pretty good at AJAX, I suggest using a correctly formatted $.ajax() code block, which is what the above examples demonstrate. Such a code block looks like this:

$('#formID').submit({
    $.ajax({
        type: 'post',
         url: 'contact.php', 
    dataType: 'json',
        data: 'email=' + form.email.value
    }).done(function(data) {
        if ( typeof(data) == 'object' ) {
            if ( data.status == 'valid') {
                form.submit();
            } else if(data.status !=='valid' {     
                alert('The e-mail address entered is wrong.');
                return false;
            } else {
                alert('Failed to connect to the server.');
                return false;
            }
        }
    });
});

(2) In an $.ajax() code block, the data: line specifies the data that is sent to the PHP processor file.

(3) The dataType: line specifies the type of data that the ajax code block expects to receive back from the PHP processor file. The default dataType is html, unless otherwise specified.

(4) In the PHP processor file, data is returned to the AJAX code block via the echo command. Whether that data is returned as html, text, or json, it is echoed back to the AJAX routine, like this:

<?php

//perform MySQL search here. For eg, get array $result with: $result['firstname'] and $result['lastname']

$out =  '<div id="myresponse">';
$out .= 'First Name: <input type="text" value="' .$result['firstname']. '" />';
$out .= 'Last Name: <input type="text" value="' .$result['lastname']. '" />';
$out .= '</div>';

echo $out;

Please try a couple of the above examples for yourself and you will see how it works.

It is not necessary to use json to send/return data. However, json is a useful format to send array data, but as you can see, you can construct a full html response on the PHP side and echo back the finished markup.


So, to definitively answer your second question, you just need to echo back some data. It is the job of the PHP file to:

(1) receive the data from the AJAX routine,

(2) Use that data in a look up of some kind (usually in a database),

(3) Construct a response, and

(4) echo (NOT return) the response back to the AJAX routine's success: or .done() functions.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • These are echo ? I'm looking for value deterrent in PHP that returns like OhGodWhy posted. – user3671699 Sep 24 '14 at 23:23
  • See updated answer. *Please experiment with the simplified examples included at top.* Note that json is a great format for sending array data, but it introduces a level of complexity that might not be necessary at this point in your AJAX career. – cssyphus Sep 25 '14 at 15:05
0

You need to return a JSON_encoded array to the ajax function, like below:

$email = $_POST['email'];
$status = false;
if (isset($_$POST['email']))
{
     $status = 'success'
} else {
     $status = false
}
echo json_encode(array('status' => $status));

?>

Further, add dataType: 'json' to your $.ajax() so that the deferred function automatically parses it as such.

Remove the typeof() as we know what we're expecting in return.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110