最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery getting array from php json_encode - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Jul 30, 2013 at 10:09 Tushar Gupta - curioustushar 57.1k24 gold badges105 silver badges109 bronze badges asked Jan 11, 2013 at 15:46 the_summer_beethe_summer_bee 4935 gold badges10 silver badges23 bronze badges 4
  • Well you can't just getJSON as it seems to be dependent on POST data being there – Esailija Commented Jan 11, 2013 at 15:48
  • What do you want to do with the data? – dnagirl Commented Jan 11, 2013 at 15:48
  • You shall do exactly what the docs describe. – Christian Commented Jan 11, 2013 at 15:49
  • You have a trailing comma after the line for "description." – Expedito Commented Jan 12, 2013 at 14:58
Add a comment  | 

4 Answers 4

Reset to default 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);
            });
        }
    });
});

You can access an array in this way

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

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);

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);
?> 
发布评论

评论列表(0)

  1. 暂无评论