0

I've been stuck on this for longer than I intended for such a simple thing, any ideas why the intended validation is not working?

if(isset($_POST['submit'])){

    /* Posted Values from Initial Form */
    $descEmail = $_POST['email'];
    $uniqueID = $_POST['name'];

    /* Check to see if the Email Provided is already in use */
    $result = mysql_query("SELECT * FROM Table WHERE Email = '$descEmail'");
    if(mysql_num_rows($result) == 0) {
        echo 'success';
    } else {
        echo '<script type="text/javascript"> emailInvalid() </script>';
    }       
}

The Javascript:

<script type="text/javascript">

function emailInvalid(){
    document.getElementById('UserEmail').style.borderColor='#CC0033';
    document.getElementById('UserEmail').style.border='solid';
    document.getElementById('UserEmail').style.borderWidth='2px';
    document.getElementById("formErrors").innerHTML = "The Email Address: <?php echo $UserEmail ?> has already been used, Please Use a Different Email Address.";
}

</script>

The form field in question: (Form is working as expected)

<div>
    <label for="email">Email</label>
    <input type="email" id="UserEmail" name="email">
</div>
Frog82
  • 464
  • 1
  • 8
  • 25
  • Is that php? You need to add the relevant tags to your question – Liam Oct 23 '15 at 10:44
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 23 '15 at 10:45
  • I can think of a few possible reasons why this might fail to work, but there isn't enough context to the code to tell which of them it might be. Most of them will give some pretty obvious error messages though. What does the Console in your browser's Developer Tools say? – Quentin Oct 23 '15 at 10:46
  • You should [learn how to use the label element properly](http://www.456bereastreet.com/archive/200711/use_the_label_element_to_make_your_html_forms_accessible/). The `for` attribute references the **id** of a form control, not the name. – Quentin Oct 23 '15 at 10:47
  • Uncaught ReferenceError: emailInvalid is not defined this is what the console spits out – Frog82 Oct 23 '15 at 10:47

1 Answers1

0

Uncaught ReferenceError: emailInvalid

You appear to have defined your emailInvalid in a <script> element that appears after the one in which you try to call the function (on possibly one on a different page).

You can't call a function before it exists. Change the order of your code so the function is declared before you try to call it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335