2015-10-23 53 views
0

我一直坚持这个比我打算这么简单的事情,任何想法为什么打算验证不工作的时间?简单的Javascript验证 - 不工作

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>'; 
    }  
} 

的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> 

表单字段中的问题:(表工作正常)

<div> 
    <label for="email">Email</label> 
    <input type="email" id="UserEmail" name="email"> 
</div> 
+0

是那个php?您需要将相关标签添加到您的问题 – Liam

+0

**危险**:您正在使用[一个**过时的**数据库API](http://stackoverflow.com/q/12859942/19068),并应使用[现代替代品](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你很容易受到[SQL注入攻击](http://bobby-tables.com/)**现代的API会使它更容易[防御](http://stackoverflow.com/questions/60174/最好的方式,以防止SQL注入在PHP)自己从。 – Quentin

+0

我可以考虑一些可能的原因,为什么这可能会失败,但代码没有足够的上下文来告诉它们可能是哪一个。他们中的大多数会给出一些非常明显的错误消息。浏览器的开发工具中的控制台说什么? – Quentin

回答

0

Uncaught ReferenceError: emailInvalid

你似乎已经定义了emailInvalid<script>在之后出现的元素tr y调用该函数(可能位于不同页面上的一个函数)。

您不能在函数存在之前调用它。更改您的代码的顺序,以便在您尝试调用它之前在之前声明

+0

更改顺序比在加载页面后调用脚本更好。也就是使用window.onload = function(){emailInvalid()}; – Danmoreng

+0

我不是一个延迟时间超过需要的粉丝。 – Quentin