【问题标题】:Target 'Search' in Whole Page在整个页面中定位“搜索”
【发布时间】:2011-03-31 20:54:29
【问题描述】:

我希望我的搜索栏完成搜索,但是,我希望目标是“整页”而不是完全移动到另一个页面?

<div id="search_anything"> 
    <form action="/search.php" method="get" id='searchForm'> 
        <div class="searchFormDiv"> 
            <input type="text" name="search" value="Search Mail..." id="search" onfocus="if( $(this).attr('value').indexOf('...') >= 0) $(this).attr('value',''); $(this).select();" /> 
        </div> 
    </form> 
</div>

【问题讨论】:

  • 您的意思是要在同一页面上搜索并显示结果,而不是重定向到另一个页面显示结果?

标签: php javascript html search


【解决方案1】:
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="get" id='searchForm'> 

不要忘记,大多数时候您也需要在同一页面上处理表单(或在该页面上包含单独的表单处理器脚本)。

例如,如果您想使用另一个 PHP 文件处理表单,您可以将其粘贴到表单上方的任何位置:

<?php if(isset($_GET['search'])) include 'formprocessor.php'; ?>

【讨论】:

  • 当我输入该代码时,我得到以下链接:-liveternet.com/?search=(My 搜索)但是,我想要:liveternet.com/search.php?search=(My 搜索)
  • @user686512 当您不想重定向到 search.php 时,为什么要在 action 属性中添加 search.php?通过 liveternet.com/?search=mysearch,您将能够在同一页面上处理表单。如果你真的想要两者,你可以用 Apache 的 mod_rewrite(在 .htaccess 文件中)进行一些重写。
【解决方案2】:

当我输入该代码时,我得到以下链接:- liveternet.com/?search=(我的搜索),但是,我想要:liveternet.com/search.php?search=(我的搜索)

【讨论】:

    【解决方案3】:

    使用 jQuery:

    $('form').submit(function() {
        $.post(
        'do_query.php',         // File with query
        { num: '1', str: 'string' },    // Vars passed as post
        function(responseText){     // Callback
            $('#your_div').html(responseText); // Load content results from do_query page.
        },
        "html"              // Set type as html
    );
        preventDefault();           // Prevent form from submiting
    });
    

    这应该可行。请提供反馈。

    【讨论】:

    • 为什么要在一段 JavaScript 中放置一个 PHP 块?除此之外,它不适用于禁用 JavaScript。
    • 已编辑。现在改用 ajax 请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多