dt2015 2014-09-25 15:24
浏览 23
已采纳

重定向不能在PHP中工作

I'm using jQuery autocomplete and trying to get it to go directly to the link when I click on suggestion or press enter. Here's the code I used:

        <script>
        $(function() {
        $( "#search" ).autocomplete({
        source: 'search.php',
        select: function(event, ui) {
         $(this).val(ui.item.value);
         $(this).parents("form").submit();  // this will submit the form.
   }
        });
        });
</script>

Then I have my php code for search: '

if ($_POST['search'] != null) {
                                //die("asdf: " . $_POST['search']);
                                $search = $_POST['search'];
                                $result = mysqli_query($con,"SELECT * FROM question WHERE text LIKE '%$search%'");
                                while($row = mysqli_fetch_array($result)) {
                                $text = $row['text'];
                                $question_id = $row['question_id'];
                                //die($search . " " . $text);
                                if ($search == $text){
                                    header("Location: http://localhost/showstats.php?question_id=$question_id");
                                    die();
                                }
                                echo "<br>";
                                echo " <a href=\"showstats.php?question_id=" . $question_id;
                                echo "\">$text</a> ";
                                //echo "<br>";
                            }}

The problem is when I click on suggestion it doesn't redirect to "showstats.php?question_id=$question_id", insted give me a blank result page. And when I insert die($search . " " . $text); inside if statement it's showing me text 2 times, so I know it enters the body of it statement.

  • 写回答

3条回答 默认 最新

  • duansang8388 2014-09-25 15:50
    关注

    When you select a suggestion, the event occurs in the client so this code in your PHP is not really necessary:

    if ($search == $text) {
        header("Location: http://localhost/showstats.php?question_id=$question_id");
        die();
    }
    

    Instead, when you select a suggestion, try triggering the redirection from jQuery:

    <script>
    $(function() {
        $( "#search" ).autocomplete({
            source: 'search.php',
            select: function(event, ui) {
                // This will redirect you to whatever URL is in ui.item.value:
                window.location = ui.item.value;
           }
        });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条