我已经创建了一个用于注册的html表单,当用户成功注册时,我希望将它们自动重定向回我的index.html
页面,并弹出警告告诉他们已经注册成功。目前我的警报有效,但它只是在空白页面中打开。我试过放入header ("location:../index.html")
但是没有用,它只是指引我直接到页面而不给我想要的警报。任何人都可以帮忙吗?
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
$select_db = mysql_select_db("mayan",$con);
if(isset($_POST['register']))
{
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$address=$_POST['address'];
$postcode=$_POST['postcode'];
$emailaddress=$_POST['emailaddress'];
$password=$_POST['password'];
$query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'";
$link = mysql_query($query)or die(mysql_error());
$num = mysql_num_rows($link);
if ($num>0){
echo 'Email already exists'; //email already taken
}
else {
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')";
$result = mysql_query($insert_query)or die(mysql_error());
if(success)
{
echo "<script type=\"text/javascript\">".
"alert('success');".
"</script>";
}
}
}
?>
答案 0 :(得分:2)
我在网站填写表格后,使用此代码将某人重定向回主页。
if(empty($errors))
{
//send the email
echo '
<script type="text/javascript">
alert("YOUR MESSAGE");
window.location.href = "http://www.yoursite.com";</script>';
}
答案 1 :(得分:1)
重定向时使用此功能:
function redirect($url) {
if(!headers_sent()) {
//If headers not sent yet... then do php redirect
header('Location: '.$url);
exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';
exit;
}
}
因此,您希望重定向到同一页面但使用?= XXX获取值
您的代码看起来像这样:
<?php
if ($_GET['success'] == 1){
echo "<script type=\"text/javascript\">".
"alert('success');".
"</script>";
}
$con = mysql_connect("localhost","root","") or die(mysql_error());
$select_db = mysql_select_db("mayan",$con);
if(isset($_POST['register']))
{
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$address=$_POST['address'];
$postcode=$_POST['postcode'];
$emailaddress=$_POST['emailaddress'];
$password=$_POST['password'];
$query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'";
$link = mysql_query($query)or die(mysql_error());
$num = mysql_num_rows($link);
if ($num>0){
echo 'Email already exists'; //email already taken
}
else {
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')";
$result = mysql_query($insert_query)or die(mysql_error());
if(success)
{
redirect('index.php?sucess=1');
}
}
}
?>
答案 2 :(得分:0)
要通过JavaScript重定向:
window.location = "http://www.google.com/";
但我建议添加一个供用户点击的链接,以及一些用户可能禁用了JavaScript。我会在重定向上设置超时,因此用户只需几秒钟即可阅读该页面。 E.g:
if (success) {
echo "<script type=\"text/javascript\">" .
"setTimeOut(function() {" .
" window.location = 'http://www.google.com';" .
"}, 2500);" .
"</script>";
}
答案 3 :(得分:0)
您可以在索引页面中执行变量,例如&#34; index.php?success&#34;。然后通过if(isset($ _ GET [&#39; success&#39;])检查此请求。并为该IF添加一些登录信息。
答案 4 :(得分:-1)
1)首先将 index.html 更改为php文件: index.php
2)在 index.php 文件的顶部写
<?php
session_start();
?>
3)在 index.php 文件的正文中,您可以:
打印消息
<?php
if(isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
或使用javascript提醒
<?php
if(isset($_SESSION['message'])) {
echo '<script> alert(\'Success\'); </script>';
unset($_SESSION['message']);
}
?>
4)在这个动作文件中,你给了我们
<?php
// START THE SESSION
session_start();
$con = mysql_connect("localhost","root","") or die(mysql_error());
$select_db = mysql_select_db("mayan",$con);
if(isset($_POST['register']))
{
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$address=$_POST['address'];
$postcode=$_POST['postcode'];
$emailaddress=$_POST['emailaddress'];
$password=$_POST['password'];
$query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'";
$link = mysql_query($query)or die(mysql_error());
$num = mysql_num_rows($link);
if ($num>0){
$_SESSION['message'] = 'Email already exists'; //email already taken
}
else {
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')";
$result = mysql_query($insert_query)or die(mysql_error());
if($result)
{
$_SESSION['message'] = 'Success';
}
}
}
// redirect to main page
header("Location: /");
?>