Jump to content

[SOLVED] foreach ($_POST["cat_id"] as $value) error???


darksniperx

Recommended Posts

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?

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

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

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

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>

 

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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.