darksniperx Posted November 27, 2007 Share Posted November 27, 2007 on my html form, I have <label><input type='checkbox' name='cat_id' value='1'/>Food</label> <label><input type='checkbox' name='cat_id' value='2'/>Gas</label> <label><input type='checkbox' name='cat_id' value='3'/>Religion</label> <label><input type='checkbox' name='cat_id' value='4'/>Chips</label> <label><input type='checkbox' name='cat_id' value='5'/>Drinks</label> on my php side I have foreach ($_POST["cat_id"] as $value) //gives error for cat_id { $this->fusion->addToFetcategory($feed_id,$entry_id,$value); } before I used: <label><input type='checkbox' name='cat_id[]' value='5'/>Drinks</label> but due to that I needed to use the array with dom I had it to: <label><input type='checkbox' name='cat_id' value='5'/>Drinks</label> in order to use : for (var i=0; i < document.entry_form.cat_id.length; i++) { if (document.entry_form.cat_id[i].value == current_id ) { document.entry_form.cat_id[i].checked = true; } } what can I do to get my foreach php code working again? Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/ Share on other sites More sharing options...
kenrbnsn Posted November 27, 2007 Share Posted November 27, 2007 You need to make cat_id an array in your form: <label><input type='checkbox' name='cat_id[]' value='1'/>Food</label> <label><input type='checkbox' name='cat_id[]' value='2'/>Gas</label> <label><input type='checkbox' name='cat_id[]' value='3'/>Religion</label> <label><input type='checkbox' name='cat_id[]' value='4'/>Chips</label> <label><input type='checkbox' name='cat_id[]' value='5'/>Drinks</label> Ken Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-399979 Share on other sites More sharing options...
darksniperx Posted November 27, 2007 Author Share Posted November 27, 2007 You need to make cat_id an array in your form: <label><input type='checkbox' name='cat_id[]' value='1'/>Food</label> <label><input type='checkbox' name='cat_id[]' value='2'/>Gas</label> <label><input type='checkbox' name='cat_id[]' value='3'/>Religion</label> <label><input type='checkbox' name='cat_id[]' value='4'/>Chips</label> <label><input type='checkbox' name='cat_id[]' value='5'/>Drinks</label> Ken if I do it in that form, then I get an error calling document.entry_form.cat_id[i].checked = true;// it complains about [] brackets Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-399994 Share on other sites More sharing options...
kenrbnsn Posted November 27, 2007 Share Posted November 27, 2007 For this to work in both Javascript and PHP, you need to use a id value and use it in Javascript. Try this (no guarantees): <label><input type='checkbox' name='cat_id[]' id=js_cat_id value='1'/>Food</label> <label><input type='checkbox' name='cat_id[]' id=js_cat_id value='2'/>Gas</label> <label><input type='checkbox' name='cat_id[]' id=js_cat_id value='3'/>Religion</label> <label><input type='checkbox' name='cat_id[]' id=js_cat_id value='4'/>Chips</label> <label><input type='checkbox' name='cat_id[]' id=js_cat_id value='5'/>Drinks</label> and document.entry_form.js_cat_id[i].checked = true; Ken Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-400012 Share on other sites More sharing options...
teng84 Posted November 27, 2007 Share Posted November 27, 2007 i dont think that will work.. correct me if im wrong but i guess naming the field like this name[] doesnt recognize by JS that your dealing with array.. unlike php when you have this name[] it will automatically give the array index as increminting values.. im also not also sure but try to initialize your field name as name[1] name[2] etc.. <label><input type='checkbox' name='cat_id[1]' id=js_cat_id value='1'/>Food</label> <label><input type='checkbox' name='cat_id[2]' id=js_cat_id value='2'/>Gas</label> <label><input type='checkbox' name='cat_id[3]' id=js_cat_id value='3'/>Religion</label> <label><input type='checkbox' name='cat_id[4]' id=js_cat_id value='4'/>Chips</label> <label><input type='checkbox' name='cat_id[5]' id=js_cat_id value='5'/>Drinks</label> Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-400013 Share on other sites More sharing options...
KevinM1 Posted November 27, 2007 Share Posted November 27, 2007 Try: document.forms["entry_form"].elements["cat_id[]"] Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-400019 Share on other sites More sharing options...
JovanLo Posted November 27, 2007 Share Posted November 27, 2007 try this : echo $item will print the selected values. <? $cat_id_array=$_POST["cat_id"]; foreach($cat_id_array as $key=>$item){ echo $item; } ?> <html> <form method="post" action""> <label><input type='checkbox' name='cat_id[]' value='1'/>Food</label> <label><input type='checkbox' name='cat_id[]' value='2'/>Gas</label> <label><input type='checkbox' name='cat_id[]' value='3'/>Religion</label> <label><input type='checkbox' name='cat_id[]' value='4'/>Chips</label> <label><input type='checkbox' name='cat_id[]' value='5'/>Drinks</label> <input type="submit" value="submit"> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-400023 Share on other sites More sharing options...
JovanLo Posted November 27, 2007 Share Posted November 27, 2007 if the name of the above form is "test" then, you can access the value of the third checkbox element by using the following line (javascript) var id_cat="cat_id[]"; var third_val =document.test[id_cat]["2"].value; Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-400039 Share on other sites More sharing options...
darksniperx Posted November 28, 2007 Author Share Posted November 28, 2007 i have used id to solve it: <label><input type='checkbox' name='cat_id[]' id ='category_id' value='5'/>Drinks</label> thx, it works now!!! Quote Link to comment https://forums.phpfreaks.com/topic/79027-solved-foreach-_postcat_id-as-value-error/#findComment-401156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.