1
$array = array("1" => "box of chocolates", "2" => "mylar balloons", "3" => "stuffed animals");
    <?php 
                $productWithItem = $array;
                foreach ($productWithItem as $pwi) {
                ?>
                    <a href="#" id="product_name"><?php echo $pwi->name ?></div></a>
                <?php    
                }
                ?>
    <script type="text/javascript">
    jQuery(function(){
        var value_array = ?;
    });
    </script>

I Want get array value from id="product_name", but I don't know get value from on this javascript, you can help me, thank you

5
  • 1
    When did PHP get this ' '.join(['array', 'definition', 'syntax'])? Commented Sep 14, 2011 at 4:56
  • 2
    This question is poorly worded. The way the foreach loop is worded, there would be many anchors with the same "product_name" ID. Is that really what you want? Commented Sep 14, 2011 at 4:58
  • @NullUserException 2 mins ago. But valid point. Hai: What he means, is that you are mixing two programming languages.. and the problem is, that they don't mix :/ Commented Sep 14, 2011 at 4:59
  • @jimbojw: I want get all value product name from code php from id of it, then javascript get this array value Commented Sep 14, 2011 at 5:01
  • 3
    @Hai Are you running this through Google Translate? I'm sorry, but your English is incomprehensible. Commented Sep 14, 2011 at 5:03

3 Answers 3

3

If you are intending to keep your <script> in your html code, build your array in php and use echo:

<script type="text/javascript">
jQuery(function(){
    var value_array = <?php echo $yourarray ?>;
});
</script>

That's not an elegant solution, though.

Sign up to request clarification or add additional context in comments.

Comments

3

Make product_name as id into class . now $('.product_name') this will be automatically array of objects

example markup

<a href="#" class="product_name">aaa</div></a>
<a href="#" class="product_name">bb</div></a>
<a href="#" class="product_name">cc</div></a>

using each you can extract array

$('.product_name').each(function(){

  alert($(this).text());

});

Comments

1

Put your array string in the name attribute of the <a>. Then you can use jQuery to get it back:

jQuery(function(){
    var ele= [YOUR ELEMENT]
    var value_array = $.parseJSON($(ele).attr("name"));
});

Comments

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.