简体   繁体   中英

PHP $_POST Form validation and Postback

I am working on an assignment for my PHP1 Class and we are working on sticky forms, my assignment is to write an order form that validates that both a name is entered and a phone model is selected and if both are filled posts that data back to the page and if one or both is missing an error message is posted back to the page. Accessories are Optional. Currently the script will post an error if no phone is selected and a name is input into the form, it will post an error if both are missing, but if a name is missing and a phone is selected then it will not flag as an error and continue processing the script back to the page. I attempted to right a function to validate that both the userName text field AND a phones radio button are selected to be true or if false then the error message is presented. Can anyone tell me why my form is processing the data when only a phone model is selected and the name field is blank?

Script(OrderForm):

<!DOCTYPE html>
<html>
<head>
    <title>Order Form</title>
</head>
<body>
<h1>Order Your Smartphone</h1>
<?php
/**
 * Created by PhpStorm.
 * User: Daniel Vermillion
 * Date: 10/27/2014
 * Time: 7:59 PM
 */

$isValid = false;

//function totalAcc() {
//    foreach($_POST['acc'] as $item) {
//        $accPrice[] = $item;
//    }
//    array_sum($accPrice);
//    return $accPrice;
//}

//function totalCost() {
//    $subtotal = $phonePrice + $accPrice;
//    $tax = 0.08;
//    $taxTotal = $subtotal * $tax;
//    $total = $subtotal + $taxTotal;
//    return $subtotal;
//    return $taxTotal;
//    return $total;
//}

function validData() {
    if(isset($_POST['userName']) && isset($_POST['phones'])) {
        return true;
    }
    else {
        return false;
    }
}

function calcResults() {
    $isValid = validData();
    if($isValid) {
        echo "Full Name: {$_POST['userName']} <br />";
        echo "Phone Model: {$_POST['phones']} <br />";
        echo "Accessories: {$_POST['acc']} <br />";
//        echo "Subtotal: $subtotal <br />";
//        echo "Tax: '$taxTotal' <br />";
//        echo "Total Cost: $total <br />";
    }
    else {
        echo "Please enter your name and select a phone model.";
    }
}

?>
<form method="post" action="index.php">
    Full Name: <input type="text" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>" /><br />
    <h4>Add Smartphone</h4>
    <table cellspacing="4" cellpadding="4" border="1">
        <tr>
            <td></td>
            <td>Phone</td>
            <td>Model</td>
            <td>Storage</td>
            <td>Price</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="SP8" <?php if(isset($_POST['phones']) && $_POST['phones'] == "SP8") echo 'checked'; ?> /></td>
            <td>SuperPhone</td>
            <td>SP8</td>
            <td>8 GB</td>
            <td>$400</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="SP16" <?php if(isset($_POST['phones']) && $_POST['phones'] == "SP16") echo 'checked'; ?> /></td>
            <td>SuperPhone</td>
            <td>SP16</td>
            <td>16 GB</td>
            <td>$450</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="MP8" <?php if(isset($_POST['phones']) && $_POST['phones'] == "MP8") echo 'checked'; ?> /></td>
            <td>MegaPhone</td>
            <td>MP8</td>
            <td>8 GB</td>
            <td>$500</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="MP16" <?php if(isset($_POST['phones']) && $_POST['phones'] == "MP16") echo 'checked'; ?> /></td>
            <td>MegaPhone</td>
            <td>MP16</td>
            <td>16 GB</td>
            <td>$550</td>
        </tr>
    </table>
    <h4>Add Accessories</h4>
    <table cellspacing="4" cellpadding="4" border="1">
        <tr>
            <td></td>
            <td>Accessory</td>
            <td>Price</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="acc[]" value="handstrap" <?php if(isset($_POST['acc']) && in_array('handstrap', $_POST['acc'])) echo ' checked'; ?> /></td>
            <td>Hand Strap</td>
            <td>$6.25</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="acc[]" value="leathercase" <?php if(isset($_POST['acc']) && in_array('leathercase', $_POST['acc'])) echo ' checked'; ?> /></td>
            <td>Leather Case</td>
            <td>$14.50</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="acc[]" value="headphones" <?php if(isset($_POST['acc']) && in_array('headphones', $_POST['acc'])) echo ' checked'; ?> /></td>
            <td>Headphones</td>
            <td>$18.75</td>
        </tr>

    </table>
    <br />
    <input type="submit" name="submit" value="Click to Finalize Order" /><br /><br />
</form>
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    calcResults();
}

?>
</body>
</html>

isset() for strings returns true for an empty string. https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

Try Empty()

edit: please note that if the field has a space in it, it will not be counted as empty. You should probably use Trim() on the result to ensure that there is no whitespace.

You need to echo the result..

replace

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    calcResults();
}

with

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo calcResults();
}

UPDATE:

<!DOCTYPE html>
<html>
<head>
    <title>Order Form</title>
</head>
<body>
<h1>Order Your Smartphone</h1>
<?php
/**
 * Created by PhpStorm.
 * User: Daniel Vermillion
 * Date: 10/27/2014
 * Time: 7:59 PM
 */

$isValid = false;

//function totalAcc() {
//    foreach($_POST['acc'] as $item) {
//        $accPrice[] = $item;
//    }
//    array_sum($accPrice);
//    return $accPrice;
//}

//function totalCost() {
//    $subtotal = $phonePrice + $accPrice;
//    $tax = 0.08;
//    $taxTotal = $subtotal * $tax;
//    $total = $subtotal + $taxTotal;
//    return $subtotal;
//    return $taxTotal;
//    return $total;
//}

function validData() {
    if(isset($_POST['userName']) && !empty($_POST['userName'])) {
        if(isset($_POST['phones']) && !empty($_POST['phones'])) {
            $acc = (isset($_POST['acc']) && !empty($_POST['acc'])) ? " <br />Accessories:  " . implode(" and ",$_POST['acc']) . " <br />" : "";
            return "Full Name: " . $_POST['userName'] . " <br />Phone Model:  " . $_POST['phones'] . $acc;
        } else {
            return "Please enter the phone model.";
        }
    } else {
        return "Please enter your name and select a phone model.";
    }
}

function calcResults() {
    $isValid = validData();
    return $isValid;
}

?>
<form method="post" action="form.php">
    Full Name: <input type="text" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>" /><br />
    <h4>Add Smartphone</h4>
    <table cellspacing="4" cellpadding="4" border="1">
        <tr>
            <td></td>
            <td>Phone</td>
            <td>Model</td>
            <td>Storage</td>
            <td>Price</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="SP8" <?php if(isset($_POST['phones']) && $_POST['phones'] == "SP8") echo 'checked'; ?> /></td>
            <td>SuperPhone</td>
            <td>SP8</td>
            <td>8 GB</td>
            <td>$400</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="SP16" <?php if(isset($_POST['phones']) && $_POST['phones'] == "SP16") echo 'checked'; ?> /></td>
            <td>SuperPhone</td>
            <td>SP16</td>
            <td>16 GB</td>
            <td>$450</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="MP8" <?php if(isset($_POST['phones']) && $_POST['phones'] == "MP8") echo 'checked'; ?> /></td>
            <td>MegaPhone</td>
            <td>MP8</td>
            <td>8 GB</td>
            <td>$500</td>
        </tr>
        <tr>
            <td><input type="radio" name="phones" value="MP16" <?php if(isset($_POST['phones']) && $_POST['phones'] == "MP16") echo 'checked'; ?> /></td>
            <td>MegaPhone</td>
            <td>MP16</td>
            <td>16 GB</td>
            <td>$550</td>
        </tr>
    </table>
    <h4>Add Accessories</h4>
    <table cellspacing="4" cellpadding="4" border="1">
        <tr>
            <td></td>
            <td>Accessory</td>
            <td>Price</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="acc[]" value="handstrap" <?php if(isset($_POST['acc']) && in_array('handstrap', $_POST['acc'])) echo ' checked'; ?> /></td>
            <td>Hand Strap</td>
            <td>$6.25</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="acc[]" value="leathercase" <?php if(isset($_POST['acc']) && in_array('leathercase', $_POST['acc'])) echo ' checked'; ?> /></td>
            <td>Leather Case</td>
            <td>$14.50</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="acc[]" value="headphones" <?php if(isset($_POST['acc']) && in_array('headphones', $_POST['acc'])) echo ' checked'; ?> /></td>
            <td>Headphones</td>
            <td>$18.75</td>
        </tr>

    </table>
    <br />
    <input type="submit" name="submit" value="Click to Finalize Order" /><br /><br />
</form>
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo calcResults();
}

?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM