Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:56:24+00:00 2026-06-13T05:56:24+00:00

I have run into a problem… I have a pair of checkboxes that I

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:56:26+00:00Added an answer on June 13, 2026 at 5:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just installed VS2008 and have run into a problem that I'm sure can
I have run into a problem where I need to remove certain items from
Frequently I have run into a problem when installing gems that provides a problem
I'm trying to get familiar with CI and I have run into a problem
I have run into a problem that is a little irritating. Here is my
I have run into a problem while writing my server program. I need to
I have run into this problem again and now really need to know how
I have run into a problem that may interest many (beginners). Let me show
I have run into this problem... I have two macros that look very similar
I have run into a problem: I have a page that will be processed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.