Storing a selected item into a global variable

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Storing a selected item into a global variable

 
0
  #1
Jun 18th, 2008
An item is selected from a list and then used as a variable in a PHP script to identify and retrieve some data from a specific record in a MySQL database.


Here's the code for the select script (first a javascript external file, then an html file) :


  1. /* This is placed in an external file, addingOptions.js */
  2.  
  3. var otherStuff = {
  4. "item 1" : [ "subitem 1.1", "subitem 1.2", "subitem 1.3", "subitem 1.4" ],
  5. "item 2" : [ "subitem 2.1", "subitem 2.2" ],
  6. "item 4" : [ "subitem 4" ],
  7. "item 6" : [ "subitem 6.1", "subitem 6.2" ]
  8. };
  9.  
  10. function selectAll(listName, selected) {
  11. var listBox = document.getElementById(listName);
  12. for(i=0; i<listBox.length; i++) {
  13. listBox.options[i].selected=selected;
  14. }
  15. if( listBox.onchange ) {
  16. listBox.onchange();
  17. }
  18. }
  19.  
  20. function lstStuff_OnChange() {
  21. var listBox = document.getElementById("lstStuff");
  22. var subListBox = document.getElementById("lstOtherStuff");
  23. subListBox.options.length=0;
  24. for(i=0; i<listBox.length; i++) {
  25. if( listBox.options[i].selected ) {
  26. var key = listBox.options[i].text;
  27. if(otherStuff[key]) {
  28. for(j=0; j<otherStuff[key].length; j++) {
  29. subListBox.options.add(new Option(otherStuff[key][j],otherStuff[key][j]));
  30. }
  31. }
  32. }
  33. }
  34. }



Here's the main .html code/file:

  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript" src="addingOptions.js"></script>
  5.  
  6. </head>
  7.  
  8. <body>
  9. <form>
  10. <select id="lstStuff" multiple="multiple" onChange="lstStuff_OnChange()" size="6" style="width:200px;">
  11. <option>item 1</option>
  12. <option>item 2</option>
  13. <option>item 3 (No Sub-Items)</option>
  14. <option>item 4</option>
  15. <option>item 5 (No Sub-Items)</option>
  16. <option>item 6</option>
  17. </select>
  18. <br>Select:
  19. <a href="javascript:selectAll('lstStuff', true);">all</a> |
  20. <a href="javascript:selectAll('lstStuff', false);">none</a>
  21. <p>
  22. <select id="lstOtherStuff" multiple="multiple" size="6" style="width:200px;">
  23. </select>
  24. <br>Select:
  25. <a href="javascript:selectAll('lstOtherStuff', true);">all</a> |
  26. <a href="javascript:selectAll('lstOtherStuff', false);">none</a>
  27. </form>
  28.  
  29.  
  30. </body>
  31.  
  32. </html>


URL for script above:
http://javascript.internet.com/forms...options-2.html


You'll notice that this produces two select boxes, one with categories and one with subcategories. I'd like to be able to store the selection of a subcategory (by an end user) into a variable (I'm guessing a global variable, since I want to pass it on to a PHP script) and then take that variable and use it in a PHP script, within the main html file, in order to identify a particular record within a MySQL database.

How do I do this?

Any assistance will be greatly appreciated and I'll be happy to publish the final complete code once I get this thing to work.
Last edited by peter_budo; Jun 20th, 2008 at 9:18 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: Storing a selected item into a global variable

 
-1
  #2
Jun 19th, 2008
C'mon folks! Can't anyone help?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Storing a selected item into a global variable

 
0
  #3
Jun 23rd, 2008
Keep a hidden form element on your page which will be populated / updated whenever the user submits the form / changes the value of the secondary drop down. This hidden field can then be read using PHP at the server in the same way you read normal text fields.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: Storing a selected item into a global variable

 
0
  #4
Jun 24th, 2008
Originally Posted by ~s.o.s~ View Post
Keep a hidden form element on your page which will be populated / updated whenever the user submits the form / changes the value of the secondary drop down. This hidden field can then be read using PHP at the server in the same way you read normal text fields.


Sounds like a good idea.

If I have the following code:


<select id="lstOtherStuff" multiple="multiple" size="6" style="width:200px;">
</select>



and I need to pass the variable (an element in an array), "1stOtherStuff", to a PHP script, how do I do that? You mentioned putting it in a hidden form element....can you provide the syntax please?




If I use the following code, I can access the file "welcome.php":

<form action="welcome.php" method="post">

Id like to also use an "onclick" command to initiate the action.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Storing a selected item into a global variable

 
0
  #5
Jun 24th, 2008
Here is a sample script (untested):
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv"Script-Content-Type" content="text/javascript">
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Example</title>
  8. <script type="text/javascript">
  9. // This function sets the hidden field value to the value of the drop
  10. // down contents in the form of comma separated values. Read this
  11. // hidden variable at teh server just like any normal form field, parse
  12. // it and pick out its contents.
  13. function setHidden(frm) {
  14. if(!frm) {
  15. return;
  16. }
  17. var selBox = frm.elements["sel"];
  18. var selValue = "";
  19. for(var i = 0, maxI = selBox.options.length; i < maxI; ++i) {
  20. var opt = selBox.options[i];
  21. if(opt.selected) {
  22. selValue += opt.value + ",";
  23. }
  24. }
  25. var hiddenElem = frm.elements["hiddenParam"];
  26. hiddenElem.value = selValue;
  27. window.alert("The value to be submitted is: " + selValue);
  28. frm.submit();
  29. }
  30. </script>
  31. </head>
  32. <body>
  33. <form id="frm" name="frm" action="#">
  34. <select name="sel" id="sel" multiple="multiple">
  35. <option value="one">1</option>
  36. <option value="two">2</option>
  37. <option value="three">3</option>
  38. <option value="four">4</option>
  39. </select>
  40. <input type="hidden" name="hiddenParam" id="hiddenParam">
  41. <br><br>
  42. <input type="submit" onclick="setHidden(this.form);">
  43. </form>
  44. </body>
  45. </html>
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC