0

I have a dynamically generated drop down menus using PHP, I use these manes on different web pages, Now I also want to add some JS validation to these menus so that the user does not forget to choose an option while filling in a form.

The problem is that my JS code does not work and does not validate my menus, The code looks good to me I have IF statements where they check if the first option value =0 therefore there has not been selection but it still does not work

PHP code:

    $option='<select id="Forex" name="workshop">';
        $option.='<option value="0">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;             
    }

function getBinary($link){
$timestamp = date("Y-m-d");

    $query2 = mysqli_query($link, $sql2);
        $option2='<select id="Binary" name="workshop">';
        $option2.='<option value="0">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 workshop=document.getElementById('Binary').value;
    if(workshop==0){
        document.getElementById('Binary').style.borderColor = "red";
        document.getElementById('error').innerHTML = "Please Select Workshop1";
        return false;
    }else{
        document.getElementById('Binary').style.borderColor = "green";
    }

        var workshop2=document.getElementById('Forex').value;
    if(workshop2==0){
        document.getElementById('Forex').style.borderColor = "red";
        document.getElementById('error').innerHTML = "Please Select Workshop";
        return false;
    }else{
        document.getElementById('Forex').style.borderColor = "green";
    }
1
  • Please check your dom names and ids, it's a good practice (and easier for the eye) to have the same name & id a single element. If you try to access a null getDocumentById your script might break. Do you see any errors in your browser console? Jan 26, 2014 at 15:33

1 Answer 1

0

You have wrongly named the select id/name.

$option='<select id="Forex" name="workshop">'; 

and yet you are trying to access it from JS by calling it by id workshop while the name attribute is workshop not the id.

  var work=document.getElementById('workshop').value; 

The id is Forex not workshop. And you call Forex from different Js line. I think your error is in trying to access a null variable. Unless you have another element with the id workshop that I'm not aware of.

2
  • yes sorry there was another element with id workshop i removed, removed it from the question not to cause any more confusion sorry, but other than that can you see the problem with the code why it is not working
    – Tomazi
    Jan 26, 2014 at 15:40
  • Would you please post how your list looks like after being rendered by php? and btw, check that the 0 is actually an int not a string in workshop==0 or workshop2==0 Jan 26, 2014 at 16:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.