-3

如果没有结果,我想将搜索词(例如 foo 的含义)提交到表单中

细节:

我有一个带有 if else 条件的搜索功能,如果有搜索结果而不是显示带有“未找到搜索结果”的 else 消息

我想通过一个表单提交来更改“未找到搜索结果”,其中值将是用户尝试查找的任何搜索词。所以...

if search result found {
 //display result
} else {
  // auto submit below form if above condition is not true ( means if no search result found )

    <form action="to url" method="post">
    <input name="title" type="text" />
    </form>
}

以上是我用来提交表格的表格。

编辑:

这是实际的代码

.......

if (count($results))
            $qa_content['title']=qa_lang_html_sub('main/results_for_x', qa_html($inquery));
        else { ?>

            <form id="nosearch" method="POST" action="<?php echo qa_opt('site_url'); ?>ask" class="top-askbox" >
                    <input name="title" type="text" value="<?php echo $inquery; ?>">
            </form>
            <script>$("#nosearch").submit();</script>

        <?php }

.......

这是我工作的部分。实际文件太长。

4

2 回答 2

1

你可以使用这样的东西

 if (is in search ) {
 //display result
} else {
  // auto submit below form if above condition is not true ( means if no search result found )

    <form action="to url" id="main" method="post">
    <input name="title" type="text" value="$value" />
    </form>
<script>$("#main").submit();</script>

}
于 2012-10-05T18:53:57.500 回答
0

好的,让我们再试一次。我的论点是,当您可以在 php 中轻松处理表单时,让前端自动发布表单是荒谬的......

if (count($results))
    $qa_content['title']=qa_lang_html_sub('main/results_for_x', qa_html($inquery));
else { 
    $url = 'http://whatever.com/your-search-file.php';
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($_POST));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
    $result = curl_exec($ch);
    curl_close($ch);

    if($results)
    {
        //echo results or do whatever
    }
    else
    {
        // your auto post failed. deal with it
    }
}

假设 cUrl 是您的选择。如果没有,请使用其他人提到的 jquery 选项。

于 2012-10-05T19:03:16.930 回答