我正在运行PHP代码来验证登录表单,如果输入为空,它应该运行一个javscript函数,该函数将某些DIV的显示属性更改为阻止。 如果我把脑袋标签之间我的JS代码,它说试图在PHP代码中调用js函数
Uncaught TypeError: Cannot read property 'style' of null
,如果我把我的代码/标记之前它说
Uncaught ReferenceError: formValidation is not defined
Javascript代码:
function formValidation() {
document.getElementById("errMessage").style.display = "block",
document.getElementById("arrow-errMessage").style.display = "block",
document.getElementById("errEmail").style.display = "block",
document.getElementById("arrow-errEmail").style.display = "block";
}
PHP代码:
else {
echo "<script> formValidation(); </script>";
}
最好是使用if else语句来验证形式的功能,然后提醒用户有关空白,然后专注于该字段并添加css,而不是使用JavaScript在php中执行此操作。如果php和JavaScript是分开的,那么它也很容易处理错误。 –
如果您通过PHP运行验证,为什么不直接通过PHP设置这些样式...?我非常怀疑这里http://stackoverflow.com/a/13840431/476的重复... – deceze