Jump to content

Selecting multiple checkboxes


Andrew R

Recommended Posts

Hi

I am having trouble writing a script that will select multiple flight ids (from a checkbox) and process them into another page via the $_POST function

So for example I select flight id (RFS1869 and RFS5697 from the checkbox) and it will display information about these two results in another page.

Can anybody point me in to any tutorials or anything that will help with learn how to do this.

Cheers
Link to comment
https://forums.phpfreaks.com/topic/23578-selecting-multiple-checkboxes/
Share on other sites

in your form:
[code]
<form action='target.php' method='post'>
  <input type = 'checkbox' name = 'somevar[]' value = 'rfs1869'> rfs1869 <br>
  <input type = 'checkbox' name = 'somevar[]' value = 'rfs5697'> rfs5697 <br>
  <input type = 'submit' value='submit'>
</form>
[/code]

target.php
[code]
$somevar = $_POST['somevar'];

// example
foreach ($somevar as $val) {
  echo "$val <br>"; // echo out each element in $somevar array
}

//another example
echo $somevar[0]; //echos first checkbox that was checked
echo $somevar[1]; //echos 2nd checkbox that was checked
[/code]
If you want to get the values for the checkboxes from the database, then you can do it like this: [code]while($row = mysql_fetch_assoc($query_result)
{
echo "\t<label><input type='checkbox' name='flight_ids[]' value='{$row['value']}' /> {$row['user_readable_name']}</label>\n";
}[/code]

Archived

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

×
×
  • 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.