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
https://forums.phpfreaks.com/topic/103760-solved-checkboxes/
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
https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531245
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
https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531246
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
https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531318
Share on other sites

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.