I want to show my database value when use type first character of the value. Let me describe it with my site I have a website with homepage input where user type input as train no. I need that user type train no. he got train name from my database where i store.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
check this code .i think it will help you
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP, jQuery search demo</title>
<link rel="stylesheet" type="text/css" href="my.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("input").keyup(function () {
$('#results').html('');
var searchString = $("#search_box").val();
var data = 'search_text=' + searchString;
if (searchString) {
$.ajax({
type: "POST",
url: 'search.php',
data: data,
dataType: 'text',
async: false,
cache: false,
success: function (result) {
$('#results').html(result);
//window.location.reload();
}
});
}
});
});
</script>
</head>
<body>
<div id="container">
<div style="margin:20px auto; text-align: center;">
<form method="post" action="do_search.php">
<input type="text" name="search" id="search_box" class='search_box'/>
<input type="submit" value="Search" class="search_button"/><br/>
</form>
</div>
<div>
<div id="searchresults">Search results :</div>
<ul id="results" class="update">
</ul>
</div>
</div>
</body>
</html>
first create html and jquery code for input field which you type then call jquery function keyup which hit database using ajax method then create a php file which manage your search i create a search.php file
<?php
$servername = "localhost";
$username = "db_username";
$password = "db_password";
$dbname = "your_db_name";
$searchquery = trim($_POST['search_text']); //input for search
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT filed1, field2 FROM yourtable_name WHERE match_text LIKE '%$searchquery%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " - Name: " . $row["filed1"]. " " . $row["field2"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
from this page you will get your search result and you can change it as your demand . for your check you can also add search text length if you do not search if search text length > 2 or etc
回答2:
I didn't really get your question but from what I got, you can use this query:
SELECT * FROM table_name WHERE column LIKE '%value_here%'
回答3:
Try this
if (isset($_POST['searchVal'])) {
$searchquery = $_POST['searchVal']; //input for search
//count table to see if any row/column has the train number
$searcher = mysqli_query("SELECT COUNT(*) FROM tablename WHERE trainno LIKE '%$searchquery%'") or die("could not search");
$count = mysqli_num_rows($searcher);
if ($count == 0) {
echo "No results found";
} else {
//while loop to select row with train name
while($row = msqli_fetch_array($query)) {
$trainnname = $row['trainname'];//name of row containing train name
echo $trainname;
}
}
}
Hope you understand how this works