Reputation: 117
I've tried finding an answer for this - and I expect it's going to be very easy to fix.
I have an HTML form with a list of events, each of which has a tick box (name="registrations[]"
and an ID pulled from the database as the value). A customer can tick as many boxes as they want to indicate whether they want to attend.
Alongside each event is a second tick box (name="lunch[]"
), which a customer can use to indicate whether they require lunch at that event.
Now, I'm able to insert multiple events into the database using:
foreach ($_POST['registrations'] as $registration) {
/// do database work
}
(with $registration
used to store the event ID for every booking).
My question is this: how would I go about storing the value of that second checkbox in the database? Thanks, as always, for your help.
Edit - database schema, as requested:
CREATE TABLE IF NOT EXISTS `bookings2` (
`booking_id` int(5) unsigned zerofill NOT NULL AUTO_INCREMENT,
`contact` varchar(150) NOT NULL DEFAULT '',
`company` varchar(150) NOT NULL DEFAULT '',
`service` varchar(50) NOT NULL DEFAULT '',
`telephone` varchar(50) NOT NULL DEFAULT '',
`email` varchar(150) NOT NULL DEFAULT '',
`website` varchar(250) NOT NULL DEFAULT '',
`address` text NOT NULL,
`cars` char(2) DEFAULT NULL,
`size` varchar(10) DEFAULT NULL,
`advertising` varchar(15) NOT NULL DEFAULT '',
`display` char(3) DEFAULT NULL,
`bag` char(3) DEFAULT NULL,
`lunch` char(3) DEFAULT NULL,
`masterclass` char(3) DEFAULT NULL,
`other_info` text,
`promo_code` varchar(50) NOT NULL,
`electric` char(3) DEFAULT NULL,
`event_id` varchar(100) NOT NULL DEFAULT '',
`booking_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`booking_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=104 ;
Upvotes: 2
Views: 267
Reputation: 16086
You can build you form like below:
<form name="abc" method="post">
<input type="checkbox" name="registrations[1]" value="1"> aa
<input type="checkbox" name="lunch[1]" value="1"> aa <br>
<input type="checkbox" name="registrations[2]" value="2"> BB
<input type="checkbox" name="lunch[2]" value="2"> aa <br>
<input type="checkbox" name="registrations[3]" value="3"> CC
aa
On submit you will get values like below:
Array
(
[lunch] => Array
(
[1] => 1
[2] => 2
[3] => 3
)
[registrations] => Array
(
[2] => 2
[3] => 3
)
[submit] => submit
)
Than you can loop you php foreach like-
foreach ($_POST['registrations'] as $key=>$registration) {
//you can access lunch value as
$lunch = isset($_POST['lunch'][$key]) ? $_POST['lunch'][$key] : 0;
}
Upvotes: 0
Reputation: 64526
Set the value of the lunch checkboxes to the event ID - just as you do with the registration checkboxes.
foreach ($_POST['registrations'] as $registration) {
$lunch = in_array($registration, $_POST['lunch']) ? 'Yes' : 'No';
// do the insert with $lunch variable which is now Yes or No
// INSERT INTO bookings2 (...) VALUES (..., '$lunch', ...)
}
This will store your lunch variable as Yes
or No
- this will work but it would be more efficient to store a boolean 1 or 0
.
Upvotes: 1