另外表格还可以,但结合......不是真的。困难在于我无法从第二个中的第一个if语句访问变量 $ name 错误:未定义的变量:名称
HTML:
<form method="POST" enctype="multipart/form-data" id="form1">
Name: <input type="text" name="name"><br>
Pass: <input type="password" name="pass"><br>
<input type="submit" name="submit1" value="Влез">
</form>
<form method="POST" enctype="multipart/form-data" id="form2">
We need some more information about you<br>
Please enter your e-mail: <input type="text" name="email"><br>
Please enter a new Password <input type="password" name="pass1" ><br>
Plese reenter tour new password <input type="password" name="pass2"><br>
<input type="submit" name="submit2" value="Save">
</form>
PHP:
require('config.php');
?><script type="text/javascript">document.getElementById("form2").style.display="none"; </script><?php
if(isset($_POST['submit1']))
{
$name = mysql_escape_string($_POST['name']);
$pass = mysql_escape_string($_POST['pass']);
//chek if the username and password are correct
$check = mysql_query("SELECT * FROM test WHERE name = '$name' AND pass = '$pass'");
if(mysql_num_rows($check) >= 1)
{
?>
<script type="text/javascript">
document.getElementById("form1").style.display="none";
document.getElementById("form2").style.display="block";
</script>
<?php
exit();
}
else echo "<h1><font color='red'> Грешно Име или Парола</font></h1>";
}
if(isset($_POST['submit2']))
{
$email = mysql_escape_string($_POST['email']);
$pass1 = mysql_escape_string($_POST['pass1']);
$pass2 = mysql_escape_string($_POST['pass2']);
$checkpass = mysql_query("SELECT * FROM test WHERE pass = '$pass1'")or die(mysql_error());
if($pass1 != $pass2){
echo "Passwords do not Match";
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Wrong email format";
}
elseif (mysql_num_rows($checkpass)>=1) {
echo "Password already taken";
}
elseif (empty($pass1) || empty($pass2) || empty($email)) {
echo "Not all fields are filled";
}
else
{
//put in DB
mysql_query("UPDATE test SET pass='$pass1' WHERE name='$name'") or die(mysql_error());
mysql_query("UPDATE test SET email='$email' WHERE name='$name'") or die(mysql_error());
?><script type="text/javascript">
document.getElementById("form1").style.display="none";
document.getElementById("form2").style.display="none";
</script><?php
echo "<h1><font color='green'>Registration successful</font></h1>";
}
}
P.S不介意里面的java脚本没有,我无法组合表格!
答案 0 :(得分:2)
您无法访问第二个$name
语句中的if
变量的原因是表单结构。您有两种不同的表单,其中只有一种可以随时提交。因此,您的if
语句只能处理其中一种表单,submit1
或submit2
。最简单的解决方案可能是将两个表单和if
语句组合在一起。然后,您需要更多if
语句来检查已提供和应处理的信息。
<强>加成强>
您的某个表单的名称不是英文。使用英语编写代码是一种很好的做法,因为其他不熟悉您的语言的开发人员会阅读您的代码(正如我们现在所做的那样)。如果他们想要在互联网上研究您的实施,这也有助于其他人。
我还建议检查关注点的分离。截至目前,您的代码混合了表示逻辑和域逻辑(动作处理的东西)。这看起来似乎势不可挡,但我可以向您保证,这是您将来会喜欢的投资。
快乐的编码!
答案 1 :(得分:1)
如评论中所述,如果您希望submit1
中的submit2
信息可以在if(isset($_POST['submit1']) || isset($_POST['submit1']))
{
//submit1 code here
if(isset($_POST['submit2']))
{
//submit2 code here
}
}
中访问,那么只会调用一个函数,然后更改您的if语句。 / p>
submit1
这样,两个提交按钮都会运行submit2
数据,tmpcell.formula = "='C:\Users\User1\Desktop\Folder1\[Page.xls]Sheet1'!A1"
将能够根据需要使用这些数据。
答案 2 :(得分:0)
我尝试使用session_start()
传递变量,如下所示:
if(isset($_POST['submit1']))
{
$name = mysql_escape_string($_POST['name']);
session_start();
$_SESSION["a"] = $name;
$pass = mysql_escape_string($_POST['pass']);
//chek if the username and password are correct
$check = mysql_query("SELECT * FROM test WHERE name = '$name' AND pass = '$pass'");
if(mysql_num_rows($check) >= 1)
{
?><script type="text/javascript">
document.getElementById("form1").style.display="none";
document.getElementById("form2").style.display="block";
</script><?php
}
else echo "<h1><font color='red'> Грешно Име или Парола</font></h1>";
}
if(isset($_POST['submit2']))
{
session_start();
$name = $_SESSION["a"];
$email = mysql_escape_string($_POST['email']);
$pass1 = mysql_escape_string($_POST['pass1']);
$pass2 = mysql_escape_string($_POST['pass2']);
$checkpass = mysql_query("SELECT * FROM test WHERE pass = '$pass1'")or die(mysql_error());
if($pass1 != $pass2){
echo "Passwords do not Match";
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Wrong email format";
}
elseif (mysql_num_rows($checkpass)>=1) {
echo "Password already taken";
}
elseif (empty($pass1) || empty($pass2) || empty($email)) {
echo "Not all fields are filled";
}
else
{
//put in DB
mysql_query("UPDATE test SET pass='$pass1' WHERE name='$name'") or die(mysql_error());
mysql_query("UPDATE test SET email='$email' WHERE name='$name'") or die(mysql_error());
?><script type="text/javascript">
document.getElementById("form1").style.display="none";
document.getElementById("form2").style.display="none";
</script><?php
echo "<h1><font color='green'>Registration successful</font></h1>";
}
}