Jump to content

[SOLVED] checkboxes


spillage

Recommended Posts

Hi,

 

I have a html form with four checkboxes. Trying to get it so any boxes ticked will be sent to db. Do I have to have a single variable for each. This is the html checkbox:

<form action="http://localhost/mail01.php" method="post">
HP:
<input type="checkbox" name="printer" value="hp">
Epson: 
<input type="checkbox" name="printer" value="epson">
Lexmark: 
<input type="checkbox" name="printer" value="lex">
Dell: 
<input type="checkbox" name="printer" value="dell">
</form>

Thought I needed a switch statement but stupidly realised this only holds the last box chosen. Could I change it to:

<form action="http://localhost/mail01.php" method="post">
HP:
<input type="checkbox" name="printer" value="$hp">
Epson: 
<input type="checkbox" name="printer" value="$epson">
Lexmark: 
<input type="checkbox" name="printer" value="$lex">
Dell: 
<input type="checkbox" name="printer" value="$dell">
</form>

$choice=$_POST['$hp','$epson','$lex','$dell'];

 

Cheers,

 

Spill

Link to comment
Share on other sites

change the name to printer[]. this will store the data in an array

<form action="http://localhost/mail01.php" method="post">
HP:
<input type="checkbox" name="printer[]" value="hp">
Epson: 
<input type="checkbox" name="printer[]" value="epson">
Lexmark: 
<input type="checkbox" name="printer[]" value="lex">
Dell: 
<input type="checkbox" name="printer[]" value="dell">
</form>

 

Now you can get the data from $_POST['printer']

print_r($_POST['printer'])

 

Ray

Link to comment
Share on other sites

use an array:

<form action="http://localhost/mail01.php" method="post">
HP:
<input type="checkbox" name="printer[]" value="hp">
Epson: 
<input type="checkbox" name="printer[]" value="epson">
Lexmark: 
<input type="checkbox" name="printer[]" value="lex">
Dell: 
<input type="checkbox" name="printer[]" value="dell">
</form>

 

That will create an array such as:

$_POST['printer'] = array('hp', 'lex', dell');

meaning that they want all but the epson.

 

 

EDIT: beaten to it :P

Link to comment
Share on other sites

Just a quick question.

 

would I use somthing like

 

$cust_pri = (array($_POST['printer']))

and

mysql_query("INSERT INTO submit

VALUES ('$id', '$cust.', '$friend', '$time','$cust_pri')");

 

or would this just print array.

 

Thanks

Link to comment
Share on other sites

Maybe I should also add that there is also 2 email address to be posted aswell.

 

I have tried

 

foreach ($_POST['printer'] as $row=>$pri){

 

$cust_pri=$pri;

}

but only the last checkboxes data is entered into the db. should it not print both.

 

thanks

 

 

Link to comment
Share on other sites

<?php
$cust_pri = (array($_POST['printer']))

mysql_query("INSERT INTO `submit` VALUES ('$id', '$cust.', '$friend', '$time', '" . implode(';', $cust_pri) . "')");
?>

 

That will insert them into the database, in the same column. Each printer name will be seperated by an ;

Link to comment
Share on other sites

ah sorry. Probably because you're setting $cust_pri as an array, when you doen't need to because $_POST['printer'] is one. try this:

<?php
$cust_pri = $_POST['printer'];

mysql_query("INSERT INTO `submit` VALUES ('$id', '$cust.', '$friend', '$time', '" . implode(';', $cust_pri) . "')");
?>

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.