user3329688
user3329688

Reputation: 11

Get the selected value of <select> tag in PHP

This is a simple problem but I don't know why i can't get the value inside my select tag. Maybe because it is inside a div but I need this to filter what should be shown on my form.

<div class="Province" id="provinces">
    <div class="Province1">
        City/Municipality
        <select class="form-control" name="city">
            <option name="" value="">Select City/Municipality</option>
            <option value="CityA">City A</option>
            <option value="CityB">CityB</option>
        </select>
    </div>
    <div class="Province2">
        City/Municipality
        <select class="form-control" name="city">
            <option name="" value="">Select City/Municipality</option>
            <option value="CityA">CityA</option>
            <option value="CityB">CityB</option>
            <option value="CityC">CityC</option>
        </select>
    </div>
</div>

My PHP script is

$City  = $_POST["city"];

There is no problem in getting the other values inside my form upon doing this procedure. This select tag inside a div tag is the only problem. Please help. Thanks.

Upvotes: 0

Views: 3273

Answers (3)

niteshSrivastava
niteshSrivastava

Reputation: 166

You have defined two select elements with same name. `

                <div class="Province1">
                    City/Municipality
                    <select class = "form-control" name="cityOne">
                        <option name="" value="">Select City/Municipality</option>
                        <option value="CityA">City A</option>
                        <option value="CityB">CityB</option>

                    </select>
                </div>
                <div class="Province2">
                    City/Municipality
                    <select class = "form-control" name="cityTwo">
                        <option name="" value="">Select City/Municipality</option>
                        <option value="CityA">CityA</option>
                        <option value="CityB">CityB</option>
                        <option value="CityC">CityC</option>


                    </select>
                </div>
        </div>

`

now use $CityOne = $_POST["cityOne"];and $CityTwo = $_POST["cityTwo"];

Upvotes: 2

Jason OOO
Jason OOO

Reputation: 3552

In Addition to John's Answer you can also define your form controls outside your form tag, for example:

<form action="" id="myform">
  Test input: <input type="text" name="test1"><br>
  <input type="submit" value="Submit">
</form>
<select name="filter1" class = "form-control" name="city" form="myform">
   <option name="" value="">Select City/Municipality</option>
   <option value="CityA">CityA</option>
   <option value="CityB">CityB</option>
   <option value="CityC">CityC</option>
</select>

here we defined form="myform" inside select element which is a reference to the form id.

Upvotes: 0

John Conde
John Conde

Reputation: 219924

You have two form elements with the same name. As a result you will only get one value and it probably isn't going to be the one you want. Change it so those select elements have unique names and that will solve your problem.

Upvotes: 3

Related Questions