Jump to content

Recommended Posts

I've been working on this for some time and can't seem to get it. I have take and array with the id and title as items in it. Like the following (using codeigniter)

 

<?php
		$username = (int) $this->session->userdata('username');
        $query = $this->db->query("SELECT * FROM Add_ons WHERE username = '$username' ORDER BY created DESC");	
		foreach($query->result() as $row)
		{
		$add_ons[$row->id] = $row->title;
		}

		if(isset($add_ons))
		{
		return $add_ons;
	    }
?>

 

now i'm trying to display a form where each add_on has its own checkbox. like the following

 

<?php

if(isset($add_ons))
{

echo "<fieldset><legend>Add-Ons</legend>";

foreach($add_ons as $id => $title)
{
                $check = TRUE; // <-- part i need help with. 

	echo form_checkbox("add_ons[$id]", "yes", $check)." $title.<br><br>";
}

echo "</fieldset>";
}
?>

 

 

codeigniter takes the $check value and if its true will check the box, if false, leave it unchecked. Also, the first parameter in codeigniter's form_checkbox() function is the input name.

 

Now i'm trying keep the correct value (true/false) based on the user's selection if the user submits the form and there's an error. So i tried this...but it's not working

 

<?php

if(isset($add_ons))
{

echo "<fieldset><legend>Add-Ons</legend>";

$num = 0;
foreach($add_ons as $id => $title)
{
	$num++;
	if($_POST["add_ons[$id]"] == "yes")// ||  strtolower($_SERVER['REQUEST_METHOD']) !== 'post')
	{
		$check = TRUE;
	}
	else
	{	
		$check = FALSE;
	}
	echo form_checkbox("add_ons[$id]", "yes", $check)." $title.<br><br>";
}

echo "</fieldset>";
}

?>

 

 

i'm getting an "Undefined index: add_ons[2]" php error.

 

anybody have suggestions?

 

Link to comment
https://forums.phpfreaks.com/topic/243123-foreach-checkbox-help/
Share on other sites

finally figured it out...not sure if its the cleanest but it works

 

<?php
if(isset($_POST['add_ons']))
{
foreach($_POST['add_ons'] as $key => $id)
{
	$addons[] = $id; //create array of checked add_ons
}
}
if(isset($add_ons))
{

echo "<fieldset><legend>Add-Ons</legend>";

foreach($add_ons as $id => $title)
{
	if(isset($addons)) //see if any add_ons were checked
	{
		if(in_array($id,$addons)){$check = TRUE;}else{$check = FALSE;} //if this add_on is in the addons array -> check, else -> don't
	}
	else               //no addons were checked
	{
		$check = FALSE; 
	}

	echo "".form_checkbox("add_ons[]", $id, $check)." $title.<br><br>";
}

echo "</fieldset>";
}

?>

Ok, I assume $add_ons is an array of ALL the possible checkboxes. The problem is that only checked checkboxes are included in the POST data. So, if you try to reference a POST value with an id of '2' and the user did not check that box it will not be in the post data. Also, you are referencng the POST field incorrectly. It is not

$_POST["add_ons[$id]"

 

It is

$_POST['add_ons'][$id]

 

 

Try this:

if(isset($add_ons))
{
   
   echo "<fieldset><legend>Add-Ons</legend>";
   
   $num = 0;
   foreach($add_ons as $id => $title)
   {
      $num++;
      echo form_checkbox("add_ons[$id]", "yes", isset($_POST['add_ons'][$id]))." $title.<br><br>";
   }
   
   echo "</fieldset>";
}

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.