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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料