简体   繁体   中英

Select table in db based on value of select tag

I'm trying to populate a second dropdown list (travelprogram-list) based on the value input to a first dropdown list (travelcategory-list). The table containing the data for the second dropdown is different depending on the value selected in the first. Portions of code follow. Currently, the second dropdown never gets populated. When troubleshooting by trying localhost/php?choice="any_value_at_all" in browser, I discovered it is always choosing the first "if" selection in my php code (verified because I see the list echoed to the screen). If you can't already tell, I have near 0 programming experience so be gentle. ;-) THANK YOU!

JS:

<script>
  $("#travelcategory-list").change(function() {
    $("#travelprogram-list").load("connmysql.php?choice=" + $("#travelcategory-list").val());
   });
</script>

PHP:

<?php
  $dbHostname = "localhost";
  $dbUsername = "root";
  $dbPassword = "root";
  $dbName = "mhgtraveldb";

  $conn=mysqli_connect($dbHostname, $dbUsername, $dbPassword, $dbName);
  if(!$conn)
    {
      die('Connect Error');
    }

  $choice = $_GET['choice'];

  if ($choice="CreditCard")
    {
      $query = "SELECT card_travel_program_name FROM card_travel_program";
      $result = mysqli_query($conn, $query);

      while ($row=mysqli_fetch_array($result))
        {
          echo "<option>" . $row{'card_travel_program_name'} . "</option>";
        }
    }
  elseif ($choice="AirlineMiles")
    {
      $query="SELECT airline_loyalty_program_name FROM airline_loyalty_program";
      $result = mysqli_query($conn, $query);

      while ($row=mysqli_fetch_array($result))
        {
          echo "<option>" . $row{'airline_loyalty_program_name'} . "</option>";
        }
    }

ETC.

As Taplar pointed out, you have to change your IF statements to double or tripple equal signs.

Eg:

if ($choice == "CreditCard")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM