Blueblazer172
Blueblazer172

Reputation: 600

How to fetch data from an select field in php and load into database?

How can i fetch the data from the option-field and load the chosen option-field into the database ?

<div class="form-group col-md-5">
                <label class="control-label">Date of birth:*</label>
                <div class="form-group">
                    <div class="col-md-4">
                        <select type="text" class="form-control" id="day" >
                            <option>--Day--</option>
                            <?php
                               $day = array(range(1,31));
                                for ($i = 1; $i <= 31; $i++) {
                                    echo "<option name='day'>".$i."</option>";
                                }
                            ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <select type="text" class="form-control" id="month">
                            <option>--Month--</option>
                            <?php
                                for ($i = 1; $i <= 12; $i++) {
                                    echo "<option name='month'>".$i."</option>";
                                }
                            ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <select type="text" class="form-control" id="year">
                            <option>--Year--</option>
                            <?php
                                for ($i = 1900; $i <= 2016; $i++) {
                                    echo "<option name='year'>".$i."</option>";
                                }
                            ?>
                        </select>
                    </div>
                </div>
            </div>

I know i can use https://eonasdan.github.io/bootstrap-datetimepicker/ cause im using bootstrap. but there it's the same. i dont know how to fetch the choosen data.

Upvotes: 2

Views: 2144

Answers (4)

Alok Patel
Alok Patel

Reputation: 8022

You need to change two things here.

First, give name to the <select> instead of <option>.

Second set the value attribute of the <option> to get the value of selected option.

So the code for selection box you've written will look something like this,

<select type="text" class="form-control" id="year" name="year">
    <option>--Year--</option>
    <?php
        for ($i = 1900; $i <= 2016; $i++) {
            echo "<option value='".$i."'>".$i."</option>";
        }
    ?>
</select>

Now if the form method is POST access the year value in php script using $_POST and if it's GET then access it using $_GET.

Upvotes: 2

S.I.
S.I.

Reputation: 3375

I don't know your server side part of code but here you missing values on option fields. This value is what you save in database. So if everything else is okay with your code you should make something like this:

            <label class="control-label">Date of birth:*</label>
            <div class="form-group">
                <div class="col-md-4">
                    <select name="day" type="text" class="form-control" id="day" >
                        <option>--Day--</option>
                        <?php
                           $day = array(range(1,31));
                            for ($dayOfBirth = 1; $dayOfBirth <= 31; $dayOfBirth++) {
                                echo '<option  value="'.$dayOfBirth.'">"'.$dayOfBirth.'"</option>';
                            }
                        ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <select name="month" type="text" class="form-control" id="month">
                        <option>--Month--</option>
                        <?php
                            for ($monthOfBirth = 1; $monthOfBirth <= 12; $monthOfBirth++) {
                                echo '<option  value="'.$monthOfBirth.'">"'.$monthOfBirth.'"</option>';
                            }
                        ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <select name="year" type="text" class="form-control" id="year">
                        <option>--Year--</option>
                        <?php
                            for ($yearOfBirth = 1900; $yearOfBirth <= 2016; $yearOfBirth++) {
                                echo '<option  value="'.$yearOfBirth.'">"'.$yearOfBirth.'"</option>';
                            }
                        ?>
                    </select>
                </div>
            </div>

Upvotes: 2

devpro
devpro

Reputation: 16117

You just need to add name attribute in <select> box and add $i value in <option> as:

<select type="text" class="form-control" id="day" name='day'>
<option>--Day--</option>
<?php
$day = array(range(1,31));
for ($i = 1; $i <= 31; $i++) {
?>
<option value="<?=$i?>"><?=$i?></option>
<?php
}
?>
</select>

Here i am using name='day' in <select> tag, and value="<?=$i?>" in <option> tag.

Same as for YEAR and MONTH fields.

Upvotes: 2

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

Check Points for getting value form the Select tag

If you need to pull out the data from the select tag you have to check for the following conditions.

  1. Whether you have provided the name for the select tag.
  2. Whether you have provided the value for the option fields.

if so you do the above two alone you can fetch out or pull out the data from the select tag.

Example:

Select tag: With name and option values

<select name="age">
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>

Then in the PHP you can fetch out the data like this.

<?php
if(isset($_POST['age'])
{
  echo $_POST['age'];
}
?>

Upvotes: 1

Related Questions