jQuery Post variable to PHP



控制台结果示例

嗨,我的jQuery有问题。我有一个文本框,我想在没有提交按钮的情况下从数据库中进行实时搜索,当我运行已经有选择SQL查询的showcharges($_GET["id"],$sort,$search(函数时激活。ajax的问题是,如果搜索某个东西,我希望将我在php变量上搜索到的数据传递为_POST['search'];在同一页上在id=63并且是我的函数$_GET["id"]的第一个参数的照片的情况下,在我的函数的$search参数上传递该数据加上所有这些而不丢失我的特定客户id的查询字符串因此,我希望ajax保留我的$get["id"],并将我搜索的单词导出到_post["search"]变量。

我做不到。。他们不将_POST['search']识别为POST,因此$search;unedified

我的PHP charge.PHP调用函数Data::showcharges($_GET[quot;id"],$sort,$search(;

  1. $_GET["id"]是上一页的查询字符串,它显示了我的查询(工作(中的特定客户id
  2. $sort正在对费用进行排序(仅在函数内部不为null时启用(
  3. $search搜索查询以查找费用,(仅在函数内部不为空时启用(

<?php

if (isset($_POST['sort']))
{
$sort = $_POST['sort'];
}


$search = $_POST['search'];
echo  $search; 

?>



$showcharge = Data::showcharges($_GET["id"], $sort, $search);          

<input type="text" name="search"  class="txt" />
<span id="show" name="mytext"></span>


此外,我的排序表单对费用进行了排序(你可以在函数中看到查询,它也想用ajax生命排序来代替,并避免提交按钮,我不想我该怎么做

<form action="" method="POST">
<tr>
<th> <select name="sort">

<option value="dateasc">Date Asc </option>
<option value="datedesc">Date Desc</option>
<option value="asc">Balance Asc </option>
<option value="desc">Balance Desc</option>

</select>

<button type="submit" name="submit" value="submit">Search</button>
</th>
</tr>
</form>

我的脚本想取代搜索框上的传统提交按钮,并在没有按钮的情况下进行终身搜索

<script>
$(document).ready(function() {
$('input.txt').keyup(function() {
var search = $(this).val();
var val = "<?php echo $_GET['id']; ?>";
$.ajax({
url: "charge.php",
type: "POST",
dataType: 'text',
data: {
search: search,
val: val
},
success: function(response) {
$('#show').html(search);
console.log(search);
console.log(val);
}
});
});
});
</script>

我的php函数正在完全工作。来自dataprocess.php


public static function showcharges($id = null, $sort = null, $search = null)
{    
$sql = "SELECT id,contact_date,tasks,taskcharges,payment,balance,payment_date,admin,customers_id,comments,created_at";

$sql .= " ,(select (taskcharges-payment) from customers where charges.customers_id=customers.id) as balance";

$sql .= " FROM charges ";
if (!empty($id)) {
$sql .= " WHERE customers_id=" . $id;
}

if (!empty($search)) {  

$sql .= "  AND tasks LIKE '%" . $search . "%' OR comments LIKE  '%" . $search . "%'";
}

if ($sort == "asc") {   
$sql .= "  ORDER BY balance ASC ";
} elseif ($sort == "desc") {

$sql .= " ORDER BY balance DESC ";
} elseif ($sort == "dateasc") {    
$sql .= " ORDER BY created_at ASC ";    
} elseif ($sort == "datedesc") {
$sql .= " ORDER BY created_at DESC ";
}
/* 
echo $sql;
die();  */
$statement = conn()->prepare($sql);
$statement->execute();
$customers = $statement->fetchAll();

return $customers;
}

尝试这种方式

$.ajax({
url: "charge.php",
type: "POST",
dataType: 'json'
data: {
query: search
},
success: function(response) {
$('#show').html(response);

}

});

当您通过Jquery发送ajax并希望设置请求的方法时。u应该使用";类型";并将数据设置为JSON,之后u可以在php 中使用$_POST

相关内容

  • 没有找到相关文章

最新更新