English 中文(简体)
2. 与Ajax php和Isql的汽车
原标题:autocomplete with ajax php and mysql

我的表格有两个文本箱和一个提交纽州。 第二箱自动完成了投入。 该网页上载有两个文本箱的内容(使用jax)。

汽车阵列储存在地板上。 文本箱的数值储存在单单面表格中。

这些法规是:

1> 自动测试。

<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);
}

该守则是在复读网页之前使用的,表格中包含有<条码>的电池(目标为“超文本输入/代码”而不是文本箱的内容。 此外,数据库中的表格也插入[object RUSInputEle]

问题是什么? 请提供帮助。

最佳回答

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.

这应该是你试图在这里做的一切。

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...