1

Here is my code in my jquery/javascript that is performing the ajax request...

else {
        $.post("http://www.example.com", {email: val}, 
        function(response){
            finishAjax('email', response);
        });
    }
        joinAjax('cemail');
}
if (id == 'cemail') {

        $('#cemailMsg').hide();
    var email = $('#email').val();
        if (errors.email == false) {

                if (val != email) {

The ajax request is working and all is good, but the next if statement where the argument is:

if (errors.email == false) {

I need to be able to set this variable from my ajax.php file to either true or false and return it so the javascript can read it. Is this possible?? It is because the ajax is checking if the username is available or not, if not available a form error would be true preventing the form submitting, or if false then the user can submit.

My ajax.php file:

if ($_REQUEST['email']) {

$q = $dbc -> prepare("SELECT email FROM accounts WHERE email = ?");
$q -> execute(array($_REQUEST['email']));
if (!$q -> rowCount()) {

    echo '<div id="emailMsg" class="success">Email OK</div>';
    exit();
}
else {

    echo '<div id="emailMsg" class="error">Email taken</div>';
    exit();
}
}

2 Answers 2

2

Why not have your ajax.php return an associative array like so:

echo json_encode(array("email" => "success", "key2" => "val2"));

And then use

var responseJSON = JSON.parse(response);

In the $.post callback function to read the response and act accordingly. You can generate the appropriate markup (divs or whatever) client-side and insert it.

Sign up to request clarification or add additional context in comments.

Comments

0

You may want to back up and determine what you want here. If all you want to do is display a different message depending on the result of the post, return (from the post) the smallest amount of information. In this case the value could simply be boolean. email valid or invalid.

I would recommend returning json from your post.

If you are returning json, the success method (function(response){}) is where you could operate on or evaluate the response.

If you return json (which can be strings from PHP, in this case), it evaluates to JavaScript objects. so if you returned '{"errors":"true"}' it will evaluate to (response.errors === true) within your success method in the post call.

'errors' is undefined, correct? Make it a property of the response json object, and be sure to test for undefined values on the data you get back.

Generate HTML or manipulate CSS based on the values you get from the post.

In that vein, jquery makes it easy to add HTML to the DOM. Try

(can someone fix my formatting and add a code tag if you can edit? I'm on my cell ATM)

  
 $('<div></div>', { 
    text : 'your text here',
    class : 'yourclass' 
}).appendTo($(element));

1 Comment

Yes I like the idea of returning true or false and inserting everything after, all I need is a boolean value of either true or false. Thanks for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.