0

I don't want a button to submit the information from my dropdown list, so I think onchange would be the best way to submit it instead. But I don't know how to implement that when I'm using href and not value. Is there a way to do this?

I have this for the 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>

and then the corresponding php for just the dropdown:

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;

And if this question is answered elsewhere, please just point me in the right direction. I haven't been able to find it anywhere thus far. Thanks!

  • This is a good solution: http://stackoverflow.com/a/2000689 – Brian Apr 21 '17 at 02:47
  • If I changed it to: would GET still work to get the mode and the genre? And how would that help make the submit happen when the option was selected and not using a button? – RachaelTheBlonde Apr 21 '17 at 03:21
  • How's your Jquery, you put it as a tag and it definitely needs it or some good javascript – Forbs Apr 21 '17 at 03:28
  • You will also need to attach an onchange handler to the select input: ` – Brian Apr 21 '17 at 03:43
  • My jquery is not the greatest but I figured it would be necessary to accomplish what I want to do. Is there a way to use jquery and still grab the mode and the genre while also submitting it from clicking and not from a button? – RachaelTheBlonde Apr 21 '17 at 03:47
  • What does the onchange look like from the php side? If I can still get the value of mode and genre separately, that is. Otherwise I can't really use it. – RachaelTheBlonde Apr 21 '17 at 06:01
  • in this case, you can put the onchange handler directly in the select tag (since it's just doing one operation). So, you would replace your ``. This means that whenever the value of the select changes, the window's location should change as well to reflect the new value (which should submit the query parameters to your php). – Brian Apr 22 '17 at 05:37

1 Answers1

1

Instead of keeping links inside options, assign values to options like this -

    <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>
T.Shah
  • 2,768
  • 1
  • 14
  • 13