2

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.

6
  • You need to put value on your option fields <option name='year' value='$i'>".$i."</option> .. for day and month also Commented Oct 10, 2016 at 7:19
  • 1
    @S.I.: agreed with u, but OP also need to add name attribute in <select> as i mentioned in answer Commented Oct 10, 2016 at 7:30
  • 1
    Yes, down in my answer is fixed since I can't edit this comment anymore. Commented Oct 10, 2016 at 7:31
  • @Blueblazer, now you have few solutions, try them Commented Oct 10, 2016 at 7:36
  • @Blueblazer,. I have provided with some general checks over to the solutions that i had provided for the select tags. And find that and use for the next projects. Commented Oct 10, 2016 at 7:40

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

I have provide with some general checks so that it might be useful for that developer right in future to check by himself and fix it.
may you explain what '<?=$i?>' means
@Blueblazer <?=?> is a short tag which is equal to <?php echo $i; ?>
@Blueblazer: glad to help u if answer work for u, dont forget to accept it, this will help to others.
2

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.

Comments

1

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'];
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.