阿賈克斯是要走的路。
本示例使用jQuery。
首先,我們需要設置一個php腳本來獲得我們的價值並搜索它,然後返回我們的數據。
PHP myPhpScript.php
<?php
$search_value = $_POST["field1"]; //Get our value from the post.
//Make sure to do some authentication on the value so that bad values dont get through
$connection = new mysqli(Stuff here);
$rows = $connection->query(
"SELECT * FROM someTable where searchColumn LIKE %".$search_value."%"
);
//Now all we need to do is send out our data. Lets use json.
$out = $rows[0];//Get the first row. NOTE: Make sure to remove any columns that you dont want the client to see.
echo json_encode($out); //Send the client the data.
?>
的Javascript Some script on the page.
var textbox = $("#mySearchBox"); //This is the jQuery object for our text box.
var viewbox = $("#myViewer"); //This is the jQuery object for the div that holds the results
textbox.on("input", function() {
$.ajax({
url: "myPhpScript.php", //This is the php script that we made above.
method: "POST", //This set our method to POST.
data: {field1: textbox.val()}, //set the textbox value to field1
dataType: "json", //So jquery formats the json into an object.
success: function (phpDataObject) { //Now its up to you to decide what to do with the data. Example below
viewbox.html("");//Clear the viewbox.
viewbox.append("Id: "+phpDataObject["id"]); //Show the id of row that was found.
}
});
});
此代碼可能無法正常工作。只需修復出現的任何語法錯誤。
希望這會有所幫助。
評論是否需要任何幫助。
你的意思是一個html輸入元素的值:'? –
定義「HTML值」。我想我知道你想在這裏做什麼,並且我建議你不要把它寫成WHERE MyColumnID =「Hello world」'。 HTML是HTML,MySQL是MySQL。 –
我的意思是隱藏的值。 – Born2Discover