i have searched all over and i cannot find a good answer for this. I have 3 dropdowns where user can select a date (all 3 are populated from DB). I need 3 becouse user can apply to all 3 at the same time. Now what i need to do is make at lease one required. Any ideas about that? Also the names are different since i am sending this to php function mail() to send all the data to my mail.
<select name="prijava_okp" id="prijava_okp" >
<option value="" selected="selected">Online klinični primeri </option>;
<?php
include("config.php");
session_start();
if (!$db) {
die('Could not connect: ' . mysqli_error($db));
}
$sql = mysqli_query($db, "SELECT ID_TECAJA, DATUM, HOUR FROM razpisani_tecaji WHERE STATUS ='odprt' AND ST_ODPRTIH_MEST>0 AND VRSTA=1");
while ($row = $sql->fetch_assoc()){
$row['DATUM'] = new DateTime($row['DATUM']);
$dateFormated = $row['DATUM']->format('d.m.Y');
echo "<option value='" . $row['ID_TECAJA'] . "'>" . $dateFormated," ","ob"," ", $row['HOUR'] . "</option>";
}
?>
</select>
<select name="prijava_ops" id="prijava_ops" >
<option value="" selected="selected">Online priprave na strokovni izpit</option>;
<?php
include("config.php");
session_start();
if (!$db) {
die('Could not connect: ' . mysqli_error($db));
}
$sql = mysqli_query($db, "SELECT ID_TECAJA, DATUM, HOUR FROM razpisani_tecaji WHERE STATUS ='odprt' AND ST_ODPRTIH_MEST>0 AND VRSTA=2");
while ($row = $sql->fetch_assoc()){
$row['DATUM'] = new DateTime($row['DATUM']);
$dateFormated = $row['DATUM']->format('d.m.Y');
echo "<option value='" . $row['ID_TECAJA'] . "'>" . $dateFormated," ","ob"," ", $row['HOUR'] . "</option>";
}
?>
</select>
<select name="prijava_ods" id="prijava_ods" >
<option value="" selected="selected">Online delavnice za študente medicine</option>;
<?php
include("config.php");
session_start();
if (!$db) {
die('Could not connect: ' . mysqli_error($db));
}
$sql = mysqli_query($db, "SELECT ID_TECAJA, DATUM, HOUR FROM razpisani_tecaji WHERE STATUS ='odprt' AND ST_ODPRTIH_MEST>0 AND VRSTA=3");
while ($row = $sql->fetch_assoc()){
$row['DATUM'] = new DateTime($row['DATUM']);
$dateFormated = $row['DATUM']->format('d.m.Y');
echo "<option value='" . $row['ID_TECAJA'] . "'>" . $dateFormated," ","ob"," ", $row['HOUR'] . "</option>";
}
?>
</select><br><br>
Use some Javascript for your form; just check to make sure that all three are equals to empty. If so, then return false
; this will prevent the form from being submitted, if the user, then, selects a single field the condition is no longer true
and the form can be submitted.
NOTE: I am using jquery; but this can easily be transcribed to Javascript Vanilla.
$('#submit').on('click', function() {
// create variables
var select1 = $('#select1');
var select2 = $('#select2');
var select3 = $('#select3');
// validate form
if ( select1.val() == '' && select2.val() == '' && select3.val() == '') {
alert('You need to select at least one value!');
return false; // do not submit
}
else
{
return true; // submit form
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="dropdown" action="" method="post">
<select name="select1" id="select1">
<option value="">Select One</option>
<option value="value2">Value2</option>
<option value="value3">Value3</option>
<option value="value4">Value4</option>
</select>
<select name="select2" id="select2">
<option value="">Select One</option>
<option value="value2">Value2</option>
<option value="value3">Value3</option>
<option value="value4">Value4</option>
</select>
<select name="select3" id="select3">
<option value="">Select One</option>
<option value="value2">Value2</option>
<option value="value3">Value3</option>
<option value="value4">Value4</option>
</select>
<button type="submit" name="submit" id="submit">Submit</button>
</form>
You can, and should, also validate your form on your server side; this will prevent the user from bypassing whatever parameters you have set - to a certain extant.
I will continue with the same concept, have an if
condition which will only return allow the code to run only if the user has selected at least one value. If all three are empty then it will echo an error and will stop the code from running.
Now if the condition is false; then that means that 1 or more select
values have been selected and the user is cleared to continue...
// first check for $_POST
if ( isset( $_POST ) )
{
// create variables
// filter them for any html tags
$prijava_okp = htmlentities( $_POST['prijava_okp'] );
$prijava_ops = htmlentities( $_POST['prijava_ops'] );
$prijava_ods = htmlentities( $_POST['prijava_ods'] );
// now check to see if all three are empty
if ( empty( $prijava_okp ) && empty( $prijava_ops ) && empty( $prijava_ods ) )
{
// if they are all three empty `exit` the script
// or use `header` to go back to the form
echo 'You have to select at least one or more fields!';
exit;
}
else
{
// ... run your code here
}
}
</div>