FallingStar
FallingStar

Reputation: 1

checking checkbox and then input to a new row

How can i insert into the database based on a checkbox result.

I have 5 rooms available and a user can book more then 1.

How do i insert the data to my bookings table for the rooms selected only.

This is what i was trying but it would only work for the 1st room, did try elseif but this didnt work either.

    if(isset($_POST['room1'])) {
        $query = "insert into bookings (bookingNo,roomNo) 
        values ('$bookingNo','1')";                 
        $result = mysql_query($query);
    } 

This is basicly just to input the roomNo and the details for that room, such as date booked and time.

Thanks

Form

            <fieldset>

            <legend>Rooms</legend>
                <ol>
                    <li>
                        <label for =youthCafe>Youth Cafe</label>
                        <input type="checkbox"name="roomid[]" value="1" ><br>
                        <label for =inkwellMain>Inkwell Main</label>
                        <input type="checkbox" name="roomid[]" value="2"><br>
                        <label for =inkwellSmall>Inkwell Small</label>
                        <input type="checkbox"name="roomid[]" value="3"><br>
                        <label for =kitchen>Kitchen</label>
                        <input type="checkbox"name="roomid[]" value="4"><br>
                        <label for =outsideCatering>Outside Catering</label>
                        <input type="checkbox" name="roomid[]" value="5"><br>
                    </li>
                </ol>
            </fieldset>

Upvotes: 0

Views: 56

Answers (4)

rack_nilesh
rack_nilesh

Reputation: 553

Try this code

<form action="index.php" method="post">
<input type="checkbox" name="room[]" value="1" />PHP<br />
<input type="checkbox" name="room[]" value="2" />HTML<br />
<input type="checkbox" name="room[]" value="3" />Java<br />
<input type="checkbox" name="room[]" value="4" />C++<br />
<input type="checkbox" name="room[]" value="5" />C++<br />
<input type="submit" value="submit" />
</form>
if ( isset($_POST['room']) ){
    foreach($_POST['room'] as $value)
    {
        $query = "insert into bookings (bookingNo,roomNo) 
            values ('$bookingNo','$value')";                 
            $result = mysql_query($query);
    }
}

Upvotes: 0

trad
trad

Reputation: 7731

You could use javascript to set an event on the checkbox that calls an ajax function on the server. The function on the server updates the database. This way you separate the database work from the interface work. With JQuery it would look like:

$('#myformelement :checkbox').click(function() {
   $.ajax({
        "url":      (call function using http request)...,
        "type":     "post",
        "data":     { elementid },
        "success":  function(data) {
            //everything ok
        },
        "error": function(data) {
           //error
        }
    });

Upvotes: 0

Jorge Campos
Jorge Campos

Reputation: 23361

You should create first your form. As you did not mentioned it I will post an example:

Important: You should use MYSQLi or PDO functions to avoid sql injection. Aside that this is what you will need. (just the basics)

<?php
if ( isset($_POST['roomid']) ){
    foreach( $_POST['roomid'] as $value ){ //Receive the checkboxes as an array

         //you did not specify where $bookingNo came from, so
         //I'm assuming that you already have it from somewhere.

         $query = "insert into bookings (bookingNo,roomNo) 
                    values ('$bookingNo','$value')"; 

         $result = mysql_query($query);
    }
}
?>

<html>

<body>
    <form name="frm" method="post">
        <input type="checkbox" name="roomid[]" value="1"/>Room 1<br/>
        <input type="checkbox" name="roomid[]" value="2"/>Room 2<br/>
        <input type="checkbox" name="roomid[]" value="3"/>Room 3<br/>
        <input type="submit" name="submit" value="Send"/><br/>
    </form>
</body>

</html>

Upvotes: 2

Marius Behrens
Marius Behrens

Reputation: 159

if room1 is the checkbox. Probably the $_POST['room1'] is set with "on" and not on "true" or "1". Debug $_POST['room1'] first

var_dump($_POST['room1']);

Most times this helps to find out more about type and setted value.

Upvotes: 0

Related Questions