473,473 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple Selection POST forms

jimleon
74 New Member
I have a post form for adding news articals to our website. It sends the headline and content and which category it belongs to to our MySQL database. This all works fine if a single category is selected. However, if i select more than one category it will only post the last one selected, hence if i select category 2, 4 and 5, it will only post one submission related to category 5. How do I set it to have either 3 submissions (ie cat 2,4 & 5) to the db or change something at the database end to handle receiving multiple selections.
Expand|Select|Wrap|Line Numbers
  1. <td>
  2. <select name=\"cat_id\" size=\"4\" multiple>
  3. <option value=\"2\">Category1</option>
  4. <option value=\"3\">Category2</option>
  5. <option value=\"4\">Category3</option>
  6. </select>
  7. </td>
Thank in advance
Aug 16 '07 #1
5 1850
pbmods
5,821 Recognized Expert Expert
Heya, Jim.

You posted this in the Articles section. I'll go ahead and move it to the Forum where an Expert will be more likely to find it.
Aug 16 '07 #2
ak1dnar
1,584 Recognized Expert Top Contributor
I have a post form for adding news articals to our website. It sends the headline and content and which category it belongs to to our MySQL database. This all works fine if a single category is selected. However, if i select more than one category it will only post the last one selected, hence if i select category 2, 4 and 5, it will only post one submission related to category 5. How do I set it to have either 3 submissions (ie cat 2,4 & 5) to the db or change something at the database end to handle receiving multiple selections.

<td>
<select name=\"cat_id\" size=\"4\" multiple>
<option value=\"2\">Category1</option>
<option value=\"3\">Category2</option>
<option value=\"4\">Category3</option>
</select>
</td>

Thank in advance
If I jumped to the correct track. This might do the initial job for you.

Form

Expand|Select|Wrap|Line Numbers
  1. <form action="server.php" method="post" name="myform">
  2. <select name="cat[]" multiple>
  3.   <option value="Cat1">Cat1
  4.   <option value="Cat2">Cat2
  5.   <option value="Cat3">Cat3
  6.  
  7. </select>
  8. <input name="" type="submit" />
  9. </form>
server.php

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $selected_cats = $_POST['cat'];
  3. print_r($selected_cats);// This is How it looks like 
  4. echo '<br>';
  5. // loop through array write each instance to db
  6. while (list($key, $val) = each ($selected_cats)) {
  7. $insertSQL = "INSERT INTO yourtable (yourfield) VALUES ({$val})";
  8. echo $insertSQL.'<br>';
  9. } // end loop
  10. ?>
P.S. Since you have used <select name=\"cat_id\" size=\"4\" multiple> "escape" like this format, just imagined you are with a Php Backend, Just throw it away if not.
Aug 16 '07 #3
Vinnie123
13 New Member
ajaxrand is correct. The part you were probably missing was naming the select ctrl as "cat[]" instead of "cat". Naming it simply "cat" will cause it to only send the last selection rather than all selections.
Aug 16 '07 #4
ak1dnar
1,584 Recognized Expert Top Contributor
ajaxrand is correct. The part you were probably missing was naming the select ctrl as "cat[]" instead of "cat". Naming it simply "cat" will cause it to only send the last selection rather than all selections.
Indeed ! And Its Not Only for ListMenus. you can Use the same Tech with Other HTML Form Elements too.
By Putting "[ ]" to the Form Element you are making a array.

Example

Expand|Select|Wrap|Line Numbers
  1. <input name="element_name[]" type="text" value="1001"/>
  2. <input name="element_name[]" type="text" value="1002"/>
  3. <input name="element_name[]" type="text" value="1003"/>
Aug 17 '07 #5
pbmods
5,821 Recognized Expert Expert
While we're having fun with this, did you know you can create multidimensional arrays this way as well?

Check this out:
Expand|Select|Wrap|Line Numbers
  1. <input name="form[username]" value="James" />
  2. <input name="form[cookies][]" value="Chocolate Chip" />
  3. <input name="form[cookies][]" value="Oatmeal Raisin" />
  4. <input name="form[cookies][]" value="White Chocolate Macadamia" />
  5.  
When the form is submitted, here's what $_REQUEST looks like:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [form] => Array
  4.         (
  5.             [username] => James
  6.             [cookies] => Array
  7.                 (
  8.                     [0] => Chocolate Chip
  9.                     [1] => Oatmeal Raisin
  10.                     [2] => White Chocolate Macadamia
  11.                 )
  12.         )
  13. )
  14.  
Aug 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: jing_li | last post by:
Hi, you all, I am a newbee for php and I need your help. One of my coworker and I are both developing a webpage for our project using php. We have a copy of the same files in different location...
1
by: NiLaY | last post by:
I have a function like the one below to move a selected option from one listbox to another. But I want to be able to select multiple and be able to move those selections to the other listbox. Can...
15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
3
by: svdh2 | last post by:
I have been looking at a problem the entire week in access, I have still not been able to find a solution. Hope that you could maybe tell where to look Concerns the link between Access and Word....
2
by: Dolorous Edd | last post by:
Hi, for a program I'm working on I need to be able to drag multiple files between Windows Explorer and a ListBox, in both directions. Implementing the "drag in" was pretty easy, but I can't find...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
0
jimleon
by: jimleon | last post by:
I have a post form for adding news articals to our website. It sends the headline and content and which category it belongs to to our MySQL database. This all works fine if a single category is...
1
by: kirkus84 | last post by:
I am currently trying to do a multiple record mail merge through a query via a command button on a form. The query basically displays customers who have said yes to privacy. The user inputs a date...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.