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

  • SEARCH
  • Home
  • 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 6886871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:48:51+00:00 2026-05-27T05:48:51+00:00

I’m trying to implement an AJAX application where the user enters the id of

  • 0

I’m trying to implement an AJAX application where the user enters the id of a user, all the information associated with the id will be filled in the remaining text fields. For example, when the user enters the id number, “1234”, the lastname, firstname, favorite color and favorite number fields automatically get filled with the information found in the mysql database for that id. From there the user will be able to see all the current information associated with that particular id and make changes.

Below is my script, however when I run this, all the values that are returned read “undefined”. When I run for errors via firebug, a GET response is made: the response simply reads:

SELECT * from users where id = \”$id\”

Any idea what might be wrong?

<html>
    <head>
    <script type = "text/javascript">
        var xhr;
        if (window.ActiveXObject)
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
            xhr = new XMLHttpRequest();
        }

        function callServer()
        {
            // Create the id number
            var id = document.getElementById("id").value;

            // Create regular expressions
            var reg1 = /\d{4}/;

            // Test the string against the regular expression
            if (reg1.test(id))
            {
                var idCheck = id;
            }
            else
            {
                alert ("Invalid id number");
                return;
            }

        // Build the URL to connect to
        var url = "(restofurl).../dataExtract.php?idCheck=" +escape(idCheck);

        // Open a connection to the server
        xhr.open("GET", url, true);

        // Setup a function for the server to run when it is done
        xhr.onreadystatechange = updatePage;

        // Send the request
        xhr.send(null);
    }

    function updatePage()
    {
        if ((xhr.readyState == 4) && (xhr.status == 200))
        {
        var response = xhr.responseText.split(",");
        var reg3 = /\S+/;
            if (!reg3.test(document.getElementById("lastname").value))
        {
                document.getElementById("lastname").value = response[1];
        }
            if (!reg3.test(document.getElementById("firstname").value))
        {
                document.getElementById("firstname").value = response[2];
        }
            if (!reg3.test(document.getElementById("color").value))
        {
                document.getElementById("color").value = response[3];
        }
            if (!reg3.test(document.getElementById("number").value))
        {
                document.getElementById("number").value = response[4];
        }
        }
    }
</script>
</head>
<body>
    <form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
        <p>Update Database:</p>

        <p>ID:<input type = "id" id="id" name="id" size="20" maxlength="40" onchange = "callServer();" /></p>
        <p>Update Last Name:<input type = "lastname" id="lastname" name="lastname" size="20" maxlength="40" /></p>
        <p>Update First Name:<input type = "firstname" id="firstname" name="firstname" size="20" maxlength="40" /></p>
        <p>Update Favorite Color:<input type = "color" id="color" name="color" size="20" maxlength="40" /></p>
        <p>Update Favorite Number:<input type = "number" id="number" name="number" size="20" maxlength="40" /></p>
       <input type="submit" id="submit" name ="submit" value="Update" /><br><br>


    </form>
</body>
</html>

dataExtract.php

<?php

// Connect to the MySQL database
$con = mysql_connect("server","user","password");

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("tablename", $con);

$id = $_GET["id"];

$table = "users";
$query = 'SELECT * from $table where id = \"$id\"';
$result = mysql_query($query) or trigger_error(mysql_error().$query);

if (empty($result))
{
    $response = " , , , ";
}
else
{
    $row = $result->fetch_row();
    $response = $row[1].",".$row[2].",".$row[3].",".$row[4];
    echo $response;
}


// Close connection to the database
mysql_close($con);
?>
  • 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-27T05:48:52+00:00Added an answer on May 27, 2026 at 5:48 am

    You have used $table in single quotes in the query resulting in ‘select * from $table…’ being executed by MySQL. Similarly, you have add backslash to double quotes which is not needed when you are using single quotes for specifying string literal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.