2013-02-04 48 views
0

我有這個javascript顯示一個div,如果用戶點擊我的搜索表單中的文本區域。每個用戶會話運行JavaScript oncer?

基本上在使用

<?php include(); ?> 

當用戶在主頁上,他們單擊文本區域和DIV表演的那一刻搜索表單正在通過對主頁拉,他們然後熄滅文區域(onblur)和div隱藏。這個函數只發生一次,不會再發生。

然而,一旦用戶點擊搜索並提交搜索功能,該功能再次啓動,因此用戶點擊文本區域和div顯示,它們脫落並隱藏。和以前一樣,只發生一次。

我不希望div顯示每次用戶點擊文本字段,我只希望他們看到這個div一次,無論他們在網站上的網頁。

我已將javascript放入search.php文件中,該文件被拉低到主頁上,並放到我網站上的其他所有頁面上。

這可能是爲什麼該功能發生在每一個新頁面上,但我希望用戶能夠從網站的任何位置訪問搜索欄,以及他們是否在home.php開始搜索或profiles.php或任何其他頁面,我希望div只顯示給用戶一次,而不是整個會話或cookie會話。

這是可能的,有人可以告訴我怎麼樣?只有

<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(); 
} 

?> 
+0

所以你想要的JS功能只能使用一次,如果我讀這正確。 – Class

+0

聽起來像一個會話cookie是你在找什麼。 –

+0

創建一個會話布爾變量$ SESSION [「hasSearched」]將其設置爲false,並且當他們執行搜索時將該變量設置爲true。在包含彈出窗口的腳本之前檢查布爾。不確定你是如何做搜索和包括腳本,但聽起來像那會工作? –

回答

2

這並不理想,但我建議設置一個$_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> 

搜索腳本想要執行一次)。然後,在包含JavaScript之前,檢查是否設置了該會話變量。如果是這樣,請不要包含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; ?> 
+0

嘗試這樣做,但它保持div易裂變,並不會消失,當用戶點擊文本區域。它也可以從頁面加載中看到,並且只能在用戶單擊文本區域時纔可見。 –