Nothang happens when I select a choice on the form. Javascript and ajax works for other pages. I'm a newbie so I know there is a stupid mistake here
Expand|Select|Wrap|Line Numbers
- <html>
- <head>
- <script src="selectuser.js"></script>
- </head>
- <body><form>
- Select a User:
- <select name="users" onchange="showUser(this.value)">
- <option value="1">Peter Griffin</option>
- <option value="2">Ronald Reagan</option>
- <option value="3">Harry Potter</option>
- </select>
- </form><p>
- <div id="txtHint"><b>User info will be listed here.</b></div>
- </p></body>
- </html>
Expand|Select|Wrap|Line Numbers
- var xmlHttpfunction showUser(str)
- {
- xmlHttp=GetXmlHttpObject()
- alert ("test if we are in javascript")
- return
- if (xmlHttp==null)
- {
- alert ("Browser does not support HTTP Request")
- return
- }
- var url="getuser.php"
- url=url+"?q="+str
- url=url+"&sid="+Math.random()
- xmlHttp.onreadystatechange=stateChanged
- xmlHttp.open("GET",url,true)
- xmlHttp.send(null)
- }function stateChanged()
- {
- if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
- {
- document.getElementById("txtHint").innerHTML=xmlHttp.responseText
- }
- }function GetXmlHttpObject()
- {
- var xmlHttp=null;
- try
- {
- // Firefox, Opera 8.0+, Safari
- xmlHttp=new XMLHttpRequest();
- }
- catch (e)
- {
- //Internet Explorer
- try
- {
- xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (e)
- {
- xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- return xmlHttp;
Expand|Select|Wrap|Line Numbers
- <?php
- $q=$_GET["q"];
- $con = mysql_connect('localhost', 'user id', 'password');
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("test_database", $con);
- //$q = 2;
- $sql="SELECT * FROM user WHERE id = '".$q."'";
- $result = mysql_query($sql);
- echo "<table border='1'>
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Age</th>
- <th>Hometown</th>
- <th>Job</th>
- </tr>";
- while($row = mysql_fetch_array($result))
- {
- echo "<tr>";
- echo "<td>" . $row['FirstName'] . "</td>";
- echo "<td>" . $row['LastName'] . "</td>";
- echo "<td>" . $row['Age'] . "</td>";
- echo "<td>" . $row['Hometown'] . "</td>";
- echo "<td>" . $row['Job'] . "</td>";
- echo "</tr>";
- }
- echo "</table>";
- mysql_close($con);
- ?>