943,542 Members | Top Members by Rank

Ad:
Jun 18th, 2008
0

Storing a selected item into a global variable

Expand Post »
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) :


JavaScript Syntax (Toggle Plain Text)
  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:

HTML Syntax (Toggle Plain Text)
  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
mgt
Reputation Points: 8
Solved Threads: 0
Light Poster
mgt is offline Offline
28 posts
since Jun 2008
Jun 19th, 2008
-1

Re: Storing a selected item into a global variable

C'mon folks! Can't anyone help?
mgt
Reputation Points: 8
Solved Threads: 0
Light Poster
mgt is offline Offline
28 posts
since Jun 2008
Jun 23rd, 2008
0

Re: Storing a selected item into a global variable

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jun 24th, 2008
0

Re: Storing a selected item into a global variable

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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.
mgt
Reputation Points: 8
Solved Threads: 0
Light Poster
mgt is offline Offline
28 posts
since Jun 2008
Jun 24th, 2008
0

Re: Storing a selected item into a global variable

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>
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Need help with form submission
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Onclick to pass a variable to a PHP script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC