Why won't this code work!!!

Discussion in 'JavaScript' started by Imozeb, Feb 1, 2010.

  1. #1
    This is my script for validating an email form. For some strange reason when I put the PHP echo code (in red) in it doesn't execute the alert command, it just sends the form. I've spent over an hour analyzing this code and can't find anything wrong with it. Please Help!!!


    <script type="text/javascript">
    function validate_sendmail_form()
    {
    //reset vars
    femailaddress = 0;
    fcheck = 0;
    f = 0;
    ftxtemailaddress = "";
    ftxtcheck = "";
    //get vars
    var emailaddress = document.getElementById("emailaddress").value;
    var check = document.getElementById("check").value;
    //check email
    if (emailaddress == "" || emailaddress == null || emailaddress.indexOf("@") < 1 || emailaddress.lastIndexOf(".") - emailaddress.indexOf("@") < 2)
    {
    femailaddress = 1;
    f = 1;
    ftxtemailaddress = "Enter a valid email address.";
    }
    if (check !== "FORP1dI1")
    {
    fcheck = 1;
    f = 1;
    ftxtcheck = "Verification failed. Please enter the correct characters!";
    }
    //return errors if any
    if (f == 1)
    {
    alert(ftxtemailaddress + " " + ftxtcheck);
    return false;
    }
    if (f == 0)
    {
    <?php echo sendmail() ?>;
    }
    }
    </script>


    ~Imozeb
     
    Imozeb, Feb 1, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Can you write full script??
     
    s_ruben, Feb 1, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is the full code: (this is just the html form and javascript)(the PHP part works.)


    <head>
    <script type="text/javascript">
    function validate_sendmail_form()
    {
    //reset vars
    femailaddress = 0;
    fcheck = 0;
    f = 0;
    ftxtemailaddress = "";
    ftxtcheck = "";
    //get vars
    var emailaddress = document.getElementById("emailaddress").value;
    var check = document.getElementById("check").value;
    //check email
    if (emailaddress == "" || emailaddress == null || emailaddress.indexOf("@") < 1 || emailaddress.lastIndexOf(".") - emailaddress.indexOf("@") < 2)
    {
    femailaddress = 1;
    f = 1;
    ftxtemailaddress = "Enter a valid email address.";
    }
    if (check !== "FORP1dI1")
    {
    fcheck = 1;
    f = 1;
    ftxtcheck = "Verification failed. Please enter the correct characters!";
    }
    //return errors if any
    if (f == 1)
    {
    alert(ftxtemailaddress + " " + ftxtcheck);
    return false;
    }
    if (f == 0)
    {
    <?php echo sendmail() ?>;
    }
    }
    </script>
    </head>
    <body>
    <form id="email" onsubmit="return validate_sendmail_form()" name="email" method="POST" action="">
    <label>Email to:
    <select name="emailto" id="emailto">
    <option value="myemail@yahoo.com">Public Administrator</option>
    </select>
    </label>
    <p>
    <label>Question Type:
    <select name="type" id="type">
    <option value="Feedback" selected="selected">Feedback</option>
    <option value="Advertising Question">Advertising Question</option>
    <option value="Job Question">Job Question</option>
    <option value="Help Question">Help Question</option>
    <option value="General Question">General Question</option>
    <option value="Bug Report">Bug, Error Report</option>
    </select>
    </label>
    </p>
    <p>
    <label>Your Name:
    <input name="name" type="text" id="name" value="Anonymous" size="21" maxlength="20" />
    </label>
    </p>
    <p>
    <label>Your Email Address:
    <input name="emailaddress" type="text" id="emailaddress" size="41" maxlength="40" />
    </label>
    *</p>
    <p>Your Message: * </p>
    <p>
    <textarea name="message" cols="50" rows="10" id="message">Enter Message Here:</textarea>
    </p>
    <p>
    <label>Type in the letters and numbers seen to the right for verification: *
    <input name="check" type="text" id="check" size="21" maxlength="20" />
    </label>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>
    <p>
    <input name="Reset" type="reset" id="Reset" value="Reset Form" />
    &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
    <input type="submit" name="Submit" value="Send Message" />
    </p>
    <input type="hidden" name="MM_insert" value="email">
    </form>
    <p>*required</p>
    </body>
    </html>
     
    Imozeb, Feb 1, 2010 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    If I understand you correctly you want to send message by php function but check fields validations by javascript?? If so you have to do it by ajax.
     
    s_ruben, Feb 1, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You are right about me trying to validate the form through javascript and the mail it using PHP. You want me to use AJAX but isn't AJAX just javascript, html, and css all rolled into one? What code am I supposed to put there so it will work. Please help, I'm a novice when it comes to javascript. :)

    Nice website, by the way!

    ~Imozeb
     
    Imozeb, Feb 1, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    Last edited: Feb 1, 2010
    s_ruben, Feb 1, 2010 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    You don't need to use AJAX...
    If your validation returns no errors then you just need to submit the form (or return true to the onsubmit event)... first the form needs to have the action attribute set to your php script (it can be the same page, if your php is within the same page as the html)...

    <form id="email" onsubmit="return validate_sendmail_form()" name="email" method="POST" action="sendmail.php">

    Then just change the javascript to:

    if (f == 0)
    {
    return true;
    }
     
    camjohnson95, Feb 2, 2010 IP