Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4116702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:44:29+00:00 2026-05-20T22:44:29+00:00

I am trying to display from a MySQL Database using PHP and Ajax but

  • 0

I am trying to display from a MySQL Database using PHP and Ajax but the while loop is not displaying my data in the dropdown list do I have a syntax error? here is my code:

<html>
<head>
    <script type="text/javascript">
        function showUser(str){
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "getuser.php?q=" + str, true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <form>
        <select name="users" onchange="showUser(this.value)">
            <option value="">Select a person:</option>
            <option value="1">Peter Griffin</option>
            <option value="2">Lois Griffin</option>
            <option value="3">Glenn Quagmire</option>
            <option value="4">Joseph Swanson</option>
        </select>
    </form>
    <br/>
    <div id="txtHint">
        <b>Person info will be listed here.</b>
    </div>
</body>
</html>

the php file:

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'djronlove', 'Djronlove');
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("djronlove", $con);

 $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);
  ?> 

The url is http://imaginationeverywhere.info/mysql/ajax/ajaxdatabase.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:44:30+00:00Added an answer on May 20, 2026 at 10:44 pm

    I didn’t see anything obviously wrong. If it was my project, this is what I would do:

    1) first see if your php file is even being called by using a proxy. This will help you figure out if the error is from your client code or your php code. (Personally, I use http://www.charlesproxy.com/ –it has a free version so that you can try it out. I bought a license and it has been worth every penny)

    2) I would use a javascript library that simplifies the ajax process for cross browser compatibility instead of trying to do ajax with straight up javascript. I recommend jquery http://api.jquery.com/jQuery.get/ This will be especially useful if your problem is in your client code.

    3) If your problem is with your PHP code, echo out data to yourself throughout your php file to pinpoint the problem.

    4) I would also enable error reporting to yourself in mysql while you are testing like this, so that if your query is causing an error, you will see it:

    $result = mysql_query($sql) or die(mysql_error());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display ul>li list using PHP while loop and MYSQL. My database
I have been trying to display image from database using php but i am
I'm trying to get the $url value to display from the MySQL database but
I am trying to display my events from a MySQL database. I'm using the
I am trying to display data from a mysql database on my server. I
I'm trying to loop through a list of results from MySQL and display all
I'm trying to display data from a database and I want to know what
HI, I am trying to display results from a database in results.php. In the
I am using this code to display image from URL, but it is not
I'm using dataTables to retrieve, display and organise data from a PHP script (which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.