0

Possible Duplicate:
Reference: Why does the PHP code in my Javascript not work?

The output always bool(false) even I already check some check boxes, I don't know what is wrong.

So far here's what I've tried

Here is my code:

    <a onclick="href='wp-content/themes/twentyelevenchild/addmarked.php?asin=<?php echo serialize('+asinValues+')?>'" class="addmarkeditems fancybox.ajax">Save Test 2</a>
</form>
<div id="results"></div>
<script type="text/javascript">
    var asinValues = $('input[name=checkboxlist]:checked').map(function(){
        return $(this).val();
    }).get();
</script>

in my addmarked.php

<?php 
var_dump(unserialize($_GET['asin']));
?>

Screenshot of my firebug:

enter image description here

Community
  • 1
  • 1
GrayFullBuster
  • 1,033
  • 3
  • 18
  • 23

2 Answers2

1

I made a quick working example of what you might be looking for:

http://jsfiddle.net/ithcy/X3Ecx/2/

Your addmarked.php should be able to use explode(",", $_GET["asin"]) or similar to do something with the values of all the checkboxes that were checked.

glomad
  • 5,539
  • 2
  • 24
  • 38
1

Assuming you added the missing echo:

<?php echo serialize('+asinValues+')?>

This totally does not do what you think it does. You cannot mix PHP and JS like that, because PHP is executed on the server first and if it happens to generate JavaScript, this will be executed on the client as generated.

So, what does PHP do here? Serializing a string with plus signs and letters: '+asinValues+'. Serialized it looks like that: s:12:"+asinValues+";

So what you have as HTML/JS is:

<a onclick="href='wp-content/themes/twentyelevenchild/addmarked.php?asin=s:12:"+asinValues+";'" class="addmarkeditems fancybox.ajax">Save Test 2</a>

Since there is no JS counterpart to PHP's unserialize, you will need another way to serialize. JSON would be a good idea:

JavaScript

'...?asin=' + JSON.stringify(asinValues)

PHP

json_decode($_GET['asin'])
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111