Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Variables

Status
Not open for further replies.

mrtopher

Programmer
Aug 27, 2003
34
US
This is difficult to explain, please bare with me. I am creating a form, and part of that form will be a list of check boxes. Those check boxes are created by using the following:

Code:
$times = $badge->getTimeFrame();
foreach($times as $id => $value)
	{
		echo '<input type="checkbox" name="chkTime' . $id . '" value="' . $id . '">' . $value . '<br />';
	}

My question is, since the check boxes (and their names) are created dynamically how would I go about getting their values after the form has been submitted? In other words, is there a way to create a variable name dynamically and then display or pass its value?
 
As your passing the data from one side (ie, where the user submits the data) to another side (ie, where they see the output or it goes to a database) if im on the right tracks.

When your generating your checkboxes add each checkbox to an array, then once the form is posted post this array also and dynamically generate a handler for the data thats been submitted.

It might not work though, it depends on what your doing exactly. Just an idea

Triangular sandwiches taste better than square ones.

Rob
 
The variable $_REQUEST is an associative array, you can examine the keys as with any hash.
 
You should also try the following code:
Code:
$times = $badge->getTimeFrame();
foreach($times as $id => $value)
    {
        echo '<input type="checkbox" name="chkTime[]" value="' . $id . '">' . $value . '<br />';
    }

In your program, you can get the array of values by doing:
Code:
$chkTime = $_POST['chkTime'];
and then iterate through the array.

Ken
 
Correction to code by kenrbnsn:

Code:
$times = $badge->getTimeFrame();
foreach($times as $id => $value)
    {
        echo "<input type=\"checkbox\" name=\"chkTime[{$id }]\" value=\"{$id}\">{$value}<br />";
    }

I put the $id in the chkTime[]

test-parsing:
Code:
for ($i = 0; $i < count($chkTime); $i++)
  {
    echo $chkTime[$i] . "<br />";
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top