I'm sure the answer to this is easy, but I'm a php/javascript noob, hence my question. The first three pieces of code, ending with "echo $_POST['new_search_text'];", works visually perfectly, but now I want to figure out how to get the input entered in the first section's form passed and used for the search criteria on my search page (the search page code starts with "{include file='header.tpl'}" and ends with "{include file='footer.tpl'}"). I'd like it so the info passed automatically searches on the search page as if I had typed it there directly and hit "search" and then have it display the results on the search page. I'm using smarty templates, and have also included the back bone PHP code from my search page (starting with "$page = "search";"). I can't use Jquery as it interferes with other code, but I imagine the solution is rather straightforward.
我确定答案很简单,但我是一个php / javascript noob,因此我的问题。前三段代码以“echo $ _POST ['new_search_text'];”结尾,视觉上完美,但现在我想弄清楚如何在第一部分的表单中输入输入并用于搜索条件在我的搜索页面上(搜索页面代码以“{include file ='header.tpl'}”开头,以“{include file ='footer.tpl'}”结尾。我喜欢它,所以传递的信息会自动在搜索页面上搜索,就像我直接在那里输入并点击“搜索”然后让它在搜索页面上显示结果一样。我正在使用智能模板,并且还包含了我的搜索页面中的骨骼PHP代码(以“$ page =”search“;”开头)。我不能使用Jquery,因为它会干扰其他代码,但我认为解决方案相当简单。
Thanks!!
<form id="searchForm" action="./search.php" method="post">
<input type="text" name="new_search_text" value="">
<input type="submit" value="Search">
<input type="submit" value="Cancel" onclick="showSearchLink();return false;">
</form>
<a id="searchLink" class="button_link_main" href="./search.php" onclick="showSearchForm();return false;">Link</a>
function showSearchForm() {
// show the form by setting style="display:inline"
document.getElementById('searchForm').style.display = 'inline';
// hide the link by setting style="display:none"
document.getElementById('searchLink').style.display = 'none';
}
function showSearchLink() {
// hide the form
document.getElementById('searchForm').style.display = 'none';
// show the link
document.getElementById('searchLink').style.display = 'inline';
}
<?php
echo $_POST['new_search_text'];
?>
{include file='header.tpl'}
<img src='./images/icons/search48.gif' border='0' class='icon_big'>
<div class='page_header'>Search Avulsion Net</div>
<div>{lang_print id=924}</div>
<br />
<br />
<form action='search.php' name='search_form' method='post'>
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td class='search'>
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td>{lang_print id=925}</td>
<td> <input style='background-color:#CCCCCC' type='text' size='30' class='text' name='search_text' id='search_text' value='{$search_text}' maxlength='100'></td>
<td>
<input type='submit' class='button' value='{lang_print id=646}'>
<input type='hidden' name='task' value='dosearch'>
<input type='hidden' name='t' value='0'>
</td>
</tr>
<tr>
<td> </td>
<td colspan='2'> <b>-=<a href='search_advanced.php'>Browse Members</a>=-</b></td>
</tr>
</table>
</div>
</form>
</td>
</tr>
</table>
<br>
{if $search_text != ""}
{if $is_results == 0}
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td class='result'>
<img src='./images/icons/bulb16.gif' class='icon'>
{lang_sprintf id=927 1=$search_text}
</td>
</tr>
</table>
{else}
{* SHOW DIFFERENT RESULT TOTALS *}
<table class='tabs' cellpadding='0' cellspacing='0'>
<tr>
<td class='tab0'> </td>
{section name=search_loop loop=$search_objects}
<td class='tab{if $t == $search_objects[search_loop].search_type}1{else}2{/if}' NOWRAP>{if $search_objects[search_loop].search_total == 0}{lang_sprintf id=$search_objects[search_loop].search_lang 1=$search_objects[search_loop].search_total}{else}<a href='search.php?task=dosearch&search_text={$url_search}&t={$search_objects[search_loop].search_type}'>{lang_sprintf id=$search_objects[search_loop].search_lang 1=$search_objects[search_loop].search_total}</a>{/if}</td>
<td class='tab'> </td>
{/section}
<td class='tab3'> </td>
</tr>
</table>
<div class='search_results'>
{* SHOW PAGES *}
{if $p != 1}<a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p-1' p=$p}'>« {lang_print id=182}</a> | {/if}
{if $p_start == $p_end}
<b>{lang_sprintf id=184 1=$p_start 2=$total_results}</b> ({lang_sprintf id=928 1=$search_time})
{else}
<b>{lang_sprintf id=185 1=$p_start 2=$p_end 3=$total_results}</b> ({lang_sprintf id=928 1=$search_time})
{/if}
{if $p != $maxpage} | <a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p+1' p=$p}'>{lang_print id=183} »</a>{/if}
<br><br>
{* SHOW RESULTS *}
{section name=result_loop loop=$results}
<div class='search_result{cycle name="class_name" values="1,2,2,1"}' style='width: 400px; float: left; border: 1px solid #CCCCCC; margin: 5px;'>
<table cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' style='padding-right: 4px;'>
<a href="{$results[result_loop].result_url}" class="title"><img src='{$results[result_loop].result_icon}' class='photo' width='60' height='60' border='0'></a>
</td>
<td valign='top'>
<div class='search_result_text'>
{capture assign='result_title'}{lang_sprintf id=$results[result_loop].result_name 1=$results[result_loop].result_name_1}{/capture}
<a href="{$results[result_loop].result_url}" class="title">{$result_title|truncate:40:"...":true}</a>
<div class='search_result_text2'>{lang_sprintf id=$results[result_loop].result_desc 1=$results[result_loop].result_desc_1 2=$results[result_loop].result_desc_2 3=$results[result_loop].result_desc_3}</div>
{if $results[result_loop].result_online == 1}<div style='margin-top: 5px;'><img src='./images/icons/online16.gif' border='0' class='icon'>{lang_print id=929}</div>{/if}
</div>
</td>
</tr>
</table>
</div>
{cycle name="clear_cycle" values=",<div style='clear: both; height: 0px;'></div>"}
{/section}
<div style='clear:both;'></div><br />
{* SHOW PAGES *}
{if $p != 1}<a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p-1' p=$p}'>« {lang_print id=182}</a> | {/if}
{if $p_start == $p_end}
<b>{lang_sprintf id=184 1=$p_start 2=$total_results}</b> ({lang_sprintf id=928 1=$search_time})
{else}
<b>{lang_sprintf id=185 1=$p_start 2=$p_end 3=$total_results}</b> ({lang_sprintf id=928 1=$search_time})
{/if}
{if $p != $maxpage} | <a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p+1' p=$p}'>{lang_print id=183} »</a>{/if}
</div>
{/if}
{/if}
{* JAVASCRIPT TO AUTOFOCUS ON SEARCH FIELD *}
{literal}
<script type="text/javascript">
<!--
window.addEvent('load', function(){ $('search_text').focus(); });
//-->
</script>
{/literal}
{include file='footer.tpl'}
<?php
$page = "search";
include "header.php";
// DISPLAY ERROR PAGE IF USER IS NOT LOGGED IN AND ADMIN SETTING REQUIRES REGISTRATION
if($user->user_exists == 0 && $setting['setting_permission_search'] == 0)
{
$page = "error";
$smarty->assign('error_header', 639);
$smarty->assign('error_message', 656);
$smarty->assign('error_submit', 641);
include "footer.php";
}
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if(isset($_POST['p'])) { $p = (int) $_POST['p']; } elseif(isset($_GET['p'])) { $p = (int) $_GET['p']; } else { $p = 1; }
if(isset($_POST['search_text'])) { $search_text = $_POST['search_text']; } elseif(isset($_GET['search_text'])) { $search_text = $_GET['search_text']; } else { $search_text = ""; }
if(isset($_POST['t'])) { $t = $_POST['t']; } elseif(isset($_GET['t'])) { $t = $_GET['t']; } else { $t = 0; }
// SET VARS
$results_per_page = 20;
$results = Array();
$total_results = 0;
$is_results = 0;
$object_count = 0;
$search_objects = Array();
$is_next_page = 0;
if($p < 1) { $p = 1; }
// DO SEARCH
if($task == "dosearch" && $search_text != "")
{
// START SEARCH TIMER
$start_timer = getmicrotime();
// SEARCH PROFILES
search_profile();
// CALL SEARCH HOOK
($hook = SE_Hook::exists('se_search_do')) ? SE_Hook::call($hook, array()) : NULL;
// GET GRAND TOTAL RESULTS
for($r=0;$r<count($search_objects);$r++)
{
if($search_objects[$r][search_total] != 0)
{
if($total_results == 0) { header("Location: search.php?task=dosearch&search_text=".urlencode($search_text)."&t=".$search_objects[$r]['search_type']); exit(); }
$is_results = 1;
}
}
// END TIMER
$end_timer = getmicrotime();
$search_time = round($end_timer - $start_timer, 3);
// CHECK TO SEE IF THERE IS A "NEXT PAGE"
if(count($results) > $results_per_page)
{
$is_next_page = 1;
while(count($results) > $results_per_page)
{
array_pop($results);
}
}
// IF TOTAL RESULTS IS MORE THAN 200, CHANGE TO 200+
if($total_results > 200)
{
if($is_next_page == 1) { $maxpage = $p+1; } else { $maxpage = $p; }
$total_results = "200+";
}
else
{
if(($total_results % $results_per_page) != 0) { $maxpage = ($total_results) / $results_per_page + 1; } else { $maxpage = ($total_results) / $results_per_page; }
$maxpage = (int) $maxpage;
}
// IF RESULTS IS EMPTY AND PAGE ISN'T 1, DISPLAY NOTHING
if(count($results) == 0 && $p != 1) { $search_text = ""; }
}
// SET THE GLOBAL PAGE TITLE
$global_page_title[0] = 646;
$global_page_description[0] = 924;
// ASSIGN SMARTY VARIABLES AND INCLUDE FOOTER
$smarty->assign('search_text', $search_text);
$smarty->assign('url_search', urlencode($search_text));
$smarty->assign('is_results', $is_results);
$smarty->assign('results', $results);
$smarty->assign('total_results', $total_results);
$smarty->assign('search_objects', $search_objects);
$smarty->assign('search_time', $search_time);
$smarty->assign('maxpage', $maxpage);
$smarty->assign('t', $t);
$smarty->assign('p', $p);
$smarty->assign('p_start', (($p-1)*$results_per_page)+1);
$smarty->assign('p_end', (($p-1)*$results_per_page)+count($results));
include "footer.php";
?>
3 个解决方案
#1
0
Did you try and search for the answer first?
您是否尝试过首先搜索答案?
This should help you...
这应该对你有所帮助......
Link to your question that has already been answered
链接到您已回答的问题
#2
0
There are probably several approaches you could take to accomplish this. One way would be to write a javascript function which checks for the presence of the POST var 'new_search_text' and then programmatically puts this string in the search box and submits the form if found.
您可以采取几种方法来实现这一目标。一种方法是编写一个javascript函数,它检查POST var'new_search_text'的存在,然后以编程方式将此字符串放在搜索框中,并在找到时提交表单。
<script>
function checkForAutoSearch()
{
var searchString = <?php echo (!empty($_POST['new_search_term'])) ? $_POST['new_search_term'] : ''; ?>;
if (searchString)
{
// put searchString into the second search field
document.getElementById('search_text').value = searchString;
// up in your search form, assign the form tag an id first: id="search_form"
// then submit the form:
document.getElementById('search_form').submit();
}
}
// now fire this function whenever the page loads
window.onload = checkForAutoSearch;
</script>
On a side note, you mentioned that you can't use jQuery but down in the js code to set auto focus to the search field it appears you are using jQuery. You may need to lookup the straight javascript way of setting focus if you need to remove jQuery.
在旁注中,你提到你不能使用jQuery但是在js代码中将自动焦点设置为你正在使用jQuery的搜索字段。如果需要删除jQuery,您可能需要查找设置焦点的直接javascript方式。
#3
0
@john-mcmahon Okay here is the code I put in search2.tpl (I use Smarty templates). The second code bit is the search form on page ONE, and the third code bit, the associated search form from page TWO. It doesn't error out, and goes to the search page when I type in a search query and click submit, but it doesn't search for the entered text or do anything except maybe refresh itself.... Any ideas?
@ john-mcmahon好的,这是我在search2.tpl中放入的代码(我使用Smarty模板)。第二个代码位是第一页上的搜索表单,第三个代码位是第二页的相关搜索表单。它不会出错,当我输入搜索查询并点击提交时会进入搜索页面,但它不会搜索输入的文本或做任何事情,除非刷新自己....任何想法?
<script type="text/javascript">
<!--
function checkForAutoSearch()
{
var searchString = <?php echo (!empty($_POST['new_search_text'])) ? $_POST['new_search_text'] : ''; ?>;
if (searchString)
{
// put searchString into the second search field
document.getElementById('search_text').value = searchString;
// up in your search form, assign the form tag an id first: id="search_form_new"
// then submit the form:
document.getElementById('search_form_new').submit();
}
}
// now fire this function whenever the page loads
window.onload = checkForAutoSearch;
//-->
</script>
<form id="searchForm_newsearch" action="./search2.php" method="post"><input type="text" name="new_search_text" value=""><input type="submit" value="Search"><input type="submit" value="Cancel" onclick="showSearchLink();return false;"></form><a id="searchLink" class="button_link_main" href="./search2.php" onclick="showSearchForm();return false;"><img src="/images/search.png" onmouseover="this.src='/images/searchsite_hover.png'" onmouseout="this.src='/images/search.png'" /></a>
<form id="search_form_new" action='search2.php' name='search_form' method='post'>
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td class='search'>
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td>{lang_print id=925}</td>
<td> <input style='background-color:#CCCCCC' type='text' size='30' class='text' name='search_text' id='search_text' value='{$search_text}' maxlength='100'></td>
<td>
<input type='submit' class='button' value='{lang_print id=646}'>
<input type='hidden' name='task' value='dosearch'>
<input type='hidden' name='t' value='0'>
</td>
</tr>
<tr>
<td> </td>
<td colspan='2'> <b>-=<a href='search_advanced.php'>Browse Members</a>=-</b></td>
</tr>
</table>
</div>
</form>
#1
0
Did you try and search for the answer first?
您是否尝试过首先搜索答案?
This should help you...
这应该对你有所帮助......
Link to your question that has already been answered
链接到您已回答的问题
#2
0
There are probably several approaches you could take to accomplish this. One way would be to write a javascript function which checks for the presence of the POST var 'new_search_text' and then programmatically puts this string in the search box and submits the form if found.
您可以采取几种方法来实现这一目标。一种方法是编写一个javascript函数,它检查POST var'new_search_text'的存在,然后以编程方式将此字符串放在搜索框中,并在找到时提交表单。
<script>
function checkForAutoSearch()
{
var searchString = <?php echo (!empty($_POST['new_search_term'])) ? $_POST['new_search_term'] : ''; ?>;
if (searchString)
{
// put searchString into the second search field
document.getElementById('search_text').value = searchString;
// up in your search form, assign the form tag an id first: id="search_form"
// then submit the form:
document.getElementById('search_form').submit();
}
}
// now fire this function whenever the page loads
window.onload = checkForAutoSearch;
</script>
On a side note, you mentioned that you can't use jQuery but down in the js code to set auto focus to the search field it appears you are using jQuery. You may need to lookup the straight javascript way of setting focus if you need to remove jQuery.
在旁注中,你提到你不能使用jQuery但是在js代码中将自动焦点设置为你正在使用jQuery的搜索字段。如果需要删除jQuery,您可能需要查找设置焦点的直接javascript方式。
#3
0
@john-mcmahon Okay here is the code I put in search2.tpl (I use Smarty templates). The second code bit is the search form on page ONE, and the third code bit, the associated search form from page TWO. It doesn't error out, and goes to the search page when I type in a search query and click submit, but it doesn't search for the entered text or do anything except maybe refresh itself.... Any ideas?
@ john-mcmahon好的,这是我在search2.tpl中放入的代码(我使用Smarty模板)。第二个代码位是第一页上的搜索表单,第三个代码位是第二页的相关搜索表单。它不会出错,当我输入搜索查询并点击提交时会进入搜索页面,但它不会搜索输入的文本或做任何事情,除非刷新自己....任何想法?
<script type="text/javascript">
<!--
function checkForAutoSearch()
{
var searchString = <?php echo (!empty($_POST['new_search_text'])) ? $_POST['new_search_text'] : ''; ?>;
if (searchString)
{
// put searchString into the second search field
document.getElementById('search_text').value = searchString;
// up in your search form, assign the form tag an id first: id="search_form_new"
// then submit the form:
document.getElementById('search_form_new').submit();
}
}
// now fire this function whenever the page loads
window.onload = checkForAutoSearch;
//-->
</script>
<form id="searchForm_newsearch" action="./search2.php" method="post"><input type="text" name="new_search_text" value=""><input type="submit" value="Search"><input type="submit" value="Cancel" onclick="showSearchLink();return false;"></form><a id="searchLink" class="button_link_main" href="./search2.php" onclick="showSearchForm();return false;"><img src="/images/search.png" onmouseover="this.src='/images/searchsite_hover.png'" onmouseout="this.src='/images/search.png'" /></a>
<form id="search_form_new" action='search2.php' name='search_form' method='post'>
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td class='search'>
<table cellpadding='0' cellspacing='0' align='center'>
<tr>
<td>{lang_print id=925}</td>
<td> <input style='background-color:#CCCCCC' type='text' size='30' class='text' name='search_text' id='search_text' value='{$search_text}' maxlength='100'></td>
<td>
<input type='submit' class='button' value='{lang_print id=646}'>
<input type='hidden' name='task' value='dosearch'>
<input type='hidden' name='t' value='0'>
</td>
</tr>
<tr>
<td> </td>
<td colspan='2'> <b>-=<a href='search_advanced.php'>Browse Members</a>=-</b></td>
</tr>
</table>
</div>
</form>