473,669 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remember selection of drop down menu using ajax with php

6 New Member
I have drop down menu.
If visitor selects certain option from drop down menu I need to show certain content (without page reload; so I need to use Ajax).

At the same time I need save selection of drop down menu after visitor click on Submit button and page reloads.

Below is code.
The first menu remembers selection but does not display content (Ajax does not work).

The second menu only Ajax works, but does not remember selection.

And the third menu remember selection, but something wrong with Ajax.

Could someone advice what to change to get Ajax work and remember selection after page reload?

file.php
Expand|Select|Wrap|Line Numbers
  1. <head>
  2.  
  3. <script type="text/javascript">
  4. function showData1(str)
  5. {
  6. if (str=="")
  7.   {
  8.   document.getElementById("showData1").innerHTML="";
  9.   return;
  10.   }
  11. if (window.XMLHttpRequest)
  12.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  13.   xmlhttp=new XMLHttpRequest();
  14.   }
  15. else
  16.   {// code for IE6, IE5
  17.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  18.   }
  19. xmlhttp.onreadystatechange=function()
  20.   {
  21.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  22.     {
  23.     document.getElementById("showData1").innerHTML=xmlhttp.responseText;
  24.     }
  25.   }
  26. xmlhttp.open("GET","__gethint.php?q1="+str,true);
  27. xmlhttp.send();
  28. }
  29. </script>
  30.  
  31.  
  32. <script type="text/javascript">
  33. function showData2(str)
  34. {
  35. if (str=="")
  36.   {
  37.   document.getElementById("showData2").innerHTML="";
  38.   return;
  39.   }
  40. if (window.XMLHttpRequest)
  41.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  42.   xmlhttp=new XMLHttpRequest();
  43.   }
  44. else
  45.   {// code for IE6, IE5
  46.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  47.   }
  48. xmlhttp.onreadystatechange=function()
  49.   {
  50.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  51.     {
  52.     document.getElementById("showData2").innerHTML=xmlhttp.responseText;
  53.     }
  54.   }
  55. xmlhttp.open("GET","__gethint.php?q2="+str,true);
  56. xmlhttp.send();
  57. }
  58. </script>
  59.  
  60.  
  61. <script type="text/javascript">
  62. function showData3(str)
  63. {
  64. if (str=="")
  65.   {
  66.   document.getElementById("showData3").innerHTML="";
  67.   return;
  68.   }
  69. if (window.XMLHttpRequest)
  70.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  71.   xmlhttp=new XMLHttpRequest();
  72.   }
  73. else
  74.   {// code for IE6, IE5
  75.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  76.   }
  77. xmlhttp.onreadystatechange=function()
  78.   {
  79.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  80.     {
  81.     document.getElementById("showData3").innerHTML=xmlhttp.responseText;
  82.     }
  83.   }
  84. xmlhttp.open("GET","__gethint.php?q3="+str,true);
  85. xmlhttp.send();
  86. }
  87. </script>
  88.  
  89.  
  90. </head>
  91.  
  92. <a name="calculate"></a>
  93.  
  94. <form method="post" enctype="multipart/form-data" action="<?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?>#calculate" name="quote">
  95.  
  96.  
  97.  
  98. <div id="calculator-left">Salary book
  99. <?php $options = array( 1=>'is submitted', 'is not submitted' ); $showData1 = $_REQUEST['showData1']; ?>
  100. <select name="showData1" onChange="showData1(this.value) style="margin-bottom:3px; width:100px">
  101. <?php foreach ( $options as $i=>$opt ) : ?>
  102. <option value="<?php echo $i?>" <?php echo $i == $showData1 ? 'selected' : ''?>><?php echo $opt ?></option>     <?php endforeach; ?> </select>
  103. </div>
  104.  
  105. <div id="showData1">Use the select box to change page content</div>
  106.  
  107.  
  108.  
  109. <div class="clear">&nbsp;</div> 
  110.  
  111.  
  112. <div id="calculator-left">
  113.  
  114. <select onChange="showData2(this.value)">
  115. <option value="">Select page Content:</option>
  116. <option value="1">Page Content 1</option>
  117. <option value="2">Page Content 2</option>
  118. <option value="3">Page Content 3</option>
  119. <option value="4">Page Content 4</option>
  120. </select>
  121.  
  122. </div>
  123.  
  124. <div id="showData2">Use the select box to change page content</div>
  125.  
  126.  
  127. <div class="clear">&nbsp;</div> 
  128.  
  129. <div id="calculator-left">
  130.  
  131. <select name="town" onChange="showData3(this.value)">
  132.  
  133. <?php
  134. $towns = array('ipswich','bury');
  135. foreach($towns as $town){
  136. $selected = (isset($_POST['town'])&&$_POST['town']==$town)? '
  137. selected="selected"':'';
  138. print("<option value=\"$town\"$selected>$town</option>");
  139. }
  140. ?>
  141.  
  142. </select>
  143.  
  144. </div>
  145.  
  146. <div id="showData3">Use the select box to change page content</div>
  147.  
  148.  
  149. <div class="clear">&nbsp;</div> 
  150.  
  151.  
  152.  
  153.  
  154. <input type = "submit" value = "Calculate"></input>
  155.  
  156.  
  157.  
  158.  
  159. </form>
  160.  
  161.  
  162. </body>
  163. </html>
  164.  
  165.  
  166.  
file
__gethint.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. $q1=$_GET["q1"];
  5.  
  6. if ($q1==1) {echo "Page Content 1q1 would be shown";}
  7. if ($q1==2) {echo "Page Content 2q1 would be shown";}
  8. if ($q1==3) {echo "Page Content 3q1 would be shown";}
  9. if ($q1==4) {echo "Page Content 4q1 would be shown";}
  10.  
  11.  
  12.  
  13. $q=$_GET["q2"];
  14.  
  15. if ($q2==1) {echo "Page Content 1q2 would be shown";}
  16. if ($q2==2) {echo "Page Content 2q2 would be shown";}
  17. if ($q2==3) {echo "Page Content 3q2 would be shown";}
  18. if ($q2==4) {echo "Page Content 4q2 would be shown";}
  19.  
  20.  
  21. $q=$_GET["q3"];
  22.  
  23. if ($q3==1) {echo "Page Content 1q3 would be shown";}
  24. if ($q3==2) {echo "Page Content 2q3 would be shown";}
  25. if ($q3==3) {echo "Page Content 3q3 would be shown";}
  26. if ($q3==4) {echo "Page Content 4q3 would be shown";}
  27.  
  28.  
  29.  
  30. ?>
  31.  
  32.  
Dec 26 '12 #1
7 3670
Rabbit
12,516 Recognized Expert Moderator MVP
You have two options.

1) Save the value in a cookie.
2) Save the value in a database on your server.
Dec 27 '12 #2
rigaconnect
6 New Member
Now the aim is that after page reload <div id="showData2" > remembers chosen value (text). At the moment it displays default value...

file.php
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3. function showData2(str)
  4. {
  5. if (str=="")
  6.   {
  7.   document.getElementById("showData2").innerHTML="";
  8.   return;
  9.   }
  10. if (window.XMLHttpRequest)
  11.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  12.   xmlhttp=new XMLHttpRequest();
  13.   }
  14. else
  15.   {// code for IE6, IE5
  16.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  17.   }
  18. xmlhttp.onreadystatechange=function()
  19.   {
  20.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  21.     {
  22.     document.getElementById("showData2").innerHTML=xmlhttp.responseText;
  23.     }
  24.   }
  25. xmlhttp.open("GET","__gethint.php?q2="+str,true);
  26. xmlhttp.send();
  27. }
  28. </script>
  29.  
  30. <script type="text/javascript">
  31.  
  32.     var saveString = "";
  33.  
  34.     function getCookie(isName){
  35.  
  36.         var cookieStr = document.cookie;
  37.         var startSlice = cookieStr.indexOf(isName+"=");
  38.         if (startSlice == -1){return false}
  39.         var endSlice = cookieStr.indexOf(";",startSlice+1)
  40.         if (endSlice == -1){endSlice = cookieStr.length}
  41.         var isData = cookieStr.substring(startSlice,endSlice)
  42.         var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
  43.         return isValue;
  44.     }
  45.  
  46.     function saveForm(){
  47.  
  48.         var saveString = "";
  49.         var nText = document.getElementsByTagName('input');
  50.         for (i=0; i<nText.length-1; i++ )
  51.             {saveString += nText[i].value+":"}
  52.         var isIdx = document.forms[0]['report'].selectedIndex;
  53.         saveString = saveString+isIdx;
  54.         document.cookie = "formData="+saveString+"";
  55.     }
  56.  
  57.     function restoreValues(){
  58.  
  59.         var nValues = getCookie('formData');
  60.         var n = 0;
  61.         if (nValues)
  62.             {
  63.              //alert(nValues)
  64.              var isIndex = nValues.substring(nValues.length-1,nValues.length);
  65.              document.forms[0]['report'].selectedIndex = isIndex;
  66.              var nForm = document.forms[0];
  67.              nValues = nValues.substring(0,nValues.length-1);
  68.              var isText = nValues.split(":");
  69.                  for (i=0; i<nForm.length; i++)
  70.                     {
  71.                      if (nForm[i].type == "text")
  72.                         {nForm[i].value = isText[n++]}
  73.                     }
  74.             }
  75.         }
  76.  
  77.         onload=restoreValues;
  78.         onbeforeunload=saveForm;
  79.  
  80. </script>
  81.  
  82. </head>
  83.  
  84. <body>
  85.  
  86. <a name="calculate"></a>
  87.  
  88. <form method="post" enctype="multipart/form-data" action="<?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?>#calculate" name="quote">
  89.  
  90. <div id="calculator-left">
  91. <select name="report" onChange="showData2(this.value);">
  92. <option value="">Select page Content:</option>
  93. <option value="1">Page Content 1</option>
  94. <option value="2">Page Content 2</option>
  95. <option value="3">Page Content 3</option>
  96. <option value="4">Page Content 4</option>
  97. </select>     
  98. </div>
  99.  
  100. <div id="showData2">Use the select box to change page content</div>
  101.  
  102. <div class="clear">&nbsp;</div> 
  103.  
  104. <input type = "submit" value = "Calculate"></input>
  105.  
  106. </form>
  107.  
  108. </body>
  109. </html>
  110.  
  111.  
file __gethint.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. $q=$_GET["q2"];
  7.  
  8. if ($q2==1) {echo "Page Content 1q2 would be shown";}
  9. if ($q2==2) {echo "Page Content 2q2 would be shown";}
  10. if ($q2==3) {echo "Page Content 3q2 would be shown";}
  11. if ($q2==4) {echo "Page Content 4q2 would be shown";}
  12.  
  13.  
  14.  
  15. ?>
  16.  
  17.  
  18.  
Dec 27 '12 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Is there a question in there somewhere? Or are you posting the code that worked for you?
Dec 28 '12 #4
rigaconnect
6 New Member
Question is
How to get the aim is that after page reload <div id="showData2" > remembers chosen value (text)? At the moment it displays default value...
Dec 28 '12 #5
Rabbit
12,516 Recognized Expert Moderator MVP
I see code in there to get, save, and restore cookies. What's not working? Are there errors? What are the errors?
Dec 28 '12 #6
rigaconnect
6 New Member
By default (when open page) I get displayed this
<div id="showData2"> Use the select box to change page content</div>

If from drop down menu I choose for example <option value="1">Page Content 1</option>, I get displayed this
if ($q2==1) {echo "Page Content 1q2 would be shown";}

So far everything is ok.

But if I press button Submit, page reloads. And after page reload again I get displayed
<div id="showData2"> Use the select box to change page content</div>

But I need to retain content before page reload (Page Content 1q2 would be shown). How to get it...? Need to change something, but I have almost no knowledges in javascript....
Dec 28 '12 #7
Rabbit
12,516 Recognized Expert Moderator MVP
If you have no knowledge in javascript, you'll probably want to learn it before taking on a job of this size.
Dec 28 '12 #8

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

Similar topics

6
15406
by: Greg Scharlemann | last post by:
I am attempting to populate a drop down menu based on the selection of a different drop down menu. However, it is not working correctly, I cannot figure out for the life of me what exactly happens because I am not getting any errors on the page. <html> <script language="javascript"> var phaseArray = new phaseArray(4); var phaseTypeId = new phaseTypeId(4); var phaseId = new phaseId(4);
10
3467
by: Haines Brown | last post by:
I've implemented the horizontal drop down menu discussed recently in this newsgroup, and it works nicely under Galeon and Mozilla, but not IE 5.0. Here are the problems: Under IE 5.0, the there is no drop down menu functionality at all. I wanted to implement an alternative stylesheet for IE, but my link to it is apparently broken. What is wrong with my link? I'd
11
2467
by: Dan | last post by:
Hello all, I am getting records from a db and displaying the records to the user through a drop down menu in an asp page. Each record has 6 fields and I need to display them all to the user in the drop down. The problem is that the fields contain values of all different lengths and the columns are not aligned.
2
1686
by: Logan | last post by:
I'd like to create a dropdown menu (with submenus) using vb.net. Does anyone have any idea how i could go abouts achieving this? tks in advance.
4
2409
by: Amil | last post by:
Does anyone have a recommendation on an AWESOME (free) horizontal drop-down menu using DTHML? I'm not using VS 2005 yet. Thanks. Amil
2
3006
by: bissatch | last post by:
Hi, I have created a drop down menu using nested lists and CSS :hover. I have uploaded it so that people can have a look: http://www.martynbissett.co.uk/testing/dd05.html In FF, it works fine but in IE the boxes are hidden behing other list elements. I have tried setting z-index but apart from that, I dont know how I might fix it. Any ideas?
0
7358
by: tusaar | last post by:
Hi all I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from Mysql. Anybody can help me please.. I will be very much thankfull. Please help me and give me a proper solution for that ASAP. Thanks Tushar
2
3061
by: ponyeyes | last post by:
Hi There, I am a bit of a newbie to PHP programming and I would like to know how I can place a selected drop down option into a PHP variable and then produce an sql query which incorporates this variable. I have created a dynamic drop down menu that populates itself with data from a MySQL table. I now want it to populate a second drop-down menu with relevant options from my database, based upon a query using a variable. For example........
6
1984
by: patelxxx | last post by:
How do I create a new folder in a 'Drop Down Menu' using JavaScript? I have not tried anything yet, so all idea's are welcomed.
5
3516
ddtpmyra
by: ddtpmyra | last post by:
I cannot select the distinct records but Im pretty sure im connected to the database. Please tell me what's wrong with this? and why I cannot see the query result? thanks! echo "Status:"; $res=mssql_query("SELECT distinct(contactmethod) FROM users with(nolock)"); if(mssql_num_rows($res)==0){ echo "there is no data in table.."; } else {
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8896
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8810
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8590
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7410
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2035
muto222
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.