如何自动分配它是一个null并发送到PHP

时间:2016-04-07 14:48:44

标签: php

这是我的用户输入数据表单的代码。

<?php
// Start the session
session_start();

?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
</head>
<body>

<?php 


//define variable and set to empty value

$forenameErr = "";
$forename = "";

if($_SERVER["REQUEST_METHOD"] =="POST"){
    $valid = true;

    if(empty($_POST["forename"])){
        $forenameErr = "Forename is required";
         $valid = false; //false
    } else {
        $forename = test_input($_POST["forename"]);

        // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$forename)) {
       $forenameErr = "Only letters and white space allowed";
    }
    }

     //if valid then redirect

     if($valid){
     $_SESSION['forename'] = $forename;
     header('Location: testing2.php');
     exit();
}   
}

//check

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>
<div id="wrapper">

<h1>Welcome to Chollerton Tearoom! </h1>

<form id = "userdetail" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

    <fieldset id="aboutyou">
    <legend id="legendauto">user information</legend>

        <p>
        <label for="forename">Forename: </label>
        <input type="text" name="forename" id="forename" value="<?php echo $forename;?>">
        <span class="error">* <?php echo $forenameErr;?></span>
        </p>

        <p> 
        <input type="submit" name="submit" value="submit" />
        </p>

       </fieldset>
    </form>
    </div>    
</body>
</html>

,这是另一页中的数据接收者php代码

<?php 

session_start();
$forename = $_SESSION['forename'];

echo "<h1>Successfull submission :</h1>";
echo "<p>Forename :  $forename; <p/>";

?>

我要做的是,如果用户没有输入任何值并按提交... 它会自动在数据中填入NULL ... 我可以这样做吗? 例如.... 姓:詹姆斯.. 姓氏:空(如果用户没有在姓氏文本字段中输入任何内容)......

3 个答案:

答案 0 :(得分:1)

如果你想让它为NULL,就像在空:

$forename = empty($_POST["forename"]) ? NULL : $_POST["forename"];

如果您希望实际值为“NULL”,如string(4)那么:

$forename = empty($_POST["forename"]) ? "NULL" : $_POST["forename"];

<强>更新

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Page Title Goes Here</title>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="form1.css"/>
    </head>
    <body>
        <?php

            // define variables and set to empty value
            $firstNameError = "";
            $lastNameError = "";
            $error = false;

            // if firstName is empty, make it NULL, else, test_input() the data.
            $firstName = empty($_POST["firstName"]) ? NULL : test_input($_POST["firstName"]);

            // if lastName is empty, make it NULL, else, test_input() the data.
            $lastName = empty($_POST["lastName"]) ? NULL : test_input($_POST["lastName"]);

            if (isset($_POST["submittingForm"])) {

                /// CHECK FIRST NAME ERRORS
                if ($firstName === NULL) {
                    // firstName is empty
                    $firstNameError = "First name is required!";
                    $error = true;

                } else {
                    // check characters
                    if (!preg_match("/^[a-zA-Z ]*$/", $firstName)) {
                        $firstNameError = "Only letters and white spaces allowed!";
                        $error = true;
                    }
                }

                /// CHECK LAST NAME ERRORS
                if (!preg_match("/^[a-zA-Z ]*$/", $lastName)) {
                    // check characters
                    $lastNameError = "Only letters and white spaces allowed!";
                    $error = true;
                }

                // if no error then redirect
                if (!$error) {
                    $_SESSION['fistName'] = $firstName;
                    $_SESSION['lastName'] = $lastName;
                    header('Location: testing2.php');
                    exit();
                }

            } else {
                // user did not submit form!
            }

            // clean input
            function test_input($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }

        ?>
        <div id="wrapper">
            <h1>Welcome to Chollerton Tearoom! </h1>
            <form id="userdetail" method="POST">
                <fieldset id="aboutyou">
                    <legend id="legendauto">user information</legend>
                    <p>
                        <label for="firstName">First Name: </label>
                        <input type="text" name="firstName" id="firstName" value="<?php echo $firstName; ?>">
                        <span class="error">* <?php echo $firstNameErr;?></span>

                        <label for="lastName">Last Name: </label>
                        <input type="text" name="lastName" id="lastName" value="<?php echo $lastName; ?>">
                        <span class="error">* <?php echo $lastNameErr;?></span>
                    </p>
                    <p> 
                        <input type="submit" name="submittingForm" value="submit">
                    </p>
                </fieldset>
            </form>
        </div>
    </body>
</html>

答案 1 :(得分:0)

if ($forename == ""){
    $forename = "NULL";
}

答案 2 :(得分:0)

PHP代码段

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($data == '') {
        $data = 'NULL';
    }
    return $data;
}