0

我正在尝试验证在此脚本的第 3 步中的运输详细信息的文本字段中是否有一些输入,换句话说,它们没有留空。我苦苦挣扎的地方是放置验证脚本的地方。我不断在“processStep3”函数和“displayStep3”函数中添加一些东西,但还没有成功。如果表单只有一页,我了解如何验证表单,但现在我已将其分解为多个步骤,我不再了解我的脚本需要去哪里!

//已解决 我需要使用 if(empty($_POST["value"]) 将验证代码添加到 processStep3() 函数我怀疑这是做我正在做的事情的最佳方法,但它有效!

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <title>Mid-Term Practical</title>
        <link rel="stylesheet" type="text/css" href="common.css" />
      </head>
      <body>


    <?php

    if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"] <= 5 ) {
      call_user_func( "processStep" . (int)$_POST["step"] );
    } else {
      displayStep1();
    }


    function setValue( $fieldName ) {
      if ( isset( $_POST[$fieldName] ) ) {
        echo $_POST[$fieldName];
      }
    }

    function setChecked( $fieldName, $fieldValue ) {
      if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
        echo ' checked="checked"';
      }
    }

    function setSelected( $fieldName, $fieldValue ) {
      if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
        echo ' selected="selected"';
      }
    }

    function processStep1() {
      displayStep2();
    }


    function processStep2() {
      if ( isset( $_POST["submitButton"] ) and $_POST["submitButton"] == "< Back" ) {
        displayStep1();
      } else {
        displayStep3();
      }
    }

        function processStep3() {

    if( isset( $_POST["submitButton"] ) and $_POST["submitButton"] == "Cancel" ) {
    displayStep1();
  } elseif ( isset( $_POST["submitButton"] ) and $_POST["submitButton"] == "< Back" ){
    displayStep2();
  } else {
        if( empty($_POST["city"]) || empty($_POST["zip"]) || empty($_POST["email"])) {
            displayStep3();
            echo "

            <div style='float: left;'>
            <p style='background: red; color: white;'>Please enure all required fields (*) are filled out then re submit the form</p>
            </div>";
        } else {
            displayStep4();
        }

    }
}



    function processStep4() {
        if ( isset( $_POST["submitButton"] ) and $_POST["submitButton"] == "< Cancel" ) {
            displayStep1();
        } else {
            displayThanks();
        }
    }

    function displayStep1() {
    ?>
        <h1>Please Select an Item:</h1>

        <form action="midterm-practical.php" method="post">
          <div style="width: 40em;">
            <input type="hidden" name="step" value="1" />



            <label for="item1">Moroccan Mint: $9.99</label>
                <input type="radio" name="item" id="item1" value="Moroccan Mint"<?php setChecked( "item", "Moroccan Mint" )?>/>
                <input type="hidden" name="price" id="price1" value="9.99"<?php setValue( "price", "9.99" )?> />

            <label for="item2">Peppermint: $8.99</label>
                <input type="radio" name="item" id="item2" value="Peppermint"<?php setChecked( "item", "Peppermint" )?>/>
                <input type="hidden" name="price" id="price1" value="8.99"<?php setValue( "price", "8.99" )?> />

            <label for="item3">Citron Green: $9.99</label>
                <input type="radio" name="item" id="item3" value="Citron Green"<?php setChecked( "item", "Citron Green" )?>/>
                <input type="hidden" name="price" id="price1" value="9.99"<?php setValue( "price", "9.99" )?> />

            <label for="item4">All About Adam: $10.99</label>
                <input type="radio" name="item" id="item4" value="All About Adam"<?php setChecked( "item", "All About Adam" )?>/>
                <input type="hidden" name="price" id="price1" value="10.99"<?php setValue( "price", "10.99" )?> />

            <label for="item5">Detox Herbal: $12.99</label>
                <input type="radio" name="item" id="item5" value="Detox Herbal"<?php setChecked( "item", "Detox Herbal" )?>/>
                <input type="hidden" name="price" id="price1" value="12.99"<?php setValue( "price", "12.99" )?> />

            <label for="item6">Serenity: $7.99</label>
                <input type="radio" name="item" id="item6" value="Serenity"<?php setChecked( "item", "Serenity" )?>/>
                <input type="hidden" name="price" id="price1" value="7.99"<?php setValue( "price", "7.99" )?> />

            <label for="item7">Spiced Chai Black: $8.99</label>
                <input type="radio" name="item" id="item7" value="Spiced Chai Black"<?php setChecked( "item", "Spiced Chai Black" )?>/>
                <input type="hidden" name="price" id="price1" value="8.99"<?php setValue( "price", "8.99" )?> />

            <label for="item8">Spiced Chai Rooibos: $10.99</label>
                <input type="radio" name="item" id="item8" value="Spiced Chai Rooibos"<?php setChecked( "item", "Spiced Chai Rooibos" )?>/>
                <input type="hidden" name="price" id="price1" value="10.99"<?php setValue( "price", "10.99" )?> />

            <label for="item9">Chamomile: $6.99</label>
                <input type="radio" name="item" id="item9" value="Chamomile"<?php setChecked( "item", "Chamomile" )?>/>
                <input type="hidden" name="price" id="price1" value="6.99"<?php setValue( "price", "6.99" )?> />

            <label for="item10">Earl Grey Lavender: $11.99</label>
                <input type="radio" name="item" id="item10" value="Earl Grey Lavender"<?php setChecked( "item", "Earl Grey Lavender" )?>/>
                <input type="hidden" name="price" id="price1" value="11.99"<?php setValue( "price", "11.99" )?> />

            <div style="clear: both;">
              <input type="submit" name="submitButton" id="nextButton" value="Next &gt;" />
            </div>
          </div>
        </form>
    <?php
    }

    function displayStep2() {
    ?>
        <h1>Item Quantity</h1>

        <form action="midterm-practical.php" method="post">
          <div style="width: 30em;">
            <input type="hidden" name="step" value="2" />
            <input type="hidden" name="item" value="<?php setValue( "item" ) ?>" />
            <input type="hidden" name="price" value="<?php setValue( "price" ) ?>" />
            <input type="hidden" name="city" value="<?php setValue( "city" ) ?>" />
            <input type="hidden" name="state" value="<?php setValue( "state") ?>" />
            <input type="hidden" name="zip" value="<?php setValue( "zip" ) ?>" />
            <input type="hidden" name="email" value="<?php setValue( "email" ) ?>" />

            <label for="item">Item in Cart:</label> 

            <?php echo setValue( "item" ) ?>

            <label for="quantity">Please Choose a Quantity:</label>
                <input type="number" name="quantity" id="quantity" value="<?php setValue( "quantity" ) ?>" />



            <div style="clear: both;">
              <input type="submit" name="submitButton" id="nextButton" value="Next &gt;" />
              <input type="submit" name="submitButton" id="backButton" value="&lt; Back" style="margin-right: 20px;" />
            </div>
          </div>
        </form>
    <?php
    }

    function displayStep3() {


    ?>
        <h1>Your Cart and Shipping Details</h1>
        <form action="midterm-practical.php" method="post">
        <style type="text/css">
            .error{ background: #d33; color: white; padding: 0.2em; }
        </style>
          <div style="width: 30em;">
            <input type="hidden" name="step" value="3" />
            <input type="hidden" name="item" value="<?php setValue( "item" ) ?>" />
            <input type="hidden" name="price" value="<?php setValue( "price" )?>" />
            <input type="hidden" name="city" value="<?php setValue( "city" ) ?>" />
            <input type="hidden" name="state" value="<?php setValue( "state") ?>" />
            <input type="hidden" name="zip" value="<?php setValue( "zip" ) ?>" />
            <input type="hidden" name="email" value="<?php setValue( "email" ) ?>" />
            <input type="hidden" name="quantity" value="<?php setValue( "quantity" ) ?>" />

            <h4>Item(s) in Cart:</h4>
            <br />
            <p><?php echo setValue( "item" ) ?>
            <br />
            Quantity:<?php echo setValue( "quantity" ) ?>
            <br />
            Cost: $<?php echo setValue( "price" ) ?> ea.
            <br /></p>


            <h3>Please Enter Your Shipping Address</h3>
            <label for="city"> City *</label>
                <input type="text" name="city" id="city" value="<?php setValue( "city" )?>" />


            <label for="state">State *</label>
            <select name="state" id="state" size="1">
              <option value="Ohio"<?php setSelected( "state", "Ohio" ) ?>>Ohio</option>
              <option value="Kentucky"<?php setSelected( "state", "Kentucky" ) ?>>Kentucky</option>
              <option value="Indiana"<?php setSelected( "state", "Indiana" ) ?>>Indiana</option>
            </select>

            <label for="zip">Zip *</label>
                <input type="number" name="zip" id="zip" value="<?php setValue( "zip" )?>" />

            <label for="email">Email *</label>
                <input type="email" name="email" id="email" value="<?php setValue( "email" )?>" />


            <div style="clear: both;">
              <input type="submit" name="submitButton" id="nextButton" value="Finalize Order &gt;" />
              <input type="submit" name="submitButton" id="backButton" value="&lt; Back" />
              <input type="submit" name="submitButton" id="cancelButton" value="Cancel" style="margin-right: 20px;" />
            </div>
          </div>
        </form>
    <?php
    }

    function displayStep4() {
    $subTotal = $_POST["price"] * $_POST["quantity"];
    $tax = $subTotal * .06;
    $noTax = 0;
    $total = $subTotal + $tax;
    $ohio = "Ohio";
    ?>
        <h1>Process Order</h1>
        <form action="midterm-practical.php" method="post">
            <div style="width: 30em;">
                <input type="hidden" name="step" value="4" />
                <input type="hidden" name="item" value="<?php setValue( "item" ) ?>" />
                <input type="hidden" name="price" value="<?php setValue( "price" ) ?>" />
                <input type="hidden" name="city" value="<?php setValue( "city" ) ?>" />
                <input type="hidden" name="state" value="<?php setValue( "state") ?>" />
                <input type="hidden" name="zip" value="<?php setValue( "zip" ) ?>" />
                <input type="hidden" name="email" value="<?php setValue( "email" ) ?>" />
                <input type="hidden" name="quantity" value="<?php setValue( "quantity" ) ?>" />


                <p>
                    Sub Total: $<?php echo number_format($subTotal, 2) ?>
                    <br />
                    Tax: $<?php if ( $_POST["state"] == $ohio){
                    echo number_format($tax, 2);} else { echo number_format( $noTax, 0); };?>
                    <br />
                    Order Total: $<?php echo number_format($total, 2) ?>
                </p>



            <div style="clear: both;">
              <input type="submit" name="submitButton" id="sendOrder" value="Process Order &gt;" />
              <input type="submit" name="submitButton" id="backButton" value="&lt; Cancel" style="margin-right: 20px;" />
            </div>
          </div>
        </form>

    <?php
    }

    function displayThanks() {
    $date = date('m-d-Y', strtotime('+5 days'));
    $subTotal = $_POST["price"] * $_POST["quantity"];
    $tax = $subTotal * .06;
    $noTax = 0;
    $total = $subTotal + $tax;
    $ohio = "Ohio";

    ?>
        <form action="midterm-practical.php" method="post">
            <div style="width: 30em;">
                <input type="hidden" name="step" value="4" />
                <input type="hidden" name="item" value="<?php setValue( "item" ) ?>" />
                <input type="hidden" name="price" value="<?php setValue( "price" ) ?>" />
                <input type="hidden" name="city" value="<?php setValue( "city" ) ?>" />
                <input type="hidden" name="state" value="<?php setValue( "state") ?>" />
                <input type="hidden" name="zip" value="<?php setValue( "zip" ) ?>" />
                <input type="hidden" name="email" value="<?php setValue( "email" ) ?>" />
                <input type="hidden" name="quantity" value="<?php setValue( "quantity" ) ?>" />


        <h1>Thank You</h1>
        <p>You ordered <?php echo setValue( "quantity" ) ?> boxes of <?php echo setValue("item")?> Tea
        <br />
        <br />
        <strong>Total Cost:</strong> <br />

                    Sub Total: $<?php echo number_format($subTotal, 2) ?>
                    <br />
                    Tax: $<?php if ( $_POST["state"] == $ohio){
                    echo number_format($tax, 2);} else { echo number_format( $noTax, 0); };?>
                    <br />
                    Order Total: $<?php echo number_format($total, 2) ?>
                </p>




        <p>Your order has been received.
        Your item(s) should arrive by <br /><?php echo $date?>. </p>

            </div>
        </form>
    <?php
    }
    function displayError() {
    ?>
    <form action="midterm-practical.php" action="post">
    <h1>There seems to be an issue with form you submitted.  Please make sure all required fields(*) are filled out.</h1>
    </form>
    <?php
    }
    ?>

      </body>
    </html>
4

1 回答 1

0

有两种使用 PHP 的方法。

最常见的一种是在同一个脚本上执行所有操作,这将要求您验证同一个文件中的每个表单。

如果您是编程新手,另一种最不常见但更高效且有点困难的是OOP(面向对象编程)方式。

如果您刚开始,请尝试第一个。

如果您有验证脚本,则可以通过以下命令将其包含在其他文件中:

require_once 'validation.php';

当然假设您的验证脚本确实称为验证。

每次有这样的提交时,您都可以调用每个函数:

//Call function to validate email: 

require_once 'validation.php';

if(isset($_POST['submitForm'])){
    //call your functions to validate here
    validateEmail($_POST['usermail']);
}

一旦你掌握了它,这很简单。

于 2013-10-29T17:24:29.120 回答