0

I have the following PHP code that created a JSON request from posted form fields.

<?php
if ( isset($_POST['data']['Affiliate'])) { //Where ['data']['Affiliate'] is an array of posted field names of the form.
    $data = $_POST['data'];

    $base = 'https://api.whatever.com/Api?';

    $params = array(
        'Format' => 'json'
        ,'account' => $data['Affiliate']
    );

    $url = $base . http_build_query( $params );
    $result = file_get_contents( $url );
    $result = json_decode( $result );

    echo "<pre>";
    print_r( $result );
    echo "</pre>";
?>

print_r( $result ) prints the following.

stdClass Object
(
    [request] => stdClass Object
        (
            [Format] => json
            [account] => stdClass Object
                (
                    [country] => US
                    [address1] => asdasd
                    [city] => asdasd
                    [zipcode] => asdasd
                    [phone] => asdasd
                )
        )

    [response] => stdClass Object
        (
            [status] => -1
            [data] => 
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [err_code] => 3
                            [err_msg] => A user already exists with this email address.
                            [attribute_name] => Email
                            [publicMessage] => User account or user is not valid.
                        )
                    [1] => stdClass Object
                        (
                            [err_code] => 1
                            [err_msg] => City cannot be blank.
                            [attribute_name] => city
                        )
                )
        )
)

How can i use JQuery Ajax to achieve the same results and print out the array of errors in <li> tags.

2
  • 1
    The same way you would use jQuery.ajax to make any other request? What part of the process is giving you problems? Commented Sep 22, 2012 at 6:47
  • @Dcoder, do i use ajax type as JSON. Do i serialize the form. How do i sort thru the error array. I just need some jquery help on this Commented Sep 22, 2012 at 6:49

2 Answers 2

3
var errorsArr='';
var affiliate='';
//affiliate is your data of $data['Affiliate'] which you can get using jquery or js..
$.ajax({
    url:'https://api.whatever.com/Api?'
    data:{Format: 'json',account : affiliate},
    dataType:'json',
    success:function(data){
      errorsArr=data.response.errors;
    }
  }
});

you will get all errors in errorsArr. it is a json array you can iterate it for desired output

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

3 Comments

data:{Format: 'json',account => affiliate, How do all the form fields gets stringified. How does account => affiliate in data works.
@hedhud you need to use json_encode($result) before you send it back to the server. That's how you get the JSON stringified.
you can serialize the form in a variable (affiliate).
0

I suggest you don't decode the JSON to a PHP object as you did here, but keep the pure JSON, give that to your AJAX request and then do the decoding client-side (I believe there is a special jquery ajax function that gets and decodes it already for you. See Ravi's answer for that)

But if you don't want to do this: why don't you access the fields of the object you got from json_decode()?
I believe that $result->$response->$errors is the array of errors you are looking for.

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.