Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
        )

)
share|improve this question
1  
could you put the code of 'receiver.php' ? what is actually your problem? – Fopa Léon Constantin Oct 16 '12 at 22:38
1  
That's not how things work (in php & ajax is what I ment ), you need to provide more information. Why is it that your array can exist in file A but not in file B and so needs to be passed via ajax? – Alin Mircea Cosma Oct 16 '12 at 22:38
@AlinMirceaCosma I updated my question.. – Sui Go Oct 16 '12 at 23:47
updated.. please see thanks! :) – Sui Go Oct 16 '12 at 23:48

3 Answers

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();
share|improve this answer
$array in not defined :( – Sui Go Oct 16 '12 at 22:42
You need to have elements in the array right – Sushanth -- Oct 16 '12 at 22:43
what elements?? – Sui Go Oct 16 '12 at 22:46
You said .. $array = array(); //assuming this contains many values – Sushanth -- Oct 16 '12 at 22:48
var data = "<?php echo json_encode($array);?>"; var the_array= $.parseJSON(the_array); – Reflective Oct 16 '12 at 22:53
show 1 more comment

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.

share|improve this answer
This is a quick-and-dirty explanation, let me know if you need more detail... – redShadow Oct 16 '12 at 22:50
it returns null.. :( – Sui Go Oct 16 '12 at 22:53

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>
share|improve this answer
i forgot to put it sorry :) – Sui Go Oct 16 '12 at 22:45
it has errors JSON.parse: unexpected character – Sui Go Oct 16 '12 at 23:01
Firefox firebug - debug it and see what json_encode returns as a string ... json_encode has some options you can try – Reflective Oct 16 '12 at 23:04
$.parseJSON(the_array)? – Sui Go Oct 16 '12 at 23:17
sorry , my fault $.parseJSON(data) is the correct one – Reflective Oct 16 '12 at 23:19
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.