By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,583 Members | 617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,583 IT Pros & Developers. It's quick & easy.

Multiple select box population

P: 10
So i have this portion of code

Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="insert_disease_has_symptom_db.php" onsubmit="multipleSelectOnSubmit()">
  2. <select multiple name="fromBox[]" id="fromBox">
  3.     <option value="1">Sintoma 1</option>
  4.     <option value="2">Sintoma 2</option>
  5.     <option value="3">Sintoma 3</option>
  6.     <option value="4">Sintoma 4</option>
  7.     <option value="5">Sintoma 5</option>
  8.     <option value="6">Sintoma 6</option>
  9.     <option value="7">Sintoma 7</option>
  10.     <option value="8">Sintoma 8</option>
  11. </select>
  12. <select multiple name="toBox[]" id="toBox">
  13. </select>
  14. <input type="submit" value="OK">
I am to populate my option values with names on a table in a sql, storing afterwards the ids of the selected names that i passed to the right. Any ideas, because im stuck :\
Nov 16 '08 #1
Share this Question
Share on Google+
23 Replies


Markus
Expert 5K+
P: 6,050
I don't understand. Can you explain a little more?

Also, you'll notice I've formatted you code. Please, when posting code, wrap it in [code] tags. Just higlight the code, and hit the '#' button at the top of this editor.

Moderator.
Nov 16 '08 #2

pbmods
Expert 5K+
P: 5,821
Heya, toshibata.

Try print_r()'ing $_POST after you submit the form, and you'll see what data has been input.
Nov 16 '08 #3

P: 10
Im trying to populate those option values with info stored on my database, but since this is the first time im doing a multiple select box where you have to select from the left box (which will contain all attributes) the attributes you which tou chose passing to the right box.

After Submiting it should store on a different table the id of the attributes i selected.

Managed to get this working on a scroll down type thing, but that only allows me to chose one attribute at a time, so im stuck :\

Any help would be most appreciated
Nov 25 '08 #4

dlite922
Expert 100+
P: 1,584
I'm still a little lost.

Explain it more in the means of the application, what is this page support to help the user do.

From the what it looks like you're trying to have the user select one (or more) symptoms from the first drop down or multi-select box, based on their choices you want to display the diseases that cause these symptom and have the user select one (or more?) of these diseases or illnesses?

Let me know if this is what you want to do.




Dan
Nov 25 '08 #5

P: 10
No, i wish to populate with symptoms which are on my database, selecting the ones that are on that specific disease, saving after that the ID of those symptoms in the database.

Hope i made myself clear
Nov 26 '08 #6

dlite922
Expert 100+
P: 1,584
@toshibata
Well, that was assumed. I'm not actually asking you to have the symptom box hard-coded, it would be pulled from the database (list of all symptoms).

I don't know what you mean by "saving after that the ID of those symptoms in the database." It's not a complete sentence.

Is this for a doctor that assigns the symptoms to a disease or for visitors who wish to find out with disease is causing their symptom by indicating those symptoms on the site?

Also: I've seen a similar application on web MD. The list of symptoms are too large for a single drop down. They at least need to be categorized.

Here's an example at WebMD:

Symptom Checker

I'm trying to find out what your trying to do so I can help you achieve it.


Dan
Nov 26 '08 #7

P: 10
Ill try to explain in more detail and as well as i can.

I have a database for a clinic. And the objective is to make mini-appications in PHP to insert data. One of those insertions ( The one im having problems with ) is to insert diseases, making a form asking for its details, inserting it after in the database. After that disease has been inserted, i have to use a multiple select box in which on the left part will be all the symptoms i have stored on the symptoms table on my database. From which ill choose the symptons present on the disease i inserted into the database on the previous step, moving them to the right box and clickling submit. This will then store in a table name disease_has_symptom a tuple of the id of the disease and the symptom.

Tried to be as clear as possible, hope you understand the issue as this is burning my brain :>

Might aswell post an image of the tables of the database maybe will make my point more clear.

http://img151.imageshack.us/img151/3992/naturalmc0.png
Nov 26 '08 #8

P: 7
I don't see, given the code provided, how users move the selected symptoms from the complete list (fromBox[]) to the short list on the right (toBox[]). If, on submit, nothing has been added to the toBox (and subsequently selected), then on the post your toBox[] field will be empty.

Presumably you could do this with just the one multiple select box, so perhaps it's a matter of design/taste to use the "all available -> selected" model. If you have just the one fromBox, the selected items should come through on the POST and you'll have your list.

If you do want the two boxes, you probably need either some drag and drop functionality, or at least a button to move items from the left to the right (and back again) and then, before submitting, make sure to select all the items in the right box, because just having the options won't help unless they're selected.

Of course, maybe you did all that and just didn't put it in your code sample. In which case I probably didn't help with the brain burning thing.
Nov 26 '08 #9

dlite922
Expert 100+
P: 1,584
Ok here's what I understand. You need to give the user a way to add a disease to the disease table and then have them select the symptoms (from the list you already have in the database) that apply to that disease. Then you save this link in the table you specified. the relationship of the disease and symptom table is a many to many relationship, ie. one or more disease match up to one or more symptoms, obviously.

here's what I would do. You don't need to give the user two pages. It can all be done in one. Give the user the form fields for the name of the disease, its description, etc and right below it, display a multi-select box with all your symptoms where they can select multiple symptoms. Once they hit submit, You'll grab the data, create the disease (after appropriate validations) and insert it into the database. Once you insert it, you can call the mysql_insert_id() and be presented with ID (or primary key value) of that disease.

You then create an entry in the disease_has_symptom table by inserting all the symptoms that disease has. (I'm assuming the multi-select box above will have the symptom ID (primary key) as the value, and the name as the label.

Here's how you populate the symptom mult-select box:
Populate Select Box - PHP and MySQL | Tech-Evangelist

Let's go back to your original question. Which part of this page you don't know how to do?
Nov 26 '08 #10

P: 10
The problem is i might have to select multiple symptoms, thats why i was going through the 2 boxs route, passing the ones associated to that disease to the right and then saving to database.That code only allows me to get 1
Nov 26 '08 #11

P: 7
The only thing you should have to add to the Tech-Evangelist example would be the "multiple" designation in the select tag - then you would be able to select more than one from that form element.
Nov 26 '08 #12

dlite922
Expert 100+
P: 1,584
@toshibata
oh no, that's now true. Here's the code to make a select box that gives you an array of those selected when you grab it with $_POST()

Expand|Select|Wrap|Line Numbers
  1. <select name="symptoms[]" multiple="multiple" size="5">
  2. <option value="1">Pain</option>
  3. <option value="2">Ache</option>
  4. <option value="3">Rash</option>
  5. <option value="4">Cough</option>
  6. <option value="5">Ache</option>
  7. <option value="6">Rash</option>
  8. <option value="7">Cough</option>
  9. </select>
  10.  
notice the name has [] attached to it (symptoms[])

You can do this if you have multiple form elements with the same name and you want to grab them on the PHP side as an array. Remeber however, when you do your $_POST['symptoms'], you don't need to add the brackets to the name.

An example with code: PHP Tutorial-Multi-Select

Good luck!


Dan
Nov 26 '08 #13

P: 10
Still cant see how i would replace those Rash, etc you write there with my own stuff from the database :\
Nov 26 '08 #14

P: 7
Well, let's back up a second... Your original code has a complete multiple-select box named fromBox[]. I don't think that's the problem. The problem seems to be that you have another multi-select box (toBox[]) where you are expecting to see the results, but you have no way of moving symptoms from the fromBox[] to the toBox[]. When you submit that form nothing will ever be in the toBox[].
Nov 26 '08 #15

P: 10
No no, i have full code with all the buttons to pass info from left to right. The problem is the populating the left one with the symptoms on my database
Nov 26 '08 #16

P: 7
Ah, well then the code from the Tech-evangelist article posted by dlite a few posts ago should do the trick. The HTML structure of your "fromBox[]" is sound, you just need to query the database and, rather than writing out all the option tags, loop over the query results (something like "select * from symptoms") to create an option tag for each symptom with the appropriate id and value.
Nov 26 '08 #17

dlite922
Expert 100+
P: 1,584
@toshibata
This is from the link I posted earlier: It assumes you've already made a mysql connection.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <select name="symptomID">
  3. <?php
  4. $sql = "SELECT id, name FROM symptom".
  5. "ORDER BY name";
  6.  
  7. $rs = mysql_query($sql);
  8.  
  9. while($row = mysql_fetch_array($rs))
  10. {
  11.   echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n  ";
  12. }
  13. ?>
  14. </select>
  15.  
  16.  
  17.  
Although if you know PHP well, you wouldn't do it this way. I would use scripts in advance that get the data, perhaps a framework, and maybe even a templating system like Smarty that will generate the options for me without doing a loop.
Nov 26 '08 #18

P: 10
it worked finally, many thanks dude

Although i know have a new problem which is, its suppose to save the id of the disease and symptom on the disease_has_symptom php.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //loads the configuration file
  4. require_once('inc/config.php');
  5.  
  6.   $sql="INSERT INTO disease_has_symptom (disease_id, symptom_id) VALUES (??????)";
  7.  
  8.   if (!mysql_query($sql,$con))
  9.   {
  10.   die('Error: ' . mysql_error());
  11.   }
  12.  
  13. mysql_close($con)
  14. ?>

How shall i fetch the ids of the selected stuff to put where the ???? are
Nov 26 '08 #19

P: 7
Both the disease ID and symptom IDs should be POSTed from the form on the previous page. The diseaseID would likely have been in a hidden field having been created on the first page. The symptomIDs should be the values of your multiple select toBox[] field. You will need to insert a separate set of values for each symptom ID (though the disease ID should always be the same).
Nov 26 '08 #20

P: 10
Im not being able to get the id from disease, should i put disease page on the same page as the multiple select box for symptoms of that disease to be able to get the id ?
Nov 26 '08 #21

P: 7
From your description of the flow in post #8 in this thread, you have a first page where the user enters a name (and possibly other info) for a new disease to add to the database. When the user submits that form, you insert a new disease. When you do so, you should be sure to find out what ID was assigned to that new disease, then include this ID in the same form as the one used to identify the symptoms. You can use the PHP function mysql_insert_id() to get the ID immediately after your insert statement has executed.

Someone earlier suggested combining these two pages into one and that is certainly possible. In fact, since the selection of symptoms is not dependent on any info entered for the new disease, you might save some page views/steps in this process by doing so. That's mostly a matter of usability and design... the steps you need to go through to get a new disease and its symptoms into a database are essentially the same either way.
Nov 26 '08 #22

P: 10
Thanks for all the suggestions,

Im having this issue when trying to insert the ids on table.

Error: Cannot add or update a child row: a foreign key constraint fails (`db/remedy_has_plant`, CONSTRAINT `remedy_has_plant_ibfk_1` FOREIGN KEY (`remedy_id`) REFERENCES `remedy` (`remedy_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

Any ideas ?
Nov 28 '08 #23

P: 52
It seems a foreign key constarint is violated. you are using a remedy_id which is not present in remedy table.
Nov 28 '08 #24

Post your reply

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