3

I created an array in php. I simply want to get the data it in jquery

here is the array in support.php:

$postData = array(
        "error" => $error,
        "successInfo" => $successInfo,
        "email" => $_POST["email"],
        "subject" => $_POST["subject"],
        "description" => $_POST["description"],
);

echo json_encode($postData);

What shall I do in javascript side using jquery.getJSON?

Thanks in advance!

4
  • Well you can't just getJSON as it seems to be dependent on POST data being there Commented Jan 11, 2013 at 15:48
  • What do you want to do with the data? Commented Jan 11, 2013 at 15:48
  • You shall do exactly what the docs describe. Commented Jan 11, 2013 at 15:49
  • You have a trailing comma after the line for "description." Commented Jan 12, 2013 at 14:58

4 Answers 4

9

It depends a lot on what you want todo with it, but this is a basic way of accessing the element keys. You can simply use the dot operator for each element key: "data.email" etc.

$.ajax({
    type: 'POST',
    url: 'support.php',
    success: function(result) {
        var data = jQuery.parseJSON(result);
        alert(data.email);
    }
});

INSERT INTO HTML ELEMENT:

I created a div with id="landingPad" and swapped out the alert line with:

$('#landingPad').html(data.email);

MAKE A LIST OF THE DATA RECEIVED:

Then I changed my div into an unordered list:

<ul id="landingPad"></ul>

After changing the success function, it listed all the data received from support.php:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        url: 'support.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            $.each(data, function(index, value) {
                $("#landingPad").append("<li>" + value + "</li>");
            });
        }
    });
});

CREATE FORM ELEMENTS WITH AJAX DATA:

Next, I created the following form:

<form name="http://example.com/edit_my_values" action="post">
    <div  id="landingPad"></div>
    <input type="submit" name="go" value="Edit Values"/>
</form>

Then by editing the AJAX, I created a form on the fly with the values received:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        url: 'support.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            $.each(data, function(index, value) {
                $("#landingPad").append('<input type="input" name="'+index+'" value="'+value+'"/><br/>');
            });
        }
    });
});

INSERT DATA INTO AN EXISTING FORM:

Given the following form:

<form name="http://example.com/edit_my_values" action="post">
    <label for="error">Error </label><input type="text" name="error"/><br/>
    <label for="successInfo">Success </label><input type="text" name="successInfo"/><br/>
    <label for="email">Email </label><input type="text" name="email"/><br/>
    <label for="subject">Subject </label><input type="text" name="subject"/><br/>
    <label for="description">Description </label><input type="text" name="description"/><br/>
</form>

You can fill your fields with AJAX data as follows:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        url: 'support.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            $.each(data, function(index, value) {
                $('[name='+index+']').val(value);
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can access an array in this way

$.ajax({
      type: 'POST',
      url: 'support.php',
      success: function(result) {
        $('#content1').html(result[0]);
      },
  });

Comments

2

jQuery

success: function(result) {
   var obj = jQuery.parseJSON(result);
   alert(obj.link);
}

Note: Use jQuery.parseJSON() function, to get all data without getting an error.

php

Tip: If there are any possibility that array may contain html tags then encode data like that:

$data['success']='1';
$data['link']="<a href='http://stackoverflow.com/' target='_blank' style='color:#68dff0'>Click Here</a>";
echo json_encode($data, JSON_HEX_QUOT | JSON_HEX_TAG);

Comments

0

Here is a full working version:

<!DOCTYPE html>
<html lang="en">
    <head>

        <title>HTML</title>

        <script src="http://code.jquery.com/jquery-latest.min.js"></script>

        <script>
            $(document).ready(function(){
                $("#submit").click(function(){
                    fn_val=$('#fn').val(); 
                    ln_val=$('#ln').val(); 
                    $.ajax({
                        type: 'POST',
                        url: 'resp.php',
                        data : {fn: fn_val , ln: ln_val},
                        success: function(result) {
                            var data = $.parseJSON(result);
                            $("#the_results").append('Associative array returned' + '<BR>');
                            $.each(data, function(index, value) {
                                $("#the_results").append("index =  " + index + ', data = ' + value + '<BR>');
                            });
                        }
                    });
                });
            });
        </script>
    </head>

    <body>
        <div>
            First name: <input type="text" name="fn" id='fn'>
            Last Name: <input type="text" name="ln" id='ln'>
            <button id="submit">Pass Data to PHP</button>
        </div>
        <div id="the_results"></div>
    </body>
</html>

Thats the html file, below is the php file resp.php

<?php
$resp_arr = array('First Name' => $_POST["fn"], 'Last Name' => $_POST["ln"]);
echo json_encode($resp_arr);
?> 

1 Comment

He's trying to access support.php. Just a little detail, but that's probably why you failed to get my code to work. Other than that, it's just copy and paste.

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.