Miori Miori - 1 year ago 43
HTML Question

Get the value of an element in HTML using PHP

Is it possible to get the value from a "select" and use it in php? Moreover, the two "select" are in the same form.

Here is my Car Brand and Model Form




Here's some of my mdata_car_view.php file:



<form role="form" method="post">
<div class="form-group">
<label>Brand</label>
<select class="select2 form-control" id="brand2">
<?php
$arr = array();
foreach ($table_list as $t_key) {
$arr[] = $t_key->brand;
}
$unique_data = array_unique($arr);
// now use foreach loop on unique data
foreach($unique_data as $val) {
echo "<option value='" . $val . "'>" . $val . "</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Model</label>
<select class="select2 form-control" id="model2">
</select>
</div>
<div>
<button class="btn btn-sm btn-primary pull-right m-t-n-xs" type="submit"><strong>Save Changes</strong></button>
</div>
</form>


I wanted to try something like this, but I don't know how to get the value in PHP:



<label>Model</label>
<select class="select2 form-control" id="model2">
<?php
foreach ($table_list as $t_key) {
if($t_key->brand == VALUE OF SELECT BRAND HERE--) {
echo "<option value='" . $val . "'>" . $val . "</option>";
}
?>

</select>

Answer Source

Is it possible to get the value from a "select" and use it in php?

Yes, and it is the same way you get it from any other form control.

  1. Give the select a name attribute
  2. Submit the form
  3. Read the value from $_GET or $_POST using the name you gave it
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download