Chi Shen Chi Shen - 2 days ago 4
PHP Question

how to auto assign it is a null and send to php

here is my code for the form for user input data.

<?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>


and here is the data receriver php code in another page

<?php

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

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

?>


what i wan to do is if the user did not enter any value and press submit...
it will automatically fill in NULL in the data...
so can i do it??
for example....
Forename : James..
Surname : Null (if the user did not enter anything in the surname textfield)...

Answer

If you want it to be NULL, as in empty:

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

If you want the actual value to be "NULL", as in string(4) then:

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

UPDATE:

<?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>
Comments