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 7040511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:55:37+00:00 2026-05-28T01:55:37+00:00

I have a form with two textboxes and a submit button. The second box

  • 0

I have a form with two textboxes and a submit button. The second box autocompletes the input. On submit, the page refreshes and displays a table with contents of the two textboxes(using ajax).

The autocomplete array is stored in mysql. The textboxes’ values are stored in a separate table in mysql.

These are the codes:

1) autotesting.html

<html>
<head>
<title>PHP using AJAX</title>
<script type=""text/javascript" src="prototype.js"></script>
<link rel="stylesheet" href="autocomplete.css" type="text/css" media="screen">
<script src="jquery.js" type="text/javascript"></script>
<script src="dimensions.js" type="text/javascript"></script>
<script src="autocomplete.js" type="text/javascript"></script>

<script type="text/javascript">

var time_variable;

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object

function ajaxFunction() {
  var getdate = new Date();  //Used to prevent caching during ajax call

 if(xmlhttp) {
    var txtname = document.getElementById("txtname");
    var searchField = document.getElementById("searchField");
    xmlhttp.open("POST","autotesting2.php",true); //calling testing2.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("txtname="+ txtname + "&searchField=" + searchField); //Posting to PHP File
  }
}

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}




$(function(){
        setAutoComplete("searchField", "results", "autocomplete.php?part=");
    });
</script>

</script>
<body>
<form name="myForm">
<table>
 <tr>
  <td>Add New Item Type</td>

 <td>

    <p id="auto">
        <label>Colors: </label><br>
        <input type="text" id="txtname" name="txtname" /><br><br>
        <input id="searchField" name="searchField" type="text" /><br><br>

</p>
</td>   
</tr>
 <tr>
  <td colspan="2"><input type="button" value="Add new item" onclick="ajaxFunction();" />


 </tr>
</table>
<div id="message" name="message"></div>
</form>
</body>
</head>
</html>

2) autotesting2.php

<?php
$conn = mysql_connect("localhost","demo","demo");
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
 $txtname = $_POST["txtname"];
$searchField = $_POST["searchField"];
$sql = "INSERT INTO test3 (txtname,searchField) VALUES ('$txtname','$searchField')";
mysql_select_db('test_db'); 
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "<table border='2' cellspacing='5' cellpadding='5'>";
$result=mysql_query("SELECT * FROM test3"); 
while($row=mysql_fetch_array($result)) 
  {
  echo "<tr>";
  echo "<td>" . $row['txtname'] . "</td>";
  echo "<td>" . $row['searchField'] . "</td>";
  echo "</tr>";
  }
echo "</table>";echo "<br>";
mysql_close($conn); 
?>

3) autocomplete.php

<?php

$link = mysql_connect('localhost', 'demo', 'demo');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("test_db")) {
   echo "Unable to select test_db: " . mysql_error();
   exit;
}

$result = mysql_query("SELECT name FROM  sks_color");
while ($row = mysql_fetch_assoc($result)) {
        $colors[]=$row['name'];
}
mysql_free_result($result);
mysql_close($link);

// check the parameter
if(isset($_GET['part']) and $_GET['part'] != '')
{
    // initialize the results array
    $results = array();

    // search colors
    foreach($colors as $color)
    {
        // if it starts with 'part' add to results
        if( strpos($color, $_GET['part']) === 0 ){
            $results[] = $color;
        }
    }

    // return the array as json with PHP 5.2
    echo json_encode($results);
}

The code is working fine until the page refreshes and table contains cells with [object HTMLInputEle] instead of content of textboxes. also, the table in database gets inserted with [object HTMLInputEle].

What is the problem ? Please help.

  • 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-28T01:55:38+00:00Added an answer on May 28, 2026 at 1:55 am

    First, Why are you loading jQuery if you’re not going to use it?
    jQuery already has everything you need to do AJAX requests, so you may want to give it a try (especially since you’re loading it) instead of creating your own xmlhttp object.

    Next, if you’re trying to get autocomplete working, since you already have jQuery loaded, you may also want to load the jQuery UI
    Then you can use the jQuery UI autocomplete.

    That should be everything you’re trying to do here.

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

Sidebar

Related Questions

I have form with one input for email and two submit buttons to subscribe
I have a form with two input textboxes, and I have included jQuery validation
I have a form that contains two input textboxes, one called serial and the
I have two forms, one with a button 'Add' which loads the second form
I have two buttons inside a form call: <% using (Html.BeginForm()) {%> <input type=submit
I have a form with two text_fields: <input type=text id=post_name name=post[name] /> <input type=text
I have an input form with two textareas allowing a user to type in
I have two form elements on my page that act as a smooth login
I have an HTML form containing some textboxes and two jqGrids. The user selects
I have a form on a dialog box like so: <form action=../ControlerFunction> <input type=text

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.