473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to retrieve data using dropdown from database using ajax on same page

254 100+
hello i want to display the subjects on the same page based on class selected by user.

i am using dropdown for classes. so when user will select a particular class then it will display subjects in the same page based on class from database.

i am giving my code along with so please tell me the error.

code for database
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include "database.php";
  3. $class1= $_GET['q'];
  4. $selectclass= "SELECT Subjects FROM subjects WHERE Class= '$class1'";
  5. $query=mysql_query($selectclass); 
  6. echo "<table>";
  7. echo"<tr>";
  8. echo"<th> Subjects</th><th>Marks</th></tr>";
  9. while($rows=mysql_fetch_array($query))
  10. {
  11.     echo "<tr>";
  12.     echo "<td>";
  13.     echo $rows['Subjects'];
  14.     echo "</td>";
  15.     echo "<td>";
  16.     echo "<input type= text/>";
  17.     echo "</td>";
  18.     echo "</tr>";
  19.  
  20. }
  21. echo "</table>";
  22. ?>
  23.  
  24.  
code for javascript file
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2. function selectClass(str)
  3. {
  4.     alert(str);
  5.     xmlHttp=GetXmlHttpObject()
  6.     if(xmlHttp==null)
  7.     {
  8.         alert("Error");
  9.         return 
  10.     }
  11.     var url="getsubject.php";
  12.     url=url+"?q"=str;
  13.     //url=url+"&sid="+Math.random();
  14.     xmlHttp.onreadystatechange=stateChanged
  15.     xmlHttp.open("GET",url,true)
  16.     xmlHttp.send(null)
  17. }
  18.  
  19. function stateChanged() 
  20.     { 
  21.         if (xmlHttp.readyState==4|| xmlHttp.readyState=="complete")
  22.         { 
  23.         document.getElementById("txtselect").innerHTML=xmlHttp.responseText;
  24.         }
  25.     }
  26.  
  27. function GetXmlHttpObject()
  28. {
  29.     var xmlHttp=null;
  30.     try
  31.       {
  32.       xmlHttp=new XMLHttpRequest();
  33.       }
  34.     catch (e)
  35.       {
  36.       try
  37.         {
  38.         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  39.         }
  40.       catch (e)
  41.         {
  42.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  43.         }
  44.       }
  45.     return xmlHttp
  46. }
  47.  
i have called javascript file in the header section. i have also used <div element with id txtselect>

but value is not coming and not showing any error as well..I think the value for $class1 is not coming that's why it is showing nothing.

please help me out.
Nov 5 '07 #1
2 5327
Atli
5,058 Expert 4TB
Hi. Welcome to TSDN!

What does the alert(str) show you?
Have you made sure the PHP script works and prints out the expected results? (By navigating to it in your browser)
Nov 10 '07 #2
ak1dnar
1,584 Expert 1GB
http://yourdomain/getsubject.php?q=set_the_value_here
First, execute the server page directly like this. Then you can find where the issue is.
Nov 10 '07 #3

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

Similar topics

2
by: Wysiwyg | last post by:
I was hoping to get some opinions on the efficiency of various methods of reusing the same dropdown list data. Here is the situation: Multiple panels on maintenance pages with TAB menus across...
5
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say...
2
by: Micela | last post by:
Hi, Please help me out in this task. I have a filed in the database of datetime. and 3 dropdown lists in the page. Im using Asp.net with VB coding. The three dropdown lists are 1.for date...
6
by: Daz | last post by:
Hi everyone. Firstly, I apologise if this i not what you would call a PHP problem. I get quite confused as to what lives in which realm, so if this shouldn't be posted here, please suggest where...
1
by: AndiSmith | last post by:
Hi guys, I wondered if anyone could help me with this problem, or even shed some light on the direction I need to take to resolve it? I'm using .NET 2.0 (C# flavor) to build a large user-based...
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
1
by: krishemanth | last post by:
sir, I want to update a value in dropdown using ajax i had a listing of some categories in dropdown along with that i had an add category in that dropdown box when select that addcategory option...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
1
by: d0353101 | last post by:
Hi, I am having 2 dropdowns on my page. The values in both the drop downs have to be populated through DataBase. Problem : I dont know how to populate the second Dropdown automatically if...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...
0
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,...
0
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...

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.