提交按钮不是先检查php脚本。

时间:2013-10-08 14:39:53

标签: php forms

我是php验证的新手。我正在编写注册表单,它将通过php脚本检查两个unqiue变量ac_titleemail,然后检查错误。它将在数据库中插入给定的值。文件名为formvu.php,然后它将移至activate.php。我遇到了问题,如果我使用

<form name="fone" method="post" onsubmit="return validateform(fone)" action="activate.php">

表单将忽略php脚本,它将移至下一页。我何时会用到这个:

<form name="fone" method="post" onsubmit="return validateform(fone)" action="<?php $_SERVER['PHP_SELF']?> ">

现在页面将运行整个php错误和命令。

这是我的整个PHP代码:

<?php
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $nun=$_POST['account'];
        $em=$_POST['email'];
        $register="SELECT ac_title,email FROM register";
        $query=mysql_query($register);
        while ($row= mysql_fetch_array($query))
        {   
            if ($nun == $row['ac_title'])
            {
                print "<span class=\"phclass\"><br/><b>
                    Your Account Title :</b></span>"." '"."<span class=\"ahclass\"><b>$nun</b></span>"."'".
                    " <span class=\"phclass\"><b>already exsist, please choose different.</b></span><br/>";
            }

            if($em == $row['email'])
            {
                print "<span class=\"phclass\"><b><br/>Your Email:</b>
                    </span>"." "."<span class=\"phclass\"><b> $em</b></span>"."<span class=\"phclass\">
                    <b> already exsist, please visit <a href=\"resetpassword.html\">
                    Forget password</a></b></span><br/>";
            }
            if ($nun != $row['ac_title'] && $em != $row['email'])
            {
                $ac_title=$_POST['account'];
                $email=$_POST['email'];
                $pass=$_POST['pwd'];
                $fn=$_POST['fname'];
                $gd=$_POST['gender'];
                $city=$_POST['city'];
                $VID=$_POST['vuid'];
                $course=$_POST['courseid'];
                $camp=$_POST['campus'];
                $hash=md5(rand(0,1000));

                $sql="INSERT INTO register (ac_title, email, pass,fn, sex, city,
                    VID, CRS, campus, hash,activate) VALUES('$ac_title', '$email',
                    '$pass','$fn', '$gd',' $city','$VID','$course',
                    '$camp','$hash','0')";

                $inst=mysql_query($sql,$connection);

                if (!$inst)
                {   
                    print "";
                }
                else 
                {
                    echo "Succesful ";
                }
            }// if
        } // while
    } // if
?>

如果操作是默认页面,它将运行整个脚本。我想运行整个脚本并转到下一页(activate.php)。

3 个答案:

答案 0 :(得分:1)

成功时,您想要重定向吗?如果是这样,请在回显“成功”的地方添加此代码:

header("Location: activate.php");
die();

有关重定向的详细信息,请参阅here

答案 1 :(得分:1)

验证是一个复杂的问题,但也有很多在线资源可以帮助您。对我来说这是Google的早期热门:http://www.sitepoint.com/form-validation-with-php/

虽然您可以使用JS(您似乎正在做)添加表单验证,但如果您希望它更安全,我建议您也验证数据服务器端(PHP),并且提供的链接应该可以帮助您。

我意识到这个答案并没有直接回答你的问题,但如果你对简单的验证技术越来越熟悉,你可能不会首先遇到这个问题。

答案 2 :(得分:1)

您应该通过检查电子邮件和用户名

来使用Array

使用我的

代码
<?php
//Start session
session_start();

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Input Validations
if($firstname == '') {
    $errmsg_arr[] = 'First name missing';
    $errflag = true;
}
if($lastname == '') {
    $errmsg_arr[] = 'Last name missing';
    $errflag = true;
}
if($email == '') {
    $errmsg_arr[] = 'Email missing';
    $errflag = true;
}
if($username == '') {
    $errmsg_arr[] = 'Username ID missing';
    $errflag = true;
}
if($password == '') {
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}
if($cpassword == '') {
    $errmsg_arr[] = 'Confirm password missing';
    $errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
    $errmsg_arr[] = 'Passwords do not match';
    $errflag = true;
}

//Check for duplicate email
if($email != '') {
    $qry = "SELECT * FROM ".$table." WHERE email='$email'";
    $result = mysql_query($qry);
    if($result) {
        if(mysql_num_rows($result) > 0) {
            $errmsg_arr[] = 'Email have already in Using!';
            $errflag = true;
        }
        @mysql_free_result($result);
    }
    else {
        die("Query failed");
    }
}

//Check for duplicate login ID
if($login != '') {
    $qry = "SELECT * FROM ".$table." WHERE login='$login'";
    $result = mysql_query($qry);
    if($result) {
        if(mysql_num_rows($result) > 0) {
            $errmsg_arr[] = 'User-Name already in Using!';
            $errflag = true;
        }
        @mysql_free_result($result);
    }
    else {
        die("Query failed");
    }
}

//If there are input validations, redirect back to the registration form
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: register-form.php");
    exit();
}

//Create INSERT query
$qry = "INSERT INTO members(firstname, lastname, email, username, password) VALUES('$firstname','$lastname','$email','$username','$password')";
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {
    header("location: register-success.php");
    exit();
}else {
    die("Query failed");
}
  ?>

并在页面

注册时使用此代码
<?php
//Start Session
session_start();
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
    echo '<ul class="err">';
    foreach($_SESSION['ERRMSG_ARR'] as $msg) {
        echo '<li>',$msg,'</li>'; 
    }
    echo '</ul>';
    unset($_SESSION['ERRMSG_ARR']);
}
?>