scubby3 scubby3 - 2 years ago 80
Javascript Question

Foreach not working in php and Jquery

I am trying to do a foreach loop in php and jquery for multiple select but all i am getting to echo out is Array.


<select multiple="multiple" class="select tagz" required>


$tag = $_POST['tagz']);

foreach($tag as $tagz){
echo $tagz;

echo "Please select tags";


function serealizeSelects (select)
var array = [];
select.each(function(){ array.push($(this).val()) });
return array;
var tagz = serealizeSelects($(".tagz"));
$.post('upload.php', {tagz:tagz, title:title, category:category, description:description, price:price}, function(data){



Please tell me what i am doing wrong

Answer Source

You are doing select.each(function(){ array.push($(this).val()) }); but select variable contains only the select element and each has no meaning.You need to use each in the options.So change your selector.

           var tagz = serealizeSelects($(".tagz option"));

or if you dont want to change the selector just use find to find all options inside your select

           select.find('option').each(function(){ array.push($(this).val()) })

Of course i cannot know where you define your other variables.If you have other problems too just show me your whole javascript code so I can help you more.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download