Jump to content


Photo

Form variables with variable names


  • Please log in to reply
3 replies to this topic

#1 cmccully

cmccully
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 27 October 2006 - 05:45 AM

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

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 27 October 2006 - 06:01 AM

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.

<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>";

}
 }

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 27 October 2006 - 08:46 AM

try
<?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 />";
?>


#4 cmccully

cmccully
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 27 October 2006 - 01:07 PM

That's it redarrow, that's exactly what I need. Thanks


cmccully




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users