Jump to content

how to store some id numbers


Canman2005

Recommended Posts

hi all

 

i have a php form which contains a query, the query is used to generate a series of tick boxes

 

$sql = "SELECT * FROM extras ORDER BY id ASC";
$show = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($show))
{
?>
<input name="extra" type="checkbox" value="<?php print $row['title']; ?>" />
<?php
}

 

this creates a series of tick boxes.

 

is it possible to somehow group the id numbers of the ones that are ticked, so it would create a variable containing all the id numbers, for example

 

$extrasticked = '12 ,34 ,56 ,76';

 

could this be done?

 

thanks

 

ed

Link to comment
https://forums.phpfreaks.com/topic/78512-how-to-store-some-id-numbers/
Share on other sites

some modification to your code to achieve that

 

 

$sql = "SELECT * FROM extras ORDER BY id ASC";
$show = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($show))
{
?>
<input name="extra[]" type="checkbox" value="<?php print $row['title']; ?>" />
<?php
}

 

 

when the form is posted

$extrasticked = "";
if (isset($_POST['extra'))
{
  $extrasticked = implode(",",$_POST['extra']);
}
echo $extrasticked;

 

hope it helpful notice that in the first html form I have added "[]" to the checkbox

Make the input name an array:

 

<input name="extra[]" type="checkbox" value="<?php print $row['title']; ?>" />

 

Now, when submitted, you will have a variable named $_POST['extra'] or $_GET['extra'] depending on your form method. For an array, I'd recommend POSTing the data.

 

At the receiving end, do:

 

<?php
if ( !empty($_POST['extra']) ) {
   echo "<pre>";
   print_r($_POST['extra']);
   echo "</pre>";
}

 

You will see a printout of all the values that were ticked. You can take it from there.

 

PhREEEk

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.