1

I was wondering what the proper method for passing multiple sets (strings) of data back after processing a ajax call in php.

I know echo is used to send a string of data back, but what if I want to send multiple strings? and also how do I handle those string in- success: function(html){} ?

2
  • 1
    pack them to object or array, serialize and send. Commented Sep 18, 2013 at 17:02
  • @CORRUPT - how do I open it up in JS? Commented Sep 18, 2013 at 17:03

1 Answer 1

3

Encode result array in JSON format and return the response.

<?php
$arr = array ('response'=>'error','comment'=>'test comment here');
echo json_encode($arr);
?>

//the script above returns this:
{"response":"error","comment":"test comment here"}

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function (data) {
        if (data.response == 'captcha') {
            alert('captcha');
        } else if (data.response == 'success') {
            alert('success');
        } else {
            alert('sorry there was an error');
        }
    }

}); 
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.