2017-04-21 79 views
0

我不想要一個按鈕來提交我的下拉列表中的信息,所以我認爲onchange將是提交它的最佳方式。但是當我使用href而不是value時,我不知道如何實現它。有沒有辦法做到這一點?無選擇從html提交選擇,但使用href和不值?

我有這樣的HTML:

<a href='index.php'>Home </a>| 
    <a href='index.php?mode=customers'>List of Customers </a>| 
    <a href='index.php?mode=movieList&genre=Drama'>Drama Movies </a>| 
    <a href='index.php?mode=movieList&genre=Adventure'>Adventure Movies </a>| 
    <select> 
      <option disabled selected>Popular Movies</option> 
      <option><a href='index.php?mode=favoriteGenre&genre=Adventure'>Adventure</a> 
      <option><a href='index.php?mode=favoriteGenre&genre=Romance'>Romance</a> 
      <option><a href='index.php?mode=favoriteGenre&genre=Drama'>Drama</a> 
      <option><a href='index.php?mode=favoriteGenre&genre=Comedy'>Comedy</a> 
      <option><a href='index.php?mode=favoriteGenre&genre=Sci-Fi'>Sci-Fi</a> 
    </select> 

,然後就在下拉列表中相應的PHP:

case "favoriteGenre" : 

     $genre = (isset($_GET['genre'])) ? $_GET['genre'] : -1; 
     // step 1: Define SQL Statement 
     $sql = "SELECT first_name, last_name, title, `movies`.type, date_out FROM `movies`,`customers` WHERE `movies`.type='".$genre."'"; 
     // obtain data 
     $movieInfo = getAll($sql); 
     // define column labels 
     $movie_labels = array('First Name', 'Last Name', 'Title', 'Genre', 'Date Out'); 
     echo "All instances of rental where the genre is " . $genre . ":"; 
     // display data 
     displayTable($movieInfo, $movie_labels); 
     break; 

而且,如果這個問題在其它地方得到回答,請只點我在正確的方向。到目前爲止,我還沒能找到它。謝謝!

+0

這是一個很好的解決方案:http://stackoverflow.com/a/2000689 – Brian

+0

如果我將它改爲:會GET仍然工作得到模式和流派?如果選擇該選項並且不使用按鈕,這將如何幫助提交進行? – RachaelTheBlonde

回答

1

而是保持裏面的鏈接選項,將值分配給選擇這樣的 -

<a href='index.php'>Home |</a> 
<a href='index.php?mode=customers'>List of Customers |</a> 
<a href='index.php?mode=movieList&genre=Drama'>Drama Movies |</a> 
<a href='index.php?mode=movieList&genre=Adventure'>Adventure Movies |</a> 
<select id="movieOptions"> 
     <option disabled selected>Popular Movies</option> 
     <option value="mode=favoriteGenre&genre=Adventure">Adventure</option> 
     <option value="mode=favoriteGenre&genre=Romance">Romance</option> 
     <option value="mode=favoriteGenre&genre=Drama">Drama</option> 
     <option value="mode=favoriteGenre&genre=Comedy">Comedy</option> 
     <option value="mode=favoriteGenre&genre=Sci-Fi">Sci-Fi</option> 
</select> 

<script> 
    $("#movieOptions").change(function() { 
     var goTo = this.value; 
     console.log(goTo); 
     window.location.href = "index.php?" + goTo; 
    }); 
</script> 
+0

謝謝!這工作! – RachaelTheBlonde