0

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
        )

)
2
  • 1
    could you put the code of 'receiver.php' ? what is actually your problem? Commented Oct 16, 2012 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? Commented Oct 16, 2012 at 22:38

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

You said .. $array = array(); //assuming this contains many values
var data = "<?php echo json_encode($array);?>"; var the_array= $.parseJSON(the_array);
uuuhhhhh this type of question makes my brain hurt. can we not close and get them to just you know ... learn programming or something
1

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.

Comments

1

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>

4 Comments

Firefox firebug - debug it and see what json_encode returns as a string ... json_encode has some options you can try
why no values? i'm sure my array has values on it.. :(
if I put that ' or " the firebug returns SyntaxError: JSON.parse: unexpected character so I remove it..hmm my code it retrieving it is print_r($_POST[receive_array]);
And i noticed in my firebug, post data= no value in data

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.