我正在尝试从前端的输入发布关键字搜索并在SQL查询中使用它,但它似乎无法识别我的帖子值。
注意:未定义的索引:关键字in 第19行的C:\ wamp64 \ www \ SQLPractice \ filter-table.php
整个PHP
$host = "localhost";
$user = "root";
$pass = "";
$databaseName = "practice_db";
$tableName = "t_cars";
$conn = mysqli_connect($host,$user,$pass,$databaseName);
$dbs = mysqli_select_db($conn, $databaseName);
$filter = mysqli_real_escape_string($conn, $_POST['keyword']);
$sql = 'SELECT *
FROM "$tableName"
WHERE (c_id LIKE "$filter" OR
c_brand LIKE "$filter" OR
c_model LIKE "$filter" OR
c_year LIKE "$filter" OR
c_color LIKE "$filter")';
$result = mysqli_query($conn, $sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} //query
$array = mysqli_fetch_all($result); //fetch result
echo json_encode($sql);
FRONT END EXCERPT
...<div id="filter">
<h1>Filter</h1>
<form name="form" action="http://localhost/SQLPractice/filter-table.php" method="post">
<input type="" name="keyword" id="filter">
</form>
<br>
<button>Query DB</button>
</div>
</body>
<script type="text/javascript">
var filter = document.getElementById('filter'),
button = document.getElementsByTagName('button')[0];
button.addEventListener("click", function(){filterDatabase();});
function filterDatabase(){
console.log("filterDatabase()");
console.log($("input").val());
$.ajax({
url: 'http://localhost/SQLPractice/filter-table.php', //the script to call to get data
data: $("input").val(), //you can insert url argumnets here to pass to api.php
method: "POST", //for example "id=5&parent=6"
success: function(data) //on recieve of reply
{
//Breaking CSV into array;
var json = data;
$("#filteredContent").html(data);
答案 0 :(得分:2)
输入框ID为id="filter"
以下
<input type="" name="keyword" id="filter">
所以你可以获取并发送输入类型的值为
data: {keyword:$("#filter").val()},