开发者

Sending Back Ajax variable

开发者 https://www.devze.com 2022-12-28 05:50 出处:网络
I am trying to use jquery to do a client side validation to do a check and return with a error or success.

I am trying to use jquery to do a client side validation to do a check and return with a error or success.

I know how to echo text/html back as the response, but here's what I am trying to do.

On Error, send back a message, so the user can re-submit a form. On Success,开发者_开发知识库 re-load the page.

I am not sure how to get it to send a variable which I think will let me do either condition in jquery.

Any suggestions??


The javascript could be somthing like this

$.ajax({
    type: 'POST',
    url: ajaxPage,
    data: postContent,
    success: function(response) {
        //Assume that the php failed
    },
    error: function() { alert("Ajax request failed."); }
});

Now in the ajax PHP file you could have this

<?php
    //Check for validation

    if(validation == 'success') {
        header('Location: '.FILE_LOCATION);
    }
    else {
        exit('Form submit failed. Please try again');
    }
?>

Hope this helps! Metropolis

EDIT SINCE NEW INFO

If you would like to use a json object you could do it like the following

<?php
    $response = array('validationStatus' => true);
    exit(json_encode($response));
?>

Its not really a big deal if you use json or not, you can still pass a string back either way. I think json is really better for more complicated data that you need to pass back, but theres probably 10 different ways to do this.


Check out the documentation, http://api.jquery.com/jQuery.ajax/ you will find a success and error option which you provide a function to be called when the request is successful or fails.

$.ajax({
    url: 'ajax/test.html',
    success: function(data) {
        //reload your page
    },
    error: function(data) {
        alert("Resubmit the form!");
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号