douzhao1912 2016-04-28 23:48
浏览 42
已采纳

通过ajax POST向PHP发送两个值以查询SQL db

I'm trying to send two values from a form to another PHP using ajax post method. One value is the value that's already entered in an input box, and the other is a value that is being typed into another input box. It acts like a search box. I tried executing the SQL query in my SQL workbench and it returns the value properly. What am I doing wrong in my code?

function searchq6(){
    var searchstate = $("input[name='region']").val();
    var searchTxt = $("input[name='suburb']").val();
    $.post("search-suburb.php", {searchVal: searchTxt, st:searchstate},function(sbb){
        $("#sbb").html(sbb);
    //searchq7();
    });

}

This is the input box where I search and get the value from:

<input type="text" name="region" list="state" value="<?php echo $region;  ?>" placeholder="Select State" id="output">
Suburb:
        <input type="text" name="suburb" list="sbb" value="<?php echo $suburb;  ?>" onkeyup="searchq6()" id="output">
        <datalist id="sbb" name="taskoption6" >
                <option> </option>
        </datalist>

This is the search-suburb.php file:

$output = '' ;
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
$st = $_POST['st'];
$query = mysqli_query($link, "SELECT DISTINCT title FROM `wp_locations`  WHERE state="'.$st.'"  AND `title` LIKE  '%".$searchq."%' ")or die("Could not  search!");
$count = mysqli_num_rows($query);
if($count == 0){
    $output = '<option>No results!</option>';
}else{
while($row = mysqli_fetch_array($query)){
    $suburb = $row['title'];

?>
    <option value="<?php echo $suburb; ?>"><?php echo $suburb; ?>  </option>
<?php
} // while
} // else
} // main if
  • 写回答

2条回答 默认 最新

  • dsd57259 2016-04-29 01:38
    关注

    Got the answer from small snippets gathered through the comments Changed the query to:

    $query = mysqli_query($link, "SELECT DISTINCT title FROM `wp_locations` WHERE state='".$st."'  AND `title` LIKE  '%".$searchq."%' LIMIT 10")or die("Could not search!");
    

    And the ajax to:

    function searchq6(){
        var searchstate = $("input[name='region']").val();
        var searchTxt = $("input[name='suburb']").val();
        $.post("search-suburb.php", {searchVal: searchTxt, st:searchstate})
        .done(function(sbb) {
            $("#sbb").html(sbb);
        });
        //searchq7();
    }
    

    Thanks for all the comments guys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘