来自$ _POST之外的提交按钮的值

时间:2013-05-30 16:15:34

标签: php html registration

我的注册表单和验证它的脚本存在问题(检查它是否为空白)。具体来说,如果注册表单中有一个或多个字段留空,我的注册表单中的提交按钮的值会不断出现在错误消息中。

以下是我用于检查注册表单中空白的代码摘录。

case "Register":
    //Checks for blank fields
    var_dump($_POST);
    foreach($_POST as $fields=>$rvalue)
    {
        if($rvalue=="Button")
                {       
                    continue;
                }
        //Checks if fields other than rdesc are empty
        if($fields!=="rdesc")
        {

            if(empty($rvalue))
            {
                //stores the empty field into an array
                $blanks[]=$rvalue;
            }
            else
            {
                $good_data[$fields]=strip_tags(trim($rvalue));
            }
        }
    }
    //If there are bank fields, the following code will execute.
    if(isset($blanks))
    {
        $errormsg="The fields below cannot be blank.Please fill all the fields: ";
        extract($good_data);
        $errormsg.="$rvalue ";
        include("Register.php");
        exit();
    }

include("LoginPage.php");

?>

这是我的注册表格

<?php
$regfields=array("remail"=>"Email","remail2"=>"Re-enter Email","rusername"=>"Username","rpassword"=>"Password","rpassword2"=>"Re-enter Password","rbiz"=>"BizName","rdesc"=>"Desc");
?>

<html>
<body>
<h3>Registration</h3>
<form action="Login.php" name="RegisterForm" method="post" >
<table border="0">

<?php

if(isset($errormsg))
{
    echo"$errormsg";
}

foreach($regfields as $field=>$value)
{

    if($field=="rdesc")
    {
        echo"<tr>";
        echo"<td>";
        echo"<label for='$field'>$value: </label>";
        echo"</td>";
        echo"<td>";
        echo"<input type='text' name='$field' size='300' maxlength='300' style='width:400px;height:100px; \n' /></td></tr>";
        echo"<tr>";
        echo"<td>";
This Register value keeps showing up--->echo"<input type='submit' name='Button' value='Register'/>";
        echo"</td>";
        echo"<tr>";
    }
    else if($field=="remail"||$field=="remail2")
    {
        echo"<tr>";
        echo"<td>";
        echo"<label for='$field'>$value: </label>";
        echo"</td>";
        echo"<td>";
        echo"<input type='text' name='$field' size='40' maxlength='50' /></td></tr>";

    }
    else if($field=="rpassword"||$field=="rpassword2")
    {
        echo"<tr>";
        echo"<td>";
        echo"<label for='$field'>$value: </label>";
        echo"</td>";
        echo"<td>";
        echo"<input type='password' name='$field' size='40' maxlength='50' /></td></tr>";

    }
    else
    {
        echo"<tr>";
        echo"<td>";
        echo"<label for=$field>$value: </label>";
        echo"</td>";
        echo"<td>";
        echo"<input type='text' name='$field' size='40' maxlength='50'/>";
        echo"</td>";
        echo"</tr>";
    }
}

?>

我尝试使用var转储来查看$_POST内部的内容,我得到了:

array(8) { ["remail"]=> string(5) "Email" ["remail2"]=> string(5) "Email" ["rusername"]=> string(0) "" ["rpassword"]=> string(6) "Testpw" ["rpassword2"]=> string(6) "Testpw" ["rbiz"]=> string(8) "Testname" ["rdesc"]=> string(8) "TestDesc" ["Button"]=> string(8) "Register" } **Register***<---This is the register value i was talking about that is outside of the array. 

和错误消息

  

以下字段不能为空。请填写所有字段:注册

但是,当我填写注册表而没有任何空格时,var转储给了我:

array(8) { ["remail"]=> string(5) "Email" ["remail2"]=> string(5) "Email" ["rusername"]=> string(8) "TestUser" ["rpassword"]=> string(6) "Testpw" ["rpassword2"]=> string(6) "Testpw" ["rbiz"]=> string(8) "Testname" ["rdesc"]=> string(8) "TestDesc" ["Button"]=> string(8) "Register" }

数组之外的“Register”已经消失。

我添加了代码

if($rvalue=="Button")
{       
    continue;
}

尝试跳过提交按钮的“注册”值,但似乎没有效果。

有人可以告诉我如何从错误消息中删除“注册”值,以便正确显示留空的字段吗?

编辑:这些是完整的代码

的login.php     

在session_start();

开关($ _POST [“按钮”]) {     案例“登录”:         包括( “cxn.inc”);         $ cxn = mysqli_connect($ host,$ user,$ pw,$ db)或die(“无法连接数据库”);         $ query =“SELECT * FROM Users WHERE username ='$ _ POST [lusername]'”;         $ result = mysqli_query($ cxn,$ query)或die(“无法从Users表中找到用户名”);         $ NUM = mysqli_num_rows($结果);

        if($num>0)//Username is found
        {
            $query="SELECT * FROM Users WHERE Password='$_POST[lpassword]'";
            $result2=mysqli_query($cxn,$query)or die("Cant find Password");
            $num2=mysqli_num_rows($result2);
            if($num2>0)//Password is found
            {
                $_SESSION['Auth']="Yes";
                $_SESSION['Type']="Admin";
                header("Location:AdminMain.php");
            }
            else
            {
                echo"Invalid Username and/or Password";
            }

        }
        elseif($num==0)//Executes when Username not found in Users table
        {
            $query="SELECT * FROM SubUsers WHERE Username='$_POST[lusername]'";
            $result3=mysqli_query($cxn,$query) or die(" Cant find Username from SubUsers table");
            $num3=mysqli_num_rows($result3);
            if($num3>0)//Username is found
            {
                $query="SELECT * FROM SubUsers WHERE Password='$_POST[lpassword]'";
                $result4=mysqli_query($cxn,$query);
                $num4=mysqli_num_rows($result4);
                if($num4>0)//Password is found
                {
                    $_SESSION['Auth']="Yes";
                    $_SESSION['Type']="Sub";
                    header("Location:SubMain.php");
                }
                else
                {
                    echo"Cant find Password";
                }
            }
            else
            {
                echo"Username and/or Password is non-existent";
            }
        }
        else
        {
            echo"Invalid Username and/or Password entered";
        }


    break;

case "Register":
    //Checks for blank fields
    var_dump($_POST);
    foreach($_POST as $fields=>$rvalue)
    {
        if($fields=="Button"&& $rvalue == "Register")
                {       
                    continue;
                }
        //Checks if fields other than rdesc are empty
        if($fields!=="rdesc")
        {

            if(!empty($rvalue))
            {
                //stores the empty field into an array
                $blanks[]=$fields;
            }
            else
            {
                $good_data[$fields]=strip_tags(trim($rvalue));
            }
        }
    }

    if(!empty($blanks))
    {
        $errormsg="The fields below cannot be blank.Please fill all the fields: ";
        extract($good_data);
        $errormsg.="$rvalue ";
        include("Register.php");
        exit();
    }

}

包括( “LoginPage.php”);

&GT;

3 个答案:

答案 0 :(得分:2)

您正在循环中测试Button的字段,但事实上它是您需要测试的字段 name <input type="submit">发送的值为Register,这是您在print_r()中看到的值。

// Skip the submit button by name, not value
if($fields == "Button")
{       
   continue;
}

如果您有更多提交输入名为Button,您也可以测试该值,因为只有被点击的输入将被提交给$_POST中的PHP,尽管这里没有必要。

if($fields == "Button" && $rvalue == "Register")
{       
   continue;
}

接下来,如果您已将数组$blanks明确声明为:

$blanks = array();

...而不是允许通过

在第一个空白字段中隐式声明它
if(empty($rvalue))
{
    //stores the empty field into an array
    $blanks[]=$rvalue;
}

...然后您对isset($blanks)的后续检查将始终为true,并且foreach中迭代的 last 元素将附加到错误信息。在这种情况下,恰好是Register,即使它在循环体中被跳过。

相反,请通过empty()检查空数组。

if (!empty($blanks)) 
{
   $errormsg="The fields below cannot be blank.Please fill all the fields: ";
   // etc...
}

而不是在错误消息中使用$rvalue(它始终保持循环中的 last 数组元素,无论它在哪里失败),而不是内容{{1} }。

$blanks

答案 1 :(得分:1)

尝试

if($fields=="Button")
{       
    continue;
}

$ rvalue是值,您需要密钥;)

答案 2 :(得分:1)

这里的第一个逻辑问题是:

if(empty($rvalue))
        {
            //stores the empty field into an array
            $blanks[]=$rvalue;
        }

你将空虚存入$blanks[]:P你打算存储$fields

第二个问题: $errormsg.="$rvalue ";foreach($_POST as $fields=>$rvalue)的所有迭代结束时,$rvalue将保存$ _POST的最后一个元素的值,在这种情况下恰好是rdesc。因此,每次都会弹出提交按钮的值。

需要做的事情是这样的:

foreach($blanks as $field_left_blank)
{
 $errormsg.="$field_left_blank ,";
}
相关问题