提取表单数据(PHP)

时间:2015-05-08 00:25:00

标签: php forms validation

我正在为学校作业登录/注册表单,我无法在提交时获取表格以保留任何信息。无论信息是正确还是不正确,表单在提交后都会返回空白值。这是表单代码:

<?php
ini_set("display_errors","on");
error_reporting(E_ALL | E_STRICT);
$labels = array("email" => "Email Address:",
                "password" => "Password:");
$submit = "Log In";
?>

<?php
echo "<form method='post'>";
foreach($labels as $field => $label)
{
    if($field != "password")
    {
        echo "<div><label for='$field'>$label</label>
        <input type='text' name='$field' id='$field' width='40%' maxlength='40' value='".@$$value."'></div>";
    }
    else
    {
        echo "<div><label for='$field'>$label</label>
        <input type='password' name='$field' id='$field' width='40%' maxlength='20' value='".@$$value."'</div>";
    }
}
echo "<div><input type='hidden' name='submitted' value='yes'>
<input type='submit' value='$submit' name='submit'></div>";
?>

以下是验证码:

<?php

if(isset($_POST['submitted']) and $_POST['submitted'] == 'yes')
{
    foreach($_POST as $field => $value)
    {
        if(empty($value))
        {
            $error_array[] = $field;
        }
        else
        {
            $good_data[$field] = strip_tags(trim($value));
        }
    }
    if(@sizeof($error_array) > 0)
    {
        $message = "<p class='error'>Login information is incorrect</p>";
        echo $message;
        extract($good_data);
        include('login.php');
        exit();
    }
    else
    {
        foreach($good_data as $field => $value)
        {
            $clean_data[$field] = mysqli_real_escape_string($cxn,$value);
        }
        $sql = "select * from customerdata where email='$clean_data[email]' and password='$clean_data[password]'";
        $result = mysqli_query($cxn,$sql) or die("<p class='error'>Couldn't connect to server.</p>");
        $row = mysqli_fetch_assoc($result);
        if ($row > 0)
        {
            $sql2 = "update user_login set login_time=CURRENT_TIMESTAMP where email='$clean_data[email]'";
            $result2 = mysqli_query($cxn,$sql2) or die("<p class='error'>Couldn't connect to server.</p>");
            $_SESSION['auth'] = "yes";
            header("Location: catalog.php");
        }
        else
        {
            echo $message;
            extract($clean_data);
            include('login.php');
            exit();
        }
    }
}
else
{
    include("login.php");
}

1 个答案:

答案 0 :(得分:0)

如果你的输入表单中有@$$value,那么你首先要在php中解决错误,说明在某些包含实例中没有设置$ value。

如果在POST请求中收到用户名和密码,将其用户名和密码保存到自己的不同变量中,然后将这些变量反映到字段的值中,同样通过将$$设置为动态查找动态,这是可取的。命名变量,其值为$ value,而不是变量$ value。

我想你想要这样的东西 -

foreach($labels as $field => $label)
{
    $value = '';
    if(!empty($_POST[$field ]) {
        $value = $_POST[$field];
    }

    if($field != "password")
    {
        echo "<div><label for='$field'>$label</label>
        <input type='text' name='$field' id='$field' width='40%' maxlength='40' value='".$value."'></div>";
    }
    else
    {
        echo "<div><label for='$field'>$label</label>
        <input type='password' name='$field' id='$field' width='40%' maxlength='20' value='".$value."'</div>";
    }
}