1

I have a form that submits data to a file called 'submit_link.php'. To submit the entered form values I am using this function with jQuery:

function submitLink() {
    if ($('#lnk_email2').val().length == 0) {
        $.ajax({
            type:'POST',
            url: '<?php echo $setting['site_url'] .'/';?>includes/misc/submit_link.php',
            data:$('#add_link_form').serialize(),

            beforeSend: function() {
                $('#lnk_exch_ini').remove();
                $('#loader_submit').css('display', 'block').html("<img src='<?php echo $setting['site_url'] . $setting['template_url'] .'/';?>images/loader.gif' />");
            },

            error: function() {

            },

            success: function() {
                $('#loader_submit').remove();
            },

            complete: function() {
                $('#lnk_exch_content').load('includes/forms/add_link_form2.php');
            }
        });
    } else {
        //do nothing
    }

    return false;
}

This is the file submit_link.php:

if ($user['login_status'] == 1) {
    $email = $user['email'];
    $submitter = $user['id'];
}
else {
    $submitter = 0;
}

if (strpos($url, "http://") === false) {
    $url = 'http://'.$url;
}

mysqli_query($con, "INSERT INTO ava_links SET name='$anchor', url = '$url', description = '$description', sitewide = 1, published = 0, 
submitter = $submitter, submitter_email = '$email'") or die (mysqli_error($con));

$link_id = mysqli_insert_id($con);

$referral_link = $setting['site_url'].'/?r='.$link_id;

$step2 = LINK_EXCHANGE_STEP2;

Now my problem is that I want the last three variables $link_id, $referral_link and $step2 returned from the server via json. How do I do this? How can I use the json-encoded data inside of my function submitLink() I posted above? How can I access each of the objects stored inside the json-data?

2 Answers 2

3

Create a php array:

$output=array('link_id'=>$link_id, 'referral_link'=>$referral_link);

Then :

  echo json_encode( $output):

Set dataType:'json' in the $.ajax options also, then consume the json in your success callback

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

10 Comments

Just one question: why do I have to echo json in my PHP-file?
aren't you wanting the json returned to client ( browser) as part of ajax request? If so have to tell php to send it thus echo
Yes, of course. I'll check it out - one minute. I'm sorry for that question but I do not have very much json-experience yet.
as can see is quite simple at server... create the json string from php array or object, then send it out. There are lots of tutorials on web and lots of posst on this site how to parse json to html in javascript/jQuery if that's your next step
I get this error: 'parsererror SyntaxError: JSON Parse error: Unrecognized token '<'' How to deal with this kind of error?
|
2

Try to use following PHP script.

<?php

//set header for JSON response
header("Content-Type: application/json");

//render array in JSON format
echo json_encode(array('status' => 200, 'data' => array('link_id'=>$link_id, 'referral_link'=>$referral_link)));

always have status code in response to check whether this response contains success or error, according to this status code use jQuery to interact with User.

here is complete tutorial to fetch JSON response using jQuery http://zainultutorials.blogspot.in/2013/10/ajax-how-to-retrieve-json-response-from.html

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.