Jump to content

Recommended Posts

Hi i would like to submit the following code to my database as a mutiple selection box.

when i submit i just get the last selected value for example if i hold ctrl and press 1  2  3  4  i am only receiving 4 on my database. any ideas? thanks in advance for any help!

 

Hours Of Availability:<br>
<select name="availability" multiple="multiple" size="9"> 
<option value="Monday - Period 1">Monday - Period 1</option>
<option value="Monday - Period 2">Monday - Period 2</option>
<option value="Monday - Period 3">Monday - Period 3</option>
<option value="Monday - Period 4">Monday - Period 4</option>
<option value="Monday - Period 5">Monday - Period 5</option>
<option value="Monday - Period 6">Monday - Period 6</option>
<option value="Monday - Period 7">Monday - Period 7</option>
<option value="Monday - Period 8">Monday - Period 8</option>
<option value="Monday - Period 9">Monday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Tuesday - Period 1">Tuesday - Period 1</option>
<option value="Tuesday - Period 2">Tuesday - Period 2</option>
<option value="Tuesday - Period 3">Tuesday - Period 3</option>
<option value="Tuesday - Period 4">Tuesday - Period 4</option>
<option value="Tuesday - Period 5">Tuesday - Period 5</option>
<option value="Tuesday - Period 6">Tuesday - Period 6</option>
<option value="Tuesday - Period 7">Tuesday - Period 7</option>
<option value="Tuesday - Period 8">Tuesday - Period 8</option>
<option value="Tuesday - Period 9">Tuesday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Wednesday - Period 1">Wednesday - Period 1</option>
<option value="Wednesday - Period 2">Wednesday - Period 2</option>
<option value="Wednesday - Period 3">Wednesday - Period 3</option>
<option value="Wednesday - Period 4">Wednesday - Period 4</option>
<option value="Wednesday - Period 5">Wednesday - Period 5</option>
<option value="Wednesday - Period 6">Wednesday - Period 6</option>
<option value="Wednesday - Period 7">Wednesday - Period 7</option>
<option value="Wednesday - Period 8">Wednesday - Period 8</option>
<option value="Wednesday - Period 9">Wednesday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Thursday - Period 1">Thursday - Period 1</option>
<option value="Thursday - Period 2">Thursday - Period 2</option>
<option value="Thursday - Period 3">Thursday - Period 3</option>
<option value="Thursday - Period 4">Thursday - Period 4</option>
<option value="Thursday - Period 5">Thursday - Period 5</option>
<option value="Thursday - Period 6">Thursday - Period 6</option>
<option value="Thursday - Period 7">Thursday - Period 7</option>
<option value="Thursday - Period 8">Thursday - Period 8</option>
<option value="Thursday - Period 9">Thursday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Friday - Period 1">Friday - Period 1</option>
<option value="Friday - Period 2">Friday - Period 2</option>
<option value="Friday - Period 3">Friday - Period 3</option>
<option value="Friday - Period 4">Friday - Period 4</option>
<option value="Friday - Period 5">Friday - Period 5</option>
<option value="Friday - Period 6">Friday - Period 6</option>
<option value="Friday - Period 7">Friday - Period 7</option>
<option value="Friday - Period 8">Friday - Period 8</option>
<option value="Friday - Period 9">Friday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Saturday - Period 1">Saturday - Period 1</option>
<option value="Saturday - Period 2">Saturday - Period 2</option>
<option value="Saturday - Period 3">Saturday - Period 3</option>
<option value="Saturday - Period 4">Saturday - Period 4</option>
<option value="Saturday - Period 5">Saturday - Period 5</option>
<option value="Saturday - Period 6">Saturday - Period 6</option>
<option value="Saturday - Period 7">Saturday - Period 7</option>
<option value="Saturday - Period 8">Saturday - Period 8</option>
<option value="Saturday - Period 9">Saturday - Period 9</option>
</select>
<br>Press + Hold Ctrl For Multiple</p>

Link to comment
https://forums.phpfreaks.com/topic/224574-select-multiple-form-fields-to-sql/
Share on other sites

What php code do you have a the moment for getting the form input?

 

I'm thinking you could just use a while loop to go through all the options and see if they're set or not? (checking $_POST)...

 

But can I see your php code :).

 

Denno

sure.. it sends to insert2.php which contains..

 

<?php
$date = date("F j, Y");
header("Location:tutoradd.php");
$con = mysql_connect("localhost", "123", "123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("a9387329_mrbs", $con);

$sql="INSERT INTO mrbs_tutor (firstname, lastname, dob, address, postcode, telephone, mobile, email, age, instrument, level, instrument2, level2, instrument3, level3, availability, notes, date, crbiss, crbexp)
VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[dob]', '$_POST[address]', '$_POST[postcode]', '$_POST[telephone]', '$_POST[mobile]', '$_POST[email]', '$_POST[age]', '$_POST[instrument]', '$_POST[level]', '$_POST[instrument2]', '$_POST[level2]', '$_POST[instrument3]', '$_POST[level3]', '$_POST[availability]', '$_POST[notes]', '$date', '$_POST[crbiss]', '$_POST[crbexp]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Tutor added";

mysql_close($con)
?>

I guess what you could do to start debugging your program:

 

Echo the post variable for all of the select menus. See what is inside of them. Then further from that, change what you're selecting and then sending to the script (see if it's always the last one picked). This might then be a limitation of the select menu, in which case you might have to switch to checkboxes.. I don't know myself whether there is going to be a limitation on it or not, but it's worth a try.

 

So just start echoing as many variables as you think appropriate to see just what is happening, don't worry about inserting into your database yet, just play with the echoing. Once it's echoing the correct results, then it will insert into the database fine too.

 

Denno

sure, thanks denno:

<html>
<br>
<title>Tutor database</title>
<p><h3>Add Tutor to database</h3>
<form name="Add Tutor" action="insert2.php" method="post">
First Name:<br><input type="text" name="firstname"><p>
Last Name:<br><input type="text" name="lastname"><p>
Date of Birth:<br>
e.g. DD/MM/YYYY<br><input type="text" name="dob"><p>
Address:<br>
<textarea name="address" cols="30" rows="6">
</textarea><p>
Postcode:<br><input type="text" name="postcode"><p>
Telephone:<br> <input type="text" name="telephone" value=""><p>
Mobile:<br> <input type="text" name="mobile" value=""><p>
Email:<br> <input type="text" name="email"><p>
Prefered Age Of Tuition:
<select name="age">
<option value="Child">Child</option>
<option value="Adult">Adult</option>
<option value="No Preference">No Preference</option>
</select><p>
Main Instrument To Teach:
<select name="instrument">
<option value="Guitar">Guitar</option>
<option value="Guitar">Bass</option>
<option value="Piano">Piano</option>
<option value="Singing">Singing</option>
<option value="Drums">Drums</option>
<option value="Banjo">Banjo</option>
<option value="Mandolin">Mandolin</option>
<option value="Ukuele">Ukuele</option>
<option value="Violin">Violin</option>
<option value="Viola">Viola</option>
<option value="Trumpet">Trumpet</option>
<option value="Trombone">Trombone</option>
<option value="Clarinet">Clarinet</option>
<option value="Flute">Flute</option>
<option value="Oboe">Oboe</option>
<option value="Saxophone">Saxophone</option>
<option value="Cello">Cello</option>
<option value="Other">Other</option>
</select> Level:
<select name="level">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Advanced">Advanced</option>
<option value="Specilist">Specilist</option>
</select><P>
Instrument 2:
<select name="instrument2">
<option value="N/A">N/A</option>
<option value="Guitar">Guitar</option>
<option value="Guitar">Bass</option>
<option value="Piano">Piano</option>
<option value="Singing">Singing</option>
<option value="Drums">Drums</option>
<option value="Banjo">Banjo</option>
<option value="Mandolin">Mandolin</option>
<option value="Ukuele">Ukuele</option>
<option value="Violin">Violin</option>
<option value="Viola">Viola</option>
<option value="Trumpet">Trumpet</option>
<option value="Trombone">Trombone</option>
<option value="Clarinet">Clarinet</option>
<option value="Flute">Flute</option>
<option value="Oboe">Oboe</option>
<option value="Saxophone">Saxophone</option>
<option value="Cello">Cello</option>
<option value="Other">Other</option>
</select>
Level:
<select name="level2">
<option value="N/A">N/A</option>
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Advanced">Advanced</option>
<option value="Specilist">Specilist</option>
</select><P>
Instrument 3:
<select name="instrument3">
<option value="N/A">N/A</option>
<option value="Guitar">Guitar</option>
<option value="Guitar">Bass</option>
<option value="Piano">Piano</option>
<option value="Singing">Singing</option>
<option value="Drums">Drums</option>
<option value="Banjo">Banjo</option>
<option value="Mandolin">Mandolin</option>
<option value="Ukuele">Ukuele</option>
<option value="Violin">Violin</option>
<option value="Viola">Viola</option>
<option value="Trumpet">Trumpet</option>
<option value="Trombone">Trombone</option>
<option value="Clarinet">Clarinet</option>
<option value="Flute">Flute</option>
<option value="Oboe">Oboe</option>
<option value="Saxophone">Saxophone</option>
<option value="Cello">Cello</option>
<option value="Other">Other</option>
</select>
Level:
<select name="level3">
<option value="N/A">N/A</option>
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Advanced">Advanced</option>
<option value="Specilist">Specilist</option>
</select><P>
Hours Of Availability:<br>
<select name="availability" multiple="multiple" size="9"> 
<option value="Monday - Period 1">Monday - Period 1</option>
<option value="Monday - Period 2">Monday - Period 2</option>
<option value="Monday - Period 3">Monday - Period 3</option>
<option value="Monday - Period 4">Monday - Period 4</option>
<option value="Monday - Period 5">Monday - Period 5</option>
<option value="Monday - Period 6">Monday - Period 6</option>
<option value="Monday - Period 7">Monday - Period 7</option>
<option value="Monday - Period 8">Monday - Period 8</option>
<option value="Monday - Period 9">Monday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Tuesday - Period 1">Tuesday - Period 1</option>
<option value="Tuesday - Period 2">Tuesday - Period 2</option>
<option value="Tuesday - Period 3">Tuesday - Period 3</option>
<option value="Tuesday - Period 4">Tuesday - Period 4</option>
<option value="Tuesday - Period 5">Tuesday - Period 5</option>
<option value="Tuesday - Period 6">Tuesday - Period 6</option>
<option value="Tuesday - Period 7">Tuesday - Period 7</option>
<option value="Tuesday - Period 8">Tuesday - Period 8</option>
<option value="Tuesday - Period 9">Tuesday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Wednesday - Period 1">Wednesday - Period 1</option>
<option value="Wednesday - Period 2">Wednesday - Period 2</option>
<option value="Wednesday - Period 3">Wednesday - Period 3</option>
<option value="Wednesday - Period 4">Wednesday - Period 4</option>
<option value="Wednesday - Period 5">Wednesday - Period 5</option>
<option value="Wednesday - Period 6">Wednesday - Period 6</option>
<option value="Wednesday - Period 7">Wednesday - Period 7</option>
<option value="Wednesday - Period 8">Wednesday - Period 8</option>
<option value="Wednesday - Period 9">Wednesday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Thursday - Period 1">Thursday - Period 1</option>
<option value="Thursday - Period 2">Thursday - Period 2</option>
<option value="Thursday - Period 3">Thursday - Period 3</option>
<option value="Thursday - Period 4">Thursday - Period 4</option>
<option value="Thursday - Period 5">Thursday - Period 5</option>
<option value="Thursday - Period 6">Thursday - Period 6</option>
<option value="Thursday - Period 7">Thursday - Period 7</option>
<option value="Thursday - Period 8">Thursday - Period 8</option>
<option value="Thursday - Period 9">Thursday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Friday - Period 1">Friday - Period 1</option>
<option value="Friday - Period 2">Friday - Period 2</option>
<option value="Friday - Period 3">Friday - Period 3</option>
<option value="Friday - Period 4">Friday - Period 4</option>
<option value="Friday - Period 5">Friday - Period 5</option>
<option value="Friday - Period 6">Friday - Period 6</option>
<option value="Friday - Period 7">Friday - Period 7</option>
<option value="Friday - Period 8">Friday - Period 8</option>
<option value="Friday - Period 9">Friday - Period 9</option>
</select>
<select name="availability" multiple="multiple" size="9"> 
<option value="Saturday - Period 1">Saturday - Period 1</option>
<option value="Saturday - Period 2">Saturday - Period 2</option>
<option value="Saturday - Period 3">Saturday - Period 3</option>
<option value="Saturday - Period 4">Saturday - Period 4</option>
<option value="Saturday - Period 5">Saturday - Period 5</option>
<option value="Saturday - Period 6">Saturday - Period 6</option>
<option value="Saturday - Period 7">Saturday - Period 7</option>
<option value="Saturday - Period 8">Saturday - Period 8</option>
<option value="Saturday - Period 9">Saturday - Period 9</option>
</select>
<br>Press + Hold Ctrl For Multiple</p>
Notes:<br>
<textarea name="notes" cols="70" rows="6">N/A
</textarea><p>
<input type="hidden" name="date">
Date CRB Issued:<br><input type="text" name="crbiss" size=20> 
Date CRB Expired:<br><input type="text" name="crbexp" size=20> 
<input type="submit" value="Add"/>
</form>
</html> 

there is also another thing i could do with if you wouldnt mind while testing..

i need the paid field to submit font color="green"  and if the client specifies no for it to show up font color="red"

would this be possible all i could think of would be something like:

if submit value="no" font color="red"

else font color="green"

i dont know if this would work and i dont know what the php coding would be for it...

cheers :D

Playing with the code, it does appear that only the last option selected will be sent through POST, as you said. I can't see anyway to be able to loop through the options selected, unless of course you use some javascript to see which options are selected, and then use javascript to POST those selections to the script..

 

I would suggest you swap over to using checkboxes instead.. It would make your form much more understandable and easier to fill out too I would imagine (no keyboard combos are necessary)

 

Denno

Your second question, did you want that to happen in real time? So as soon as the field is answered, it changes colour? This is done very simply using javascript.. Otherwise, I'm not sure what font is to be made green/red. I can't see any 'paid' fields in the form in the code you posted..

 

Denno

ok first lot, is the checkbox guaranteed to submit all ?

and second off.. sorry i forgot to add it in..

Paid Until:<br><input type="text" name="paid" size=20> 

 

that would be the paid field.. so if they submit a date then it stays green but if they submit no it submit red.

i dont know if this is possible...

cheers denno...

Give each checkbox a different name. Append a number to the end of the name, and then you will have the status of every checkbox sent to the script.

 

I still don't understand where the colours are going to change? Will be it before the form is submitted (before the add button is pressed).

Will it be as soon as the focus shifts away from the paid field?

Or does it happen in a results type page that shows the user what they have entered?

 

Denno

I believe this was what you were looking for. To send multiples, the form fields have to be set up as arrays. Look at how the name= attributes for the <select> fields are set up now. To understand how this data is sent in the $_POST array, print it to the screen after the form is submitted with the following, and you'll see that it all ends up in a multidimensional array within $_POST['availability']. There's no need for any javascript; this is basic HTML markup.

 

echo '<pre>';
print_r($_POST);
echo '</pre>';

Oh, for crying out loud. The code I wanted to paste wasn't even copied to the clipboard. Here it is, LOL.

 

Hours Of Availability:<br>
<select name="availability[monday][]" multiple="multiple" size="9">
<option value="Monday - Period 1">Monday - Period 1</option>
<option value="Monday - Period 2">Monday - Period 2</option>
<option value="Monday - Period 3">Monday - Period 3</option>
<option value="Monday - Period 4">Monday - Period 4</option>
<option value="Monday - Period 5">Monday - Period 5</option>
<option value="Monday - Period 6">Monday - Period 6</option>
<option value="Monday - Period 7">Monday - Period 7</option>
<option value="Monday - Period 8">Monday - Period 8</option>
<option value="Monday - Period 9">Monday - Period 9</option>
</select>
<select name="availability[tuesday][]" multiple="multiple" size="9">
<option value="Tuesday - Period 1">Tuesday - Period 1</option>
<option value="Tuesday - Period 2">Tuesday - Period 2</option>
<option value="Tuesday - Period 3">Tuesday - Period 3</option>
<option value="Tuesday - Period 4">Tuesday - Period 4</option>
<option value="Tuesday - Period 5">Tuesday - Period 5</option>
<option value="Tuesday - Period 6">Tuesday - Period 6</option>
<option value="Tuesday - Period 7">Tuesday - Period 7</option>
<option value="Tuesday - Period 8">Tuesday - Period 8</option>
<option value="Tuesday - Period 9">Tuesday - Period 9</option>
</select>
<select name="availability[wednesday][]" multiple="multiple" size="9">
<option value="Wednesday - Period 1">Wednesday - Period 1</option>
<option value="Wednesday - Period 2">Wednesday - Period 2</option>
<option value="Wednesday - Period 3">Wednesday - Period 3</option>
<option value="Wednesday - Period 4">Wednesday - Period 4</option>
<option value="Wednesday - Period 5">Wednesday - Period 5</option>
<option value="Wednesday - Period 6">Wednesday - Period 6</option>
<option value="Wednesday - Period 7">Wednesday - Period 7</option>
<option value="Wednesday - Period 8">Wednesday - Period 8</option>
<option value="Wednesday - Period 9">Wednesday - Period 9</option>
</select>
<select name="availability[thursday][]" multiple="multiple" size="9">
<option value="Thursday - Period 1">Thursday - Period 1</option>
<option value="Thursday - Period 2">Thursday - Period 2</option>
<option value="Thursday - Period 3">Thursday - Period 3</option>
<option value="Thursday - Period 4">Thursday - Period 4</option>
<option value="Thursday - Period 5">Thursday - Period 5</option>
<option value="Thursday - Period 6">Thursday - Period 6</option>
<option value="Thursday - Period 7">Thursday - Period 7</option>
<option value="Thursday - Period 8">Thursday - Period 8</option>
<option value="Thursday - Period 9">Thursday - Period 9</option>
</select>
<select name="availability[friday][]" multiple="multiple" size="9">
<option value="Friday - Period 1">Friday - Period 1</option>
<option value="Friday - Period 2">Friday - Period 2</option>
<option value="Friday - Period 3">Friday - Period 3</option>
<option value="Friday - Period 4">Friday - Period 4</option>
<option value="Friday - Period 5">Friday - Period 5</option>
<option value="Friday - Period 6">Friday - Period 6</option>
<option value="Friday - Period 7">Friday - Period 7</option>
<option value="Friday - Period 8">Friday - Period 8</option>
<option value="Friday - Period 9">Friday - Period 9</option>
</select>
<select name="availability[saturday][]" multiple="multiple" size="9">
<option value="Saturday - Period 1">Saturday - Period 1</option>
<option value="Saturday - Period 2">Saturday - Period 2</option>
<option value="Saturday - Period 3">Saturday - Period 3</option>
<option value="Saturday - Period 4">Saturday - Period 4</option>
<option value="Saturday - Period 5">Saturday - Period 5</option>
<option value="Saturday - Period 6">Saturday - Period 6</option>
<option value="Saturday - Period 7">Saturday - Period 7</option>
<option value="Saturday - Period 8">Saturday - Period 8</option>
<option value="Saturday - Period 9">Saturday - Period 9</option>
</select>
<br>Press + Hold Ctrl For Multiple</p>

 

There, that's better. Now you can use the code in the previous post to see the structure of the array . . .  :)

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.