Jump to content

Form variables with variable names


cmccully

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
Link to comment
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]
Link to comment
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]
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.