Ok I have a drop down menu that is dynamically generated by PHP and populated with data fetched from database. Frontally this works vert well, The problem that I am having is with a JS validation, am not so good with JavaScript ('Dislike it'), but for the purposes of my mini project I have to work with it anyway....
The problem is that with JS I am checking if a user has selected one of the options available in drop down menu is so the form can be submitted else display a warning.
So what I am trying to do is this.....Give a value="f" to the first option which displays "PLEASE SELECT workshop" so if at the submission of the form the value of this drop down menu == f return false else submit the form.
now if at the submission the value ==f the error is displayed but if the value is not == f and i select one of the values from drop down menu i cannot proceed with the submission its does not allow me
PHP code:
function getForex($link){
$timestamp = date("Y-m-d");
$sql = "SELECT id, course, status, endingDate, schedule FROM courses WHERE course LIKE '%Forex%' OR course LIKE '%forex%' AND status=5";
$query = mysqli_query($link, $sql);
$option='<select id="Forex" name="workshop">';
$option.='<option id="fx" value="Forex">Select Forex Workshop</option>';
$option.='';
while($result = mysqli_fetch_assoc($query))
{
if($timestamp < $result['endingDate'])
{
$option.='<option id="'.$result['id'].'" value='.$result['endingDate'].'>'.$result['course']." ".$result['schedule'].'</option>';
}
}
$option.='</select>';
return $option;
}
JS CODE:
var sel=document.getElementById('fx').value="Forex";
if(sel.match("Forex")){
document.getElementById('error').innerHTML = "Choose Forex Workshop Date";
document.getElementById('fx').style.borderColor = "red";
return false;
}else{
document.getElementById('fx').style.borderColor = "green";
}
OK guys combination of many very helpful answers have helped but when I add another drop down menu like
PHP code:
function getBinary($link){
$timestamp = date("Y-m-d");
$sql2 = "SELECT id, course, status, endingDate, schedule FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%' AND status=5";
$query2 = mysqli_query($link, $sql2);
$option2='<select id="Binary" name="workshop">';
$option2.='<option id="bi" value="Binary">Select Binary Workshop</option>';
$option2.='';
while($result2 = mysqli_fetch_assoc($query2))
{
if($timestamp < $result2['endingDate'])
{
$option2.='<option id="'.$result2['id'].'" value="'.$result2['endingDate'].'">'.$result2['course']." ".$result2['schedule'].'</option>';
}
}
$option2.='</select>';
return $option2;
}
JS Code:
var selbi=document.getElementById('Binary').value;
if(selbi.match("Binary")){
document.getElementById('error').innerHTML = "Choose Binary Workshop Date";
return false;
}
The problem becomes that The Inner HTML message displays only for forex and get twisted around i.e. if forex is not selected it shows the message choose forex workshop if i choose the forex workshop it then shows choose binary workshop :D