我的表单在页面加载PHP时自动提交

时间:2017-03-09 15:56:05

标签: php html css registration

我已创建了一个注册页面,但即使我已经进行了表单验证,我的表单也会在页面加载时自动提交空白数据。似乎无法摆脱我的错误。

此外,我收到$ _POST [first_name]和其他人的未定义索引错误,当我使用' isset'为了把它包括在内,我得到了' 1'作为存储在DB中的数据。

提前致谢。



<?php

	require_once("config.php");

	$f_name = isset($_POST['first_name']);
	$l_name = isset($_POST['last_name']);
	$e_mail = isset($_POST['email']);
	$p_w_d =  isset($_POST['password']);

	//password hashing and salting
   $hash = hash('sha256', $p_w_d);
     
   function createSalt()
   {
       $text = md5(uniqid(rand(), true));
       return substr($text, 0, 3);
   }
    
   $salt = createSalt();
   $sec_password = hash('sha256', $salt . $hash);

	//creating the database query
	$query = "INSERT INTO ad_min_user (first_name, last_name, email_id, password) VALUES
	('$f_name','$l_name','$e_mail','$sec_password')";
	$result = mysqli_query($conn,$query);

   if(!empty($result)) {
    		//echo '<script language="javascript">';
			//echo 'alert("Registration Successful !")';
			//echo '</script>';
			unset($_POST);
		} 
		else {
			echo '<script language="javascript">';
			echo 'alert("Registration Failed !")';
			echo '</script>';		
		}
	 ?>

?>


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Create Admin Panel | 9JA Home & Tours LTD</title>

<!-- BOOTSTRAP STYLES-->
<link href="assets/css/bootstrap.css" rel="stylesheet" />
<!-- FONTAWESOME STYLES-->
<link href="assets/css/font-awesome.css" rel="stylesheet" />
<!-- CUSTOM STYLES-->
<link href="assets/css/custom.css" rel="stylesheet" />
<!-- GOOGLE FONTS-->
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
</head>
<body>
<div class="container"> <br/>
  <br/>
  <br/>
  <div class="row text-center ">
    <div class="col-md-12"><br/>
      <span style="font-size:35px;font-weight:bold;color:red;">CREATE ADMIN PANEL</span></div>
  </div>
  <br/>
  <div class="row ">
    <div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1">
         
      <div class="panel panel-default" id="loginBox">
        <div class="panel-heading"> <strong> Enter Admin Details </strong> </div>
        <div class="panel-body">
          <form onSubmit="return validationForm();" role="form" id="form" method="post">
            <br />
            <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-user"  ></i></span>
              <input type="text" name="first_name" id="first_name" class="form-control" placeholder="First Name" />
            </div><div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-user"  ></i></span>
              <input type="text" name="last_name" id="last_name" class="form-control" placeholder="Last Name" />
            </div>
            <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-envelope"  ></i></span>
              <input type="text" name="email" id="email" class="form-control" placeholder="Email" />
            </div>
            <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>
              <input type="password" name="password" id="password" class="form-control"  placeholder="Your Password" />
            </div>
            <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>
              <input type="password" name="confirm_password" id="confirm_password" class="form-control"  placeholder="Confirm Password" />
            </div>
            <div class="form-group">
              <label class="checkbox-inline"> </label>
              <span class="pull-right"> <a href="index.php" >Login as Admin</a> </span> </div>
            <hr />
            <div align="center">
              <button style="width:100%;" type="submit" id="login" class="btn btn-primary"><i class="fa fa-user"  ></i>&nbsp;Create Admin</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
function validationForm(){
	if($("#first_name").val() == ''){
		alert("First Name Required !!!");
		$("#first_name").focus();
		return false;
	}
	else if($("#last_name").val() == ''){
		alert("Last Name Required !!!");
		$("#last_name").focus();
		return false;
	}
	else if($("#email").val() == ''){
		alert("Email Required !!!");
		$("#email").focus();
		return false;
	}
	else if(!validateEmail($("#email").val())){
		alert("Valid Email Required !!!");
		$("#email").focus();
		return false;
	}
	else if($("#password").val() == ''){
		alert("Password Required !!!");
		$("#password").focus();
		return false;
	}
	else if($("#confirm_password").val() == ''){
		alert("Confirm Password");
		$("#confirm_password").focus();
		return false;
	}
	else if($("#password").val() != $("#confirm_password").val()){
		alert("Passwords Don't Match");
		$("#confirm_password").focus();
		return false;
	}
	else{
		return true;
	}
}
function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}


</script>

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

不知道这是否能解决您的问题,但isset会返回true / false。

$f_name = isset($_POST['first_name']);
$l_name = isset($_POST['last_name']);
$e_mail = isset($_POST['email']);
$p_w_d =  isset($_POST['password']);

这意味着您的名字,姓氏,电子邮件,密码将为真/假,而不是实际的帖子值。

你想重写一些像......

$f_name = isset($_POST['first_name']) ? $_POST['first_name'] : null;

您还希望将整个php块包装成:

if (isset($_POST) {
    // PHP stuff here e.g. 
    $f_name = isset($_POST['first_name']) ? $_POST['first_name'] : null;
    ...
}

因为如果表单尚未发布,您将尝试访问$_POST中尚未实际存在的内容。

你上面的例子中也有一个问题......你似乎在关闭php两次......

     ?>

?>

基本上,你的代码应该看起来像......

<?php

require_once("config.php");

if (isset($_POST)) {
    $f_name = isset($_POST['first_name']) ? $_POST['first_name'] : null;
    $l_name = isset($_POST['last_name']) ? $_POST['last_name'] : null;
    $e_mail = isset($_POST['email']) ? $_POST['email'] : null;
    $p_w_d = isset($_POST['password']) ? $_POST['password'] : null;

    //password hashing and salting
    $hash = hash('sha256', $p_w_d);

    function createSalt()
    {
        $text = md5(uniqid(rand(), true));
        return substr($text, 0, 3);
    }

    $salt = createSalt();
    $sec_password = hash('sha256', $salt . $hash);

    //creating the database query
    $query = "INSERT INTO ad_min_user (first_name, last_name, email_id, password) VALUES
        ('$f_name','$l_name','$e_mail','$sec_password')";
    $result = mysqli_query($conn, $query);

    if (!empty($result)) {
        unset($_POST);
    } else {
        echo '<script language="javascript">';
        echo 'alert("Registration Failed !")';
        echo '</script>';
    }
}

但请记住验证您的输入