2015-09-06 42 views
0

我創建了一個類似於谷歌搜索使用JQuery的即時搜索。突出顯示的代碼不起作用。這是奇怪的,因爲他們自己的工作很好,其他一切正常工作。任何想法爲什麼發生這種情況? Q1。 searchq()工作正常,但createq()函數不起作用,變量txt可以發佈到其他文件(search.php)。但是,函數createq()不能POST。它在測試後得到了全局變量txt,但是無論使用什麼POST方法,php文件(create_object.php)都無法得到它。任何人都可以幫助寫一些可以在我的代碼中工作的POST代碼。Javascript兩個奇怪的問題:POST不工作,window.location.href不工作

Q2 我想創建一個函數,當按下回車鍵時,用戶將被重定向到第一個搜索結果(用一個url來錨定)。爲了達到這個目的,我創建了一個函數,讓變量redirectUrl將錨定的url作爲字符串獲取,但是,重定向函數window.location.href不起作用,頁面只是刷新了。我通過自己在另一個文件中測試了window.location.href函數,它雖然工作。這很奇怪,我的網頁只是刷新,它甚至刷新,當我直接到谷歌。 window.location.href( 「www.google.com」)。

請注意,我沒有在這裏包括連接到數據庫功能。因爲我認爲數據庫的用戶名和密碼設置與您的不同,所以如果您想測試它,請自行創建。 mysql被設置爲一個名爲「objects」的表,並且它有一個名爲「name」的列。

在此先感謝!

<html> 
    <!-- google API reference --> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <!-- my own script for search function --> 

    <center> 
    <form method="POST"> 
     <input type="text" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();"> 
     <div id="output"> 
     </div> 
    </form> 
    </center> 

     <!-- instant search function --> 
<script type="text/javascript"> 

function searchq(){ 
     // get the value 
      var txt = $("input").val(); 
      // post the value 
      if(txt){ 
       $.post("search.php", {searchVal: txt}, function(result){ 
        $("#search_output").html(result+"<div id=\"create\" onclick=\"creatq()\"><br>Not found above? Create.</div>"); 
       }); 
      } 
      else{ 
       $("#search_output").html(""); 
      } 


     }; 
function createq(){ 
    // allert for test purpose: test if the txt has got by the createq function 
    alert(txt); 
    **$.post("create_object.php",{creatVal:txt});** 

} 

// if enter key pressed, redirect page to the first search result 
$("#search").keypress(function(evt){ 
    if (evt.which == 13) { 
     // find the first search result in DOM and trigger a click event 
     var redirectUrl = $('#search_output').find('a').first().attr('href'); 
     alert(redirectUrl); 
     **window.location.href = "www.google.com"; 
window.location.href = "www.google.com";** 
    } 
}) 

</script> 
    </html> 

PHP文件(的search.php)

<?php 
if(isset($_POST["searchVal"])){ 
    //get the search 
    $search=$_POST["searchVal"]; 
    //sort the search 
    $search=preg_replace("#[^0-9a-z]#i","",$search); 
    //query the search 
    echo "<br/>SELECT * from objects WHERE name LIKE '%$search%'<br/>"; 
    $query=mysqli_query($conn,"SELECT * from objects WHERE name LIKE '%$search%'") or die("could not search!"); 
    $count=mysqli_num_rows($query); 
    //sort the result 
    if($count==0){ 
     $output="there was no search result"; 
    } 
    else{ 
     while($row=mysqli_fetch_assoc($query)){ 

      $object_name=$row["name"]; 

      $output.="<div><a href='##'>".$object_name."</a></div>"; 
     } 
    } 
    echo $output; 
} 
?> 

php文件(create_object。PHP)

<?php 
    if(isset($_POST["createVal"])){ 
     $name=$_POST["createVal"]; 
     var_dump($name); 

    } 

?> 
+0

你應該分開這兩個問題分成兩個不同的職位。他們不太相關,回答問題的人不會得到完全的批准,這會使人們迴避你的問題。只是一個友好的領導。 'creatq()'工作的 – OneRaynyDay

+0

,看看http://api.jquery.com/on/ –

回答

0

嘗試將輸入與ID綁定

var txt = $("input").val(); 

<input type="text" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();"> 

更改上面這個

var txt = $("#searchinput").val(); 

<input type="text" id="searchinput" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();"> 

,我認爲你正試圖在這裏展示搜索結果

<div id="output"></div> 

和jQuery綁定是th在代碼

$("#search_output").html(""); 

的HTML所以改變這種

<div id="search_output"></div> 

也該在我們的代碼

$("#search").keypress(function(evt){ 

沒有HTML元素綁定吧,我想你想將其與搜索輸入進行綁定,以便將其更改爲此

$("#searchinput").keypress(function(evt){ 

以上更改也應解決window.location.href not working問題

因此,HTML將是;

<form method="POST"> 
    <input type="text" id="searchinput" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();"> 
    <div id="search_output"></div> 
</form> 

和腳本將被

<script type="text/javascript"> 
function searchq(){ 
    // get the value 
    var txt = $("#searchinput").val(); 
    // post the value 
    if(txt){ 
     $.post("search.php", {searchVal: txt}, function(result){ 
      $("#search_output").html(result+"<div id=\"create\" onclick=\"creatq()\"><br>Not found above? Create.</div>"); 
     }); 
    } 
    else{ 
     $("#search_output").html(""); 
    } 
} 

function createq(){ 
// allert for test purpose: test if the txt has got by the createq function 
    alert(txt); 
    **$.post("create_object.php",{creatVal:txt});** 
} 

// if enter key pressed, redirect page to the first search result 
$("#searchinput").keypress(function(evt){ 
     if (evt.which == 13) { 
     // find the first search result in DOM and trigger a click event 
     var redirectUrl = $('#search_output').find('a').first().attr('href'); 
     alert(redirectUrl); 
     **window.location.href = "www.google.com"; 
     window.location.href = "www.google.com";** 
    } 
}); 
</script> 

注:如果您檢查瀏覽器控制檯,您可能會看到一些錯誤,還有一些錯誤,錯字缺少像你的JS ;了。

在PHP中,這裏

if($count==0){ 
    $output="there was no search result"; 
} 
else{ 
    while($row=mysqli_fetch_assoc($query)){ 
     $object_name=$row["name"]; 
     $output.="<div><a href='##'>".$object_name."</a></div>"; 
    } 
} 

$output.是錯的點,所以將其更改爲以下

if($count==0){ 
    $output="there was no search result"; 
} 
else{ 
    while($row=mysqli_fetch_assoc($query)){ 
     $object_name=$row["name"]; 
     $output="<div><a href='#'>".$object_name."</a></div>"; 
    } 
} 
0

兩件事情:

  1. 輸入搜索ID沒有被定義,$(「#search」).presspress將不起作用。更改爲:

    <輸入類型= 「文本」 名稱= 「搜索」 ID = 「搜索」風格= 「寬度:400像素」 佔位符= 「搜索框」 的onkeyup = 「SEARCHQ();」 >

  2. Div id「output」,應該是「search_output」,如$(「#search_output」)中所要求的那樣。更改爲:

    < DIV ID = 「search_output」> </DIV>