MySQL Results - selection Query

I have a php form that gets the first input field results from a mysql database query. The query works fine and the results display in a dropdown box where the end user can select the correct answer from the list.

I want to make this selection choice a mandatory field in the php form how can I go about doing this?

I tried this format which I used for other fields but it didn’t appear to work:

<input type=“Date” name=“start” id=“date”; ?, required="" placeholder=“date”
oninvalid=“this.setCustomValidity(‘Please Enter valid date’)”
oninput=“setCustomValidity(’’)”>

Any help would be appreciated.

After some further research I have found that the issue maybe due to the top result automatically being selected in the drop down list.

One of the easiest ways is to add a first option that has a blank value. Something loosely like this:

<select name="dropdown1">
    <option value="">You must enter this value!</option>
    <option value="option 1">Option 1</option>
    <option value="option 2">Option 2</option>
</select>

Then once they press the submit button, you can check if the drop-down’s value is empty or not.
If empty, throw out an error alert to tell them they did not select whatever…

Hope that makes sense!

Thanks for your suggestion.

I already have this set-up in place. Which gets the list from the database.

<?php $res=mysqli_query($con,"SELECT van FROM van"); while ($row=mysqli_fetch_array($res)) { unset($van); $van = $row['van']; echo ''.$van.''; ?>

This displays the list okay but is there a way to make the selection a mandatory one or make it so it displays the first result but this not able to be selected as an option?

Well, I just told you how to do that. Just load the data from the database, but add a first option that forces them to change it to the one they want.

I wasn’t able to get it to work when loading the data from the database. However adding all the options manually like in your example worked.

Thank you for your help.

Well, glad you solved it. If you show the code how you are trying to populate the select’s options, then we can help you do it that way. But, if it works all good !

Sponsor our Newsletter | Privacy Policy | Terms of Service