0

I have run into a problem... I have a pair of checkboxes that I have named and numbered. I need to get the checkboxes marked and insert them into my database. Here is how I am creating the checkboxes:

<div class="check"><input type="checkbox" name="acharge1" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities1" value="1">Amenity 1</div>
<div class="check"><input type="checkbox" name="acharge2" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities2" value="1">Amenity 2</div>
<div class="check"><input type="checkbox" name="acharge3" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities3" value="1">Amenity 3</div>
<div class="check"><input type="checkbox" name="acharge4" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities4" value="1">Amenity 4</div>

Some of these will be checked and some wont. Also they will not always be checked in pairs, but I have to upload them in pairs to my database. So in other words: acharge1 and amenities1 will not be checked but acharge2 and amenities2 will be. So I tried a foreach loop and that did not work. I also tried a while loop, but I did not know what to check. So this is what I have now:

$aa = 1;
    echo "Amenity: ".$_POST['amenities'.$aa];
    $aa++;
    while(isset($_POST['amenities'.$aa])){
        $amen = $_POST['amenities'.$aa];
        if(isset($_POST['acharge'.$aa])){
        $acharge = $_POST['acharge'.$aa];
        }else{
            $acharge = "0";
            }
        echo "Amen: ". $amen." charge: ".$acharge;  
        mysql_query("INSERT INTO hotel_amenities (amenity_code, hotel_id, charge) VALUES ('$amen','$hotel_id','$acharge')") or die(mysql_error());
        echo $aa;
        $aa++;
        }

But as you can see this does not work, because if amenities1 is not checked it will not do the while loop through the others. Anyway ANY help on how I can achieve this would be greatly appreciated!

EDIT** This is the concept, so as you can see that sometimes they money box will be checked with the amenity and sometimes it will not. http://www.cancunelitetravel.com/ale/Capture.PNG

1
  • They have to be inserted in the same row in mysql. Commented Oct 20, 2012 at 2:26

1 Answer 1

3

The way you are going about this is entirely wrong. If you are unsure of the number of options available, you should write it like this:

$cnt = 0;
foeach ($_POST as $option => $value) {
    if (preg_match("/(?:acharge|amenities)(\\d+)/", $option, $matches)) {
        $cnt = max($cnt, intval($matches[1]));
    }
}
for ($i = 0; $i < $cnt; $i ++) {
    $amenity = $_POST['amenities' . $i];
    $value = "0";
    if (array_key_exists("acharge" . $i, $_POST)) {
        $value = "1";
    }
    mysql_query("INSERT INTO hotel_amenities (amenity_code, hotel_id, charge) VALUES ('$amenity','$hotel_id','$value')") or die(mysql_error());
}

I think this would do it for you. That is assuming you have established the connection previously and taken care of the value for $hotel_id.

Sign up to request clarification or add additional context in comments.

8 Comments

this is not exactly right, $amen was the other input checkbox, that I need to get. They are two. One is for a hotel amenity and the other is a checkbox right next to it, that implies if it will be charged extra or not.
I added an image to gt a better idea. Thanks so much!
I tried this and get an error: Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\olympus-backend\inc\add-hotel.php on line 79
Sorry, forgot to box the regular expression. Will edit the answer to a better one now.
By the way, this will insert the same value for amenity_code, seeing that the value attribute of the aminitiesxx in your HTML is always 1. If that is not correct, you should change it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.