我有这个 javascript,如果用户单击我的搜索表单中的文本区域,它会显示一个 div。
基本上此时搜索表单正在使用
拉到主页<?php include(); ?>
当用户在主页上时,他们单击文本区域并且 div 显示,然后他们离开文本区域 (onblur) 并且 div 隐藏。这个函数只发生一次,不会再发生。
但是,一旦用户点击搜索并提交搜索,该功能就会再次启动,因此用户单击文本区域并且 div 显示,然后它们就会离开它并隐藏。和以前一样,这种情况只发生一次。
我不希望每次用户单击文本字段时都显示该 div,我只希望他们看到该 div 一次,无论他们在网站上的哪个页面上。
我已将 javascript 放入 search.php 文件中,该文件被拉到主页和网站上的所有其他页面上。
这可能就是为什么该功能会在它所访问的每个新页面上发生,但同时我希望用户能够从网站上的任何位置访问搜索栏,以及他们是否在 home.php 或配置文件上开始搜索.php 或任何其他页面,我希望 div 仅向用户显示一次,而不是在整个 session 或 cookie session 中再次显示。
这可能吗,有人可以告诉我怎么做吗?
谢谢
<script>
$(function() {
$(".search_prompt").hide();
var focusin_flag = false,
focusout_flag = false;
$("#text").focusin(function() {
if (!focusin_flag) {
$(".search_prompt").show();
focusin_flag = true;
}
}).focusout(function () {
if (!focusout_flag ) {
$(".search_prompt").hide();
focusout_flag = true;
}
function timeout_init() {
setTimeout('search_prompt()', 2000);
}
});
});
</script>
搜索脚本:
<form method="get" action="">
<input type="hidden" name="dosearch" value="1">
<input type="text" id="text" name="query" class="searchbox" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/search.png" height="19" width="20" class="searchbutton" name="submit" value="Start Search" />
</form>
<?php
//PHP CODE STARTS HERE
if(isset($_GET['dosearch'])){
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="";
$db_tb_atr_name="display_name";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT *,MATCH(display_name, location, sex, ethnicity, hobbies, station, age, weight_st, weight_lb, height_ft, height_in, build) AGAINST ('$query' IN BOOLEAN MODE) AS relevance FROM ptb_stats WHERE MATCH(display_name, location, sex, ethnicity, hobbies, station, age, weight_st, weight_lb, height_ft, height_in, build) AGAINST('$query' IN BOOLEAN MODE) ORDER BY relevance DESC LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<div class=\"text\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
echo "<div class=\"spacing\"><img width=35px height= 30px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> ";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</a></div></div>";
}
echo "<div class=\"morebutton-search\"><a href=\"search_results.php?query=$query\" \">+ view more results</a></div>";
mysql_close();
}
?>
最佳答案
这并不理想,但我建议在用户第一次点击该页面(或执行您只想执行一次的任何操作)时设置一个 $_SESSION
变量。然后,在包含 JavaScript 之前,检查是否设置了该 session 变量。如果是这样,请不要包含 JavaScript。如果没有,请包含它。
它看起来像这样:
// User just performed action for the first time.
$_SESSION['saw_div'] = true;
然后,稍后:
<?php if (!isset($_SESSION['saw_div']) || $_SESSION['saw_div'] == false): ?>
<script>
// Include your JS script
</script>
<?php endif; ?>
关于php - 每个用户 session 运行一次 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14694423/