dsnw2651 2009-11-25 12:34
浏览 5

如何进行AJAX搜索

I'm trying to make an AJAX search for a website.

HTML:

<form id="search" method="post" action="search.php">
    <input type="text" name="search" />
    <input type="image" name="submit" alt="search" src="images/buttons/search.gif" />
</form>
$(function() {
    var search_text = '';
    $('form#search input[name=submit]').click(function() {
        search_text = $('form#search input[name=search]').val();
        $.get('../search.php',{ s: search_text });
        return false;
    });
});

In the search.php file I have the following:

<?php
    $search = $_GET['s'];
    if (isset($_POST['submit_x'])) {            
        $search = $_POST['search'];
        $search = str_replace(' ','',$search);
        $search = strtolower($search);

        if($search == 'kingbabychosenheart' || $search == 'chosenheart' || $search == 'beltchosenheart') {
            echo "<meta http-equiv='refresh' content='0;url=chosen_heart.php'>";
        } else {
            echo "<meta http-equiv='refresh' content='0;url=not_found.php'>";
        }       
    }       
?>

But this doesn't work. What else do I have to do to make this work?

Thanks

  • 写回答

3条回答 默认 最新

  • dook0034 2009-11-25 12:37
    关注

    Well, first of, your form will not be usable with Javascript disabled. I hope you know that, since your form has no submit element per se.

    With that in mind, you should use the submit() event and attach it to the form, then trigger that event when the image is clicked.

    $(function() {
        var search_text = '';
        $('#search').bind('submit',function() {
            search_text = $('input[name=search]', $(this)).val();
            $.get('../search.php',{ s: search_text });
            return false;
        });
    
        $('#search img[name="submit"]').click(
            $('#search').trigger('submit');
        });
    });
    

    Or better still, use the jquery form plugin.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法