jeudi 13 août 2015

Discern 'echo'd returned data from PHP script to Ajax response function

I need a way to discern returned values from a PHP processing script to an Ajax return response function so that I can do different things if the response is positive vs negative.

I'm aware of indexOf (which seems to be deprecated) as well as inArray. The problem is my returned data is not an array, they are simple 'echos'.

I also want to avoid encoding the echo statements into JSON. It seems extraneous to do so.

How can I discern the returned data?

if(!mysqli_num_rows($result)){
    // Insert data into database...
        if($result){
            echo "Thanks for Registering!";
            $_SESSION['username'] = $userName;
        } else {
            echo "Something went wrong with your registration. Please try back later.";
        }
    } else {
        echo 'It seems that your email has already been registered.';
        // return false;
    }

I thought about adding a 'return false' to "mimic" a failed Ajax response, but that doesn't work.

$.ajax({
        url:'includes/register.php',
        type:'POST',
        data: formData,
        success: function(data){
             $("#register-form #message p").html(data);
         }
    }); // $.ajax
    // if registration was successful, show the message and redirect
    $('#register-form img').addClass('hide');
    $("#register-form div + div").addClass('hide');
    $("#register-form #message").removeClass('hide');
    setTimeout(function(){
        $('#overlay, form#register-form').fadeOut('slow');
        $(".register").off();
        window.location.href = 'sandbox/workshop.php';
    },2000);

    // if registration was not successful, do something else...

Thanks in advance for your replies.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire