How can I create an if you can change the value of a select by pressing a button?

-1

How can I create an if you can change the value of a select when you press a button?

Example:

SELECT * FROM pais  WHERE ciudad ='$valor-a-camiar-con-if'";

I need it to be with an if. Is it possible?

This would be for a project with php and html

    
asked by Sandro Rosario 17.05.2018 в 20:54
source

2 answers

1

As I see what you want to do is that by pressing a button carry an information either by GET or by POST to a PHP script.

There what you should do is the following

if($ciudad == 1){
  $ciudad = "xxx";
}
if($ciudad == 2){
  $ciudad = "yyy";
}
else{
$ciudad = "zzz";
}

Then in the query:

SELECT * FROM pais WHERE ciudad ='$ciudad';
    
answered by 17.05.2018 в 22:33
0
  

You can get it by using a if/else if/else which will   will help first to verify the value of the variable that is   arriving so that later you pass said variable to the body of the   consult and execute the same

if it is by the method GET;

$ciudad = $_GET['ciudad'];

or if it is by the method POST;

$ciudad = $_POST['ciudad'];

if($ciudad === 1){
  $ciudad = 'Mexico';
  $query = "SELECT * FROM pais WHERE ciudad = .'$ciudad'."
}else if($ciudad === 2){
  $ciudad = 'Bogota';
  $query = "SELECT * FROM pais WHERE ciudad = .'$ciudad'."
}else{
  $ciudad = 'Madrid';
  $query = "SELECT * FROM pais WHERE ciudad = .'$ciudad'."
}
  

You can extend this chain of if, else if as many times as   you need

    
answered by 18.05.2018 в 05:19