PHP & Javascript Combined Problem

Hi,

I am very new to Java Script and currently working on a program. Basically i am developing a pharmaceutical website. I have a php form with a drop down menu. The user selects a drug from the drop down menu to prescribe, then submits it. However, i need to try and validate this form with Javascript, and this is where my problem lies. Because i have echo’d out the list of all the drug id’s,name and dosage, i dont know how to then validate each variable.

<form name=“myform” action=“<?php echo $pfile;?>” method=“post” >
<input type=“hidden” name=“patientid” value=“<?php echo $patientid;?>” />



    $sql    = 'SELECT * FROM `Drug` ';
    $result = mysql_query($sql);

    if (mysql_num_rows($result) == 0)
        echo " -- No patients found -- ";
    else {
        echo "&lt;select name=\\"drugid\\"&gt;/n";
        echo "&lt;option value=\\"\\"&gt; &lt;/option&gt;\
";
        while ($r = mysql_fetch_array($result, MYSQL_ASSOC))
            echo "&lt;option value=\\"" . $r['ref'] . "\\"&gt;" . $r['ref'] . " : " . $r['name'] . " " . $r['dose'] . "&lt;/option&gt;\
";

        echo "&lt;/select&gt;\
";
    }




I then have tried to put Java Script into my header like so:

 &lt;script&gt;
	function formvalidation() {
    // Fetch email-value
  dose = document.myform.dose.value ;
    // value empty?
    if (dose == "10mg")
        {
            alert('That's a High Dosage');
            return false;
        }
   &lt;/script&gt;

My JavaScript is just being ignored. Does anyone have any idea to validate for example, if the dose being submitted is 10mg, an alert will appear? Thanks in advance

The name of your drop down is drugid, not dose.

You mean to try:

function formvalidation() {

drugid = document.myform.drugid.value ;

if (dose == "2.5mg")
    {
        alert('Enter Email-Address.');
        return false;
    }

Still JS being ignored! Would you say the other code is correct? Been at this for days!

You changed your variable to drugid too, so dose is never assigned in your JavaScript

function formvalidation() {

drugid = document.myform.drugid.value ;

if (drugid == "2.5mg")
{
alert('Enter Email-Address.');
return false;
}

}

On another thought, that won’t work either, as you are storing the ‘ref’ column as the value of the drop down, not the ‘dose’ column, so you’d have to alter your drop down value or read the dose from the text property (which would need to be parsed)

Appreciate the help! But i think there is something fundamentally wrong here. The Javascript is just doing nothing every time :frowning:

If you are using chrome, or IE, use the built in debugger.

In IE, press F12, click on Script, from the drop down (ext to Start Debugging), select the script file your code exists in. Find the line “if (drugid == “2.5mg”)”, right click on it and press Insert Breakpoint, then press Start Debugging.

Select the value on your form and press the button that invokes the function. It should stop on the line you created the breakpoint, and you can see what value it is receiving by looking at the Locals tab or placing your mouse over the variables.

Chrome, is similar, but you must go to the Sources tab, and to set a breakpoint, you click on the line number of the code you want to set the breakpoint at. You also do not have to click Start Debugging, it is started automatically.