我使用了一个if条件,其中有一些脚本和头部。但是脚本不起作用,直接用头就行了。
if(strpos(mysql_error(),'Email')!=false)
{
print '<script type="text/javascript">';
print 'alert("The email address is already registered")';
print '</script>';
header('Location: register.php');
}发布于 2013-08-15 14:19:35
用典型的JS替换header。这就起到了作用。
if(strpos(mysql_error(),'Email')!==false)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('The email address is already registered')
window.location.href='register.php';
</SCRIPT>");
}发布于 2013-08-15 14:17:20
在您的代码中有一些不同的错误。
首先,header调用应该是您输出的first 。你不能这么做。在此之前,您有未缓冲的print%s。来自(非常好的) PHP文档:
请记住,必须在发送任何实际输出之前调用header(),无论是通过普通的HTML标记、文件中的空行,还是从PHP.使用include或require函数或其他文件访问函数读取代码,并在调用header()之前输出空格或空行,这是一个非常常见的错误。使用单个PHP/HTML文件时也存在同样的问题。
其次,即使您使用缓冲输出,Javascript也永远不会执行,因为当存在Location头时,不会计算正文。
第三,使用mysql_error找出某个记录是否存在并不是一种可行的方法。最好是类似于(伪代码)的东西:
SELECT email FROM table WHERE email="someEmail"
if (rows found) {
alert "email already exists"
}祝你学习luck开发好运!
发布于 2013-08-15 14:18:24
使用:
window.location = "example.html";先看@Bart Friederichs的答案,即使header工作得很好,JavaScript警报也不会起作用,因为页面一旦完成就会被重定向,浏览器也不会为你执行JavaScript。
<?php
print '<script type="text/javascript">';
print 'alert("The email address is already registered");';
print 'window.location = "register";';
print '</script>';
?>你也可以在没有PHP的情况下输出!例如:
if(strpos(mysql_error(),'Email')!==false)
{
?>
<script type="text/javascript">
alert("The email address is already registered");
window.location = "register.php";
</script>
<?php
}
?>https://stackoverflow.com/questions/18247260
复制相似问题