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

Link to comment
Share on other sites

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

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.