2017-03-29 59 views
0

我想在文本框中寫入數據之後將數據插入佔位符。我發現下拉的替代解決方案中進行選擇:在文本框中寫入之後在佔位符中動態插入

<html> 
<head> 
<script> 
function showUser(str) { 
    if (str == "") { 
     document.getElementById("txtHint").innerHTML = ""; 
     return; 
    } else { 
     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 (this.readyState == 4 && this.status == 200) { 
       document.getElementById("txtHint").innerHTML = this.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">Joseph Swanson</option> 
    <option value="4">Glenn Quagmire</option> 
    </select> 
</form> 
<br> 
<div id="txtHint"><b>Person info will be listed here...</b></div> 

</body> 
</html> 

php文件:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
table { 
    width: 100%; 
    border-collapse: collapse; 
} 

table, td, th { 
    border: 1px solid black; 
    padding: 5px; 
} 

th {text-align: left;} 
</style> 
</head> 
<body> 

<?php 
$q = intval($_GET['q']); 

$con = mysqli_connect('localhost','peter','abc123','my_db'); 
if (!$con) { 
    die('Could not connect: ' . mysqli_error($con)); 
} 

mysqli_select_db($con,"ajax_demo"); 
$sql="SELECT * FROM user WHERE id = '".$q."'"; 
$result = mysqli_query($con,$sql); 

echo "<table> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
<th>Age</th> 
<th>Hometown</th> 
<th>Job</th> 
</tr>"; 
while($row = mysqli_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>"; 
mysqli_close($con); 
?> 
</body> 
</html> 

如何修改它,如果我想與Form::text('foo')使用,並開始啓動從MySQL表immidiately搜索?

+2

答:用文本輸入替換'