gallo2000sv
gallo2000sv

Reputation: 121

PHP: $var = $_GET not getting any value from select dropdown

i'm struggling on finding the reason why i can grab the $_GET (or $_POST) in my PHP code. I have a select dropdown populated from a table that works fine. The second part supposes to grab the selected value and use it in a second query but all i get is a blank page. I want to do it only using php.

<?php
$connect = mysqli_connect("localhost","db","pwd");
mysqli_select_db($connect, "db");

?>
<!DOCTYPE html>
<html>
<head>
<title> Title </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="GET">
<Select name = "Paises">
<option selected disabled>Choose an option</option>
<?php
   $r = mysqli_query($connect, "SELECT Pais from Brokers"); // Runing the query.
   while($row = mysqli_fetch_array($r)) 
    {
?>
<option value =""><?php echo $row["Pais"];  ?>   </option>
<?php
}
?>
<input type="submit" name="submit" value="submit" />
</form>
</select>
<?php 
  //Second part of code: Getting the selected value from dropdown menu named Paises
   $selected_pais = $_GET['Paises'];
   //using the value on second query:
   $r2 = mysqli_query($connect, "SELECT BrokeryMercado from Brokers WHERE Pais = '".$selected_pais."'"); // Run the query.

   while($row2 = mysqli_fetch_array($r2)) {
   echo"{$row2['BrokeryMercado']}";

    // I get the same blank page when trying: echo "{$row2["BrokeryMercado"]}"; 
    //same result trying: echo "You have selected :" .$selected_pais; 
    }
  ?>
  </body>
  </html>

It might be something stupid but I keep getting only a blank page. Tried 2 separates files and same results. The url looks like this: myfile.php?Paises=&submit=submit
So I guess there isn't getting any value there. Thanks in advance for any insights.

Upvotes: 0

Views: 213

Answers (2)

Abdel mounaim
Abdel mounaim

Reputation: 1338

You have to specify the value attribute in option tag <option value ="<?= $row["Pais"]; ?>"><?php echo $row["Pais"]; ?> </option>

Upvotes: 0

z3nth10n
z3nth10n

Reputation: 2441

This can be caused because you have an empty 'value' attribute in your option tag and this can be the reason why GET or POST aren't getting anything.

<option value ="">

Try putting a value in it.

Ie, this:

<option value="<?php echo $row["Pais"]; ?>"><?php echo $row["Pais"]; ?></option>

I hope it works, or at least GET or POST gives you something to test with.

Upvotes: 0

Related Questions