SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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;?>" />

    PHP Code:
        
        $sql    
    'SELECT * FROM `Drug` ';
        
    $result mysql_query($sql);
       
        if (
    mysql_num_rows($result) == 0)
            echo 
    " -- No patients found -- ";
        else {
            echo 
    "<select name=\"drugid\">/n";
            echo 
    "<option value=\"\"> </option>\n";
            while (
    $r mysql_fetch_array($resultMYSQL_ASSOC))
                echo 
    "<option value=\"" $r['ref'] . "\">" $r['ref'] . " : " $r['name'] . " " $r['dose'] . "</option>\n";
            
            echo 
    "</select>\n";
        } 
    <input type="submit" onclick="formvalidation('document.myform')" id="formbutton" value="Prescribe">

    </form>

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

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

    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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    The name of your drop down is drugid, not dose.

  3. #3
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    You changed your variable to drugid too, so dose is never assigned in your JavaScript
    Code JavaScript:
    function formvalidation() {
     
    drugid = document.myform.drugid.value ;
     
    if (drugid == "2.5mg")
    {
    alert('Enter Email-Address.');
    return false;
    }
     
    }

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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)

  6. #6
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Appreciate the help! But i think there is something fundamentally wrong here. The Javascript is just doing nothing every time

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •