Sui Go
Sui Go

Reputation: 463

How to get an array value from php via jquery/ajax and pass it to another php file?

Here's my php code, The $contents must be passed after the change event..

<?php

    if($flag == true){
        $contents = $store; // this is the array that needs to be passed
        $color = array();
        foreach($store as $item){
            $color[] = $item['color'];
        }
        $u_color = array();
        $u_color = array_unique($color);
        echo '<label>Available Colors:</label>
        <select id="color">
            <option>Select a color</option>';
        foreach($u_color as $item){
            echo '<option>'.$item.'</option>';
        }
        echo '</select>';
    }

    ?>

Here's my jquery/ajax code that should be triggered after the change event

$(function () {
    $('#color').live('change', function () {
        var data = <? php echo json_encode($contents); ?> ;
        var the_array = $.parseJSON(data);
        $.ajax({
            url: 'wp-content/themes/twentyeleven-child/receiver.php',
            type: 'post',
            data: {
                data: the_array
            },
            datatype: 'json',
            success: function () {

            }
        });
    });
});

Here's my receiver.php

   <?php

print_r($_POST['data']);

?>

Here's what contains my $contents:

    Array
(
    [0] => Array
        (
            [size] => 2
            [price] => $59.00
            [color] => Black
        )

    [1] => Array
        (
            [size] => 4
            [price] => $59.00
            [color] => Black
        )

    [2] => Array
        (
            [size] => 6
            [price] => $59.00
            [color] => Black
        )

    [3] => Array
        (
            [size] => 8
            [price] => $59.00
            [color] => Black
        )

    [4] => Array
        (
            [size] => 10
            [price] => $59.00
            [color] => Black
        )

    [5] => Array
        (
            [size] => 12
            [price] => $59.00
            [color] => Black
        )

    [6] => Array
        (
            [size] => 14
            [price] => $59.00
            [color] => Black
        )

    [7] => Array
        (
            [size] => 16
            [price] => $59.00
            [color] => Black
        )

)

Upvotes: 0

Views: 4912

Answers (3)

Reflective
Reflective

Reputation: 3917

I don't see an actual Ajax request here ... use $.ajax ... or $.post ... or $.get

EDDIT & ADD:

var data = '<?php echo json_encode($array);?>'; var the_array= $.parseJSON(data);

a complete example:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<?php
  $array = array();
  $array['id'] = '2335';
  $array['data'] = 'data string';
?>

<script type="text/javascript">
$(function() {
  var data = '<?php echo json_encode($array);?>';
  var the_array = $.parseJSON(data);
  // keep in mind that the_array is Object not Array 
  // may be you should convert it to an Array
  // other way is to post 'data' adding it to array  var the_array = ('data': data);   
});
</script>
</body>
</html>

Upvotes: 1

redShadow
redShadow

Reputation: 6777

Send ajax request to page

you can use jQuery.ajax() to send your array to server via POST (It thing your code is missing some piece)

$.ajax({
  type: 'POST',
  url: 'receiver.php',
  data: { myarray: the_array },
  success: function(data){
    // executed on success
  },
  dataType: dataType
});

Initialize values in rendered page

If you want to pass some code to js in the rendered PHP template, use json_encode() to "convert" your PHP array/object into a JavaScript Object Notation string:

<script>
var the_array = <?php print json_encode($the_array); ?>;
// ...
</script>

Reply to async (ajax) request

If you want receiver.php to return some json to the caller (eg. the success function), just print it

<?php

// .. do stuff here ..

header('Content-type: application/x-json');
print json_encode($the_array);

?>

You'll then "automagically" get $the_array as data in your success: callback.

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55740

Try this

 var the_array = $array.join() ;

Will join the array as a comma separated string and this can be passed to the Ajax Request..

Otherwise you can serialize your array and pass the arrayobject to your Request too,

 vat the_array = $array.serializeArray();

Upvotes: 1

Related Questions