Jump to content

Recommended Posts

Hi. PHP Newbie here but old guy learning new tricks. lol

 

I'm having a hard time figuring out how to get information from check boxes in to a database and show them on screen.

 

I have 2 sections as below in bold with the checkboxes below each; (the sections will probably increase)

 

Singles

Pop

Rap

Swing

Jazz

 

 

Albums

Pop

Rap

Swing

Jazz

 

1. How do I set up the database to accept this data?

2. Do I create a different name"" for every input checkbox so above there would be 8 different names?

This can't be right. Databases are much cleverer than this.

 

Hope you can help

Link to comment
https://forums.phpfreaks.com/topic/189835-data-from-check-boxes/
Share on other sites

Ok, getting a bit closer.

 

I sectioned off each option like this

FRUIT

<input type="checkbox" name="sec1[]" id="sec1[]" value="apples" /> <label>Apples.</label><br />

<input type="checkbox" name="sec1[]" id="sec1[]" value="oranges" /><label>Oranges.</label><br />

<input type="checkbox" name="sec1[]" id="sec1[]" value="banana" /><label>Banana.</label><br />

 

Vegetables

<input type="checkbox" name="sec2[]" id="sec2[]" value="tomato" /><label>Tomato.</label><br />

<input type="checkbox" name="sec2[]" id="sec2[]" value="beans" /><label>Beans.</label><br />

<input type="checkbox" name="sec2[]" id="sec2[]" value="carrots" /><label>Carrots.</label><br />

 

How do I get this info in to the database? Do I use an IF statement or do I check the items selected.

I'm completely lost here  :shrug:

 

Thanks

 

Would this be correct. The 'sec1other etc are text fields in the form.

 

if(isset($_POST["sec1"])) {$sec1 = $_POST["sec1"];} else {$sec1 = array();}
if(isset($_POST["sec1other"])) {$sec1other = $_POST["sec1other"];}
if(isset($_POST["sec2"])) {$sec2 = $_POST["sec2"];} else {$sec2 = array();}
if(isset($_POST["sec2other"])) {$sec2other = $_POST["sec2other"];}
if(isset($_POST["sec3"])) {$sec3 = $_POST["sec3"];} else {$sec3 = array();}
if(isset($_POST["sec3other"])) {$sec3other = $_POST["sec3other"];}
if(isset($_POST["sec4"])) {$sec4 = $_POST["sec4"];} else {$sec4 = array();}
if(isset($_POST["sec4other"])) {$sec4other = $_POST["sec4other"];}
if(isset($_POST["cantsee"])) {$cantsee = $_POST["cantsee"];}
if(isset($_POST["sec5"])) {$sec5 = $_POST["sec5"];} else {$sec5 = array();}
if(isset($_POST["sec5other"])) {$sec5other = $_POST["sec5other"];}
if(isset($_POST["sec6"])) {$sec6 = $_POST["sec6"];} else {$sec6 = array();}
if(isset($_POST["sec6other"])) {$sec6other = $_POST["sec6other"];}
if(isset($_POST["sec7"])) {$sec7 = $_POST["sec7"];} else {$sec7 = array();}
if(isset($_POST["sec7other"])) {$sec7other = $_POST["sec7other"];}
if(isset($_POST["sec8"])) {$sec8 = $_POST["sec8"];} else {$sec8 = array();}
if(isset($_POST["sec8other"])) {$sec8other = $_POST["sec8other"];}
if(isset($_POST["whendate"])) {$whendate = $_POST["whendate"];}
if(isset($_POST["username"])) {$username = $_POST["username"];}
$date = date('d/m/Y \a\t g:i.s a');

 

Would this be correct.

Only if you want to edit your code every time you change something.

 

One of the points of using a database driven design (for form fields) is that the database holds the definition of the fields. This allows you to use the list of data from the database to dynamically generate the form, validate the expected form fields, and insert the results into a table.

 

Is your question actually about generating the form, determining what data the form will send (what $_POST variables you will use), iterating through the data (edit: you should generally use some kind of loop to process sets of related of data values instead of duplicating code for each value), or defining/inserting the data into a table?

Thanks for your reply.

 

I am trying to get the data from quite a long form in to my database.

I sectioned off each section of the form, hence sec1, sec2 etc.

 

NOW I want to check what has been selected and store it in the database.

 

So later I can selects this information from the database and echo it to the screen

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.