このスクリプトのステップ 3 で、配送の詳細のテキスト フィールドに何らかの入力があったこと、つまり空欄になっていないことを確認しようとしています。私が苦労しているのは、検証スクリプトをどこに置くかです。「processStep3」関数と「displayStep3」関数に何かを入れることを行ったり来たりしていますが、まだ成功していません。フォームが 1 ページだけの場合にフォームを検証する方法は理解していますが、これを複数のステップに分割したため、スクリプトがどこに行く必要があるのか わかりません!
//解決済み 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 >" />
</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 >" />
<input type="submit" name="submitButton" id="backButton" value="< 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 >" />
<input type="submit" name="submitButton" id="backButton" value="< 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 >" />
<input type="submit" name="submitButton" id="backButton" value="< 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>