PHP执行完之后,如何返回主页面

这是我的程序
<html>
<body>

<p><strong>注册账号</strong></p>
<form action="zhuce.php" method="post">
username: <input type="text" name="username" />
password: <input type="text" name="password" />
<input type="submit" />
</form>
 
<p><strong>登陆</strong></p>
<form action="denglu.php" method="post">
username: <input type="text" name="username" />
password: <input type="text" name="password" />
<input type="submit" />
</form>
</body>
</html>

这个是主页面,有注册 还有登陆框
这个是zhuce.php

<html>
<body>
<?php
$con = mysql_connect("localhost","root","423904");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test",$con);
$sql = "INSERT INTO username (username,password)
VALUES
('$_POST[username]','$_POST[password]')";
if(!mysql_query($sql,$con))
{
echo "<script>alert('账号已经被人注册');</script>";
}
else {echo "<script>alert('注册成功')</script>";}

mysql_close($con);
?>
</body>
</html>
我现在的情况就是,在注册地方点击提交按钮之后,如果注册成功,跳出alert,然后页面就空白了,浏览器地址栏变成http://127.0.0.1:81/zhuce.php

现在我想按了注册按钮之后,依旧停留在主页面上,请问这要怎么实现呢??

按了注册按钮之后,已经跳走了。所以如果要alert之后跳回:
这样:<script>alert('账号已经被人注册');window.location="index.php";</script>
和这样:<script>alert('注册成功');window.location="index.php";</script>

如果确实是要不跳走:(代码复制的你的,我只是帮你重组一下,如果你原本代码里有语法错误,我这个依然会有。改动是:form的action改为自己,那边的sql代码剪切了过来)
<html>
<body>
<?php
$con = mysql_connect("localhost","root","423904");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test",$con);
$sql = "INSERT INTO username (username,password)
VALUES
('$_POST[username]','$_POST[password]')";
if(!mysql_query($sql,$con))
{
echo "<script>alert('账号已经被人注册');</script>";
}
else {echo "<script>alert('注册成功')</script>";}
mysql_close($con);
?>
<p><strong>注册账号</strong></p>
<form action="" method="post">
username: <input type="text" name="username" />
password: <input type="text" name="password" />
<input type="submit" />
</form>

<p><strong>登陆</strong></p>
<form action="denglu.php" method="post">
username: <input type="text" name="username" />
password: <input type="text" name="password" />
<input type="submit" />
</form>
</body>
</html>追问

那如果我把登陆的PHP代码也写到主页面里边,那应该怎么分呢?就是action= 这个应该是什么呢?

追答

“登陆的PHP代码也写到主页面里边”,也就是说,处理登录的程序也是当前页,所以当然也是action=""
“怎么分”么,当然是根据提交的数据的特征来分。要注意,提交的数据,不是网页里所有的INPUT。而是类型为SUBMIT的键被按下的时候,这个键所在的FORM里的所有INPUT类型的东西(包括INPUT和SELECT之类的)。
所以你这里有点小问题,你两个FORM里的东西的NAME完全一样了。这是不好的。
下面一个FORM这样改:你写的“”
改成下一行写
那么,如果是注册:
if (isset($_POST['username']) && !isset($_POST['login'])) {
...
}
如果是登录:
if (isset($_POST['username']) && isset($_POST['login'])) {
...
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-20
可以通过PHP中的require()、include()包含引用函数来实现返回主页面
例如:
require('1.php');
require('2.php');
或者:

include('1.php');
include('2.php');
require()、include()
第2个回答  2011-05-09
你可以再加一句跳转到本页面 echo "<script language='javascript'>window.location.href='';</script>";类似这样的。中间的地址输入你要跳转的页面
第3个回答  2011-05-11
echo "<script>alert('注册成功');window.location.href="/index.php";</script>";登陆成功提示之后有个跳转,("/index.php")是你要跳转到的页面路径。
第4个回答  2011-05-08
你可以在alert后面加一句window.location.href="index.php" ;

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网