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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.