I'm making a website in HTML and PHP.

I want to make a dropdown menu using select and also have an input field as well.

Basically I have a mysql database.

Say a table in it is called Project, with columns ID, Name, Budget, Start_date.

I want a dropdown menu that allows me to select the columns: ID, Name, Budget, Start_date.

After the column is selected, I want to be able to input a value (also might be nice if I can specify number vs text for each different column for input type)

<form action="somepage.php" method="GET">
ID: <input type="text" name="Pid" /><br><br>

And then on somepage.php, I will be doing this:

$myvariable = $_GET['Pid']; 


$sql = "INSERT INTO Project(ID)
VALUES('$myvariable'    ";

I know how to do all this with multiple input fields with one submit button.

But I want to know how to do this with SELECT dropdown.

Is there some way to tell on the somepage.php what dropdown element was selected?

Because I need to know which column in Table Project that $myvar is supposed to be inserted into.

Thanks!

1 Answers

1
Budimir Skrtic On

You can check by name if something was selected in select. Same as input text. I'm giving you an example here, not mentioning that you need to work on security against SQL injection, that is some other topic.

<form action="somepage.php" method="GET">
<select name="first">
 <option value="-1" selected>Select something...</select>
 <option value="first_chice">First choice</select>
</select>

<select name="second">
 <option value="-1" selected>Select something else ...</select>
 <option value="first_chice">Other First choice</select>
</select>
</form>


<?php
//somepage.php

if(isset($_GET['first']) && ($_GET['first'] != "-1")) {
  //You have a selection in first select box
 echo $_GET['first'];
}

if(isset($_GET['second']) && ($_GET['second'] != "-1")) {
  //You have a selection in first select box
  echo $_GET['second'];
}
?>