阿贾克斯是要走的路。
本示例使用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