Jump to content

Archived

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

cmccully

Form variables with variable names

Recommended Posts

Hi,

  I have a dynamically generated form with several checkboxes. The checkboxes are referenced by number, such as cb_1, cb_4, cb_25 etc... What I want to know is the number associated with the checkbox or boxes that were selected. How can I parse the names of the form variables submited to extract the number(s) that referenced the selected checkboxes?


cmccully

Share this post


Link to post
Share on other sites
Heres a little example for you ok.

in this example the user wants to buy somethink in this case its water but then we change the water to a fish.

good luck.

[code]
<form method="POST" action="">
<br>
do you want to  buy a coffe
<br>
<input type="checkbox" name="description[]" value="coffe">
<br>
do you want to buy a tea
<br>
<input type="checkbox" name="description[]" value="tea">
<br>
do you want to buy a coke
<br>
<input type="checkbox" name="description[]" value="coke">
<br>
do you want to buy water
<br>
<input type="checkbox" name="description[]" value="water">
<br>
<br>
<input type="submit" name="submit" value="buy now!">
</form>

<?php

if($_POST['submit']){

foreach($description as $x){

if($x=="water"){

$x="fish";

}
echo "<br>I would like to have a $x <br>";

}
}

?>[/code]

Share this post


Link to post
Share on other sites
try[code]<?php
$data = array(
array(id => 1, 'name' => 'asd'),
array(id => 2, 'name' => 'qwe'),
array(id => 3, 'name' => 'xcv'),
array(id => 4, 'name' => 'mko')
);
echo '<form method="POST">';
foreach ($data as $a) echo '<input type="checkbox" name="ch['.$a['id'].']" value="'.$a['name'].'">'.$a['name'].'<br />';
echo '<input type="submit"></form>';
if(isset($_POST['ch'])) foreach ($_POST['ch'] as $key => $value) echo "ch box no $key with value $value is selected<br />";
?>[/code]

Share this post


Link to post
Share on other sites

×

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.