所需的示例輸出是(在同一頁面上):
提交前
- 襯衫訂單下拉選單(帶子、尺碼、顏色、款式)
聯系表
- 姓名: _
- 電子郵件: _
- 電話: _
- 地址:_(包括城市、州和郵編)
- 提交/重置按鈕
提交后
如果錯誤:
- 顯示包含客戶已填寫的資訊的聯系表。
- 顯示錯誤。
如果沒有錯誤:
謝謝您的訂單!
- 在此處從下拉選單中訂購資訊 <示例:大號黑色 AJR 連帽衫>
運輸和聯系資訊:
姓名: _
電子郵件: _
電話: _
地址:_(包括城市、州和郵編)
在底部提交另一個表單的鏈接
我是新來的,所以如果我誤解了如何在此處提交問題,請指出正確的方向。另外,對不起,這太長了。
我正在處理一個 PHP 表單,該表單包含產品選擇的下拉串列以及客戶資訊的欄位。我需要在單個頁面上處理它,而不是在單獨的頁面上處理它。因此,在提交資訊時,需要處理并顯示提交的資訊并附上感謝資訊。此外,如果表格未完整填寫或需要填寫的方式,則應為不正確的欄位顯示錯誤。但是,用戶輸入的資訊仍應保留。
我的作業和問題:我在下面分享了我的代碼(再次,抱歉,它應該全部在一頁上)。我仍然遇到的問題是:
1. I am getting warning errors stating that I have "Undefined array keys". I get this before and after the form has been submitted. I have defined the arrays at the top so I am unsure why they are undefined.
2. The t-shirt drop-down options are not retaining any selected information if selected. I have tried creating the arrays for this information in the form code itself and at the top and can't seem to get it to stick. I have also tried another coding option of echoing out the selected option and putting the selection in the $_POST or the $_GET.
3. My error trapping is not working if I enter an incorrect phone number format. I initially get the error stating to put it in the right format, but if I put the information in the correct format and submit I still get the error and the phone number resets to what it was initially. I am getting that it is pulling it from the $_GET, but not sure why when it is submitted again it is not updating the $_GET to populate it correctly.
4.最后,表單和正確提交后應填充的資訊同時顯示在頁面上。它應該是表格,直到正確填寫所有內容,然后是感謝訊息,并在下面提交資訊作為確認。我已經嘗試將這些都用 if(empty....) 放入 PHP 中,如果按鈕沒有被按下,則回顯表單并檢查錯誤,如果它已被按下以檢查錯誤,如果沒有顯示感謝您提交資訊。但是,如果不正確,我無法使錯誤捕獲起作用,也無法獲取表單資訊以保留在表單中。
也歡迎任何關于如何縮短我的代碼或格式的建議。先感謝您!
<?php
//arrays
$bands = array ("ACDC", "Journey", "Modest Mouse", "Band of Horses", "Vampire Weekend", "Of Monsters and Men", "Broken Bells", "Phoenix", "Fleetwood Mac", "AJR",);
$colors = array ("Black", "Navy", "Red", "Orange", "Pink", "Yellow", "Green", "Gray", "White", "Purple",);
$sizes = array ("X-Small", "Small", "Medium", "Large", "X-Large", "XX-Large", "XXX-Large",);
$styles = array ("Tank Top", "T-Shirt", "Long Sleeve", "Hoodie", "Sweatshirt", "Jacket",);
$product_vars = array(
array( 'element_name' => 'band', 'title' => 'Band', 'options' => $bands, 'validation' => 'style', 'placeholder' => 'Choose One', ),
array( 'element_name' => 'color', 'title' => 'Color', 'options' => $colors, 'validation' => 'style', 'placeholder' => 'Choose One', ),
array( 'element_name' => 'size', 'title' => 'Size', 'options' => $sizes, 'validation' => 'style', 'placeholder' => 'Choose One', ),
array( 'element_name' => 'style', 'title' => 'Style', 'options' => $styles, 'validation' => 'style', 'placeholder' => 'Choose One', ),
);
$address_vars = array(
array( 'element_name' => 'firstName', 'title' => 'First Name', 'validation' => 'text',),
array( 'element_name' => 'lastName', 'title' => 'Last Name', 'validation' => 'text',),
array( 'element_name' => 'email', 'title' => 'Email Address', 'validation' => 'email',),
array( 'element_name' => 'phone', 'title' => 'Phone Number', 'validation' => 'phone',),
array( 'element_name' => 'address', 'title' => 'Address', 'validation' => 'text',),
array( 'element_name' => 'city', 'title' => 'City', 'validation' => 'text',),
array( 'element_name' => 'state', 'title' => 'State', 'validation' => 'text',),
array( 'element_name' => 'zip', 'title' => 'Zip Code', 'validation' => 'zip',),
);
if(empty($_POST['submit-button'])) //button has NOT been pushed
{
?>
<form action="" method="post">
<?php
foreach($product_vars as $cur_product) {
?>
<label><?php echo $cur_product['title']; ?>: </label>
<select name="<?php echo $cur_product['element_name']; ?>" size="1">
<option><?php echo $cur_product['placeholder']; ?></option>
<?php
foreach($cur_product['options'] as $cur_option)
{
echo "<option value = '".$cur_option."' ".(isset($_POST[$cur_product['element_name']]) && $_POST[$cur_product['element_name']] == $cur_option ? "selected='selected'" : "")."> $cur_option </option>";
}
?>
</select>
<?php
}
foreach($address_vars as $cur_address_field) {
?>
<label><?php echo $cur_address_field['title']; ?>:
<input
type = "text"
value="<?php echo (isset($_POST[$cur_address_field['element_name']]) ? $_POST[$cur_address_field['element_name']] : ''); ?>"
id = "<?php echo $cur_address_field['element_name']; ?>"
placeholder = "<?php echo $cur_address_field['title']; ?>"
name ="<?php echo $cur_address_field['element_name']; ?>">
</label>
<?php } ?>
<input type="reset" class="buttons">
<input type="submit" name = "submit-button" class="buttons">
</form>
<?php
}
if(!empty($_POST['submit-button']))
foreach($product_vars as $validate_field)
{
$messages = validate_field_contents($_POST[$validate_field['element_name']], $validate_field['title'], $validate_field['validation'], $messages);
}
foreach($address_vars as $validate_field)
{
$message = validate_field_contents($_POST[$validate_field['element_name']], $validate_field['title'], $validate_field['validation'], $message);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>T-Shirt Form</title>
<link type="text/css" rel="stylesheet" href="css/style-prod.css">
<script src="..js/script.js" defer></script>
</head>
<body>
<?php
if($message) {
echo '<p>'.$message.'</p>';
}
else {
?>
<!--display processed information here-->
<h3 class="subheaderone">Thank you for your order!</h3><br>
<h3 class = "subheadertwo">Product:</h3>
<div class = "output">
<?php
foreach($product_vars as $cur_product) {
echo "<option value = '".$cur_option."' ".(isset($_POST[$cur_product['element_name']]) && $_POST[$cur_product['element_name']] == $cur_option ? "selected='selected'" : "")."> $cur_option </option>";
}
?>
<h3 class = "subheadertwo">Shipping & Contact Information:</h3>
<?php
foreach($address_vars as $cur_address) {
echo '<p>' . $cur_address['title'] . ': ' . (isset($_POST[$cur_address['element_name']]) ? $_POST[$cur_address['element_name']] : '') . '</p>';
}
?>
</div>
<div class="another">
<nav><a href="./products.php"> Submit Another Form</a></nav>
</div>
<?php } ?>
</body>
</html>
<?php
function validate_field_contents($content, $title, $type, $message, ) {
if($type == 'text') {
$string_exp = "/^[A-Za-z0-9._%-]/";
if(!preg_match($string_exp, $content)) {
$message .= '<li>Please enter a valid ' . strtolower($title) . '.</li>';
}
}
elseif($type == 'email') {
$email_exp = '/^[A-Za-z0-9._%-] @[A-Za-z0-9.-] \.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $content)) {
$message .= '<li>Please enter a valid ' . strtolower($title) . '.</li>';
}
}
elseif($type == 'phone'){
$num_exp = "/^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/";
if(!preg_match($num_exp,$content)) {
{$message .= '<li>Please enter a valid ' . strtolower($title) . ' in the following format (xxx)xxx-xxxx.</li>';}
}
}
elseif($type == 'zip'){
$num_exp = "/^[0-9]{5}$/";
if(!preg_match($num_exp,$content)) {
{$message .= '<li>Please enter a valid ' . strtolower($title) . ' with 5 numbers only.</li>';}
}
}
elseif(empty($_POST['band'])){
{$message .= '<li>Please select a ' . strtolower($title) . '.</li>';}
}
elseif(empty($type == 'color')){
$message .= '<li>Please select a ' . strtolower($title) . '.</li>';
}
elseif(empty($type == 'size')){
$message .= '<li>Please select a ' . strtolower($title) . '.</li>';
}
elseif(empty($type == 'style')){
$message .= '<li>Please select a ' . strtolower($title) . '.</li>';
}
return $message;
}
?>
uj5u.com熱心網友回復:
好的,這里有幾件事:
您不需要頂部的變數分配。這在 C 和其他語言中非常重要,但 PHP 不需要。
您的
if(isset($_GET['firstName'])) $firstName = $_GET['firstName'];宣告正在使用,$_GET而您的<form>標簽正在使用$_POST- 這是您的主要問題我建議使用一組變數,如下所示:
$address_vars = array( array( 'element_name' => 'firstName', 'title' => 'First Name', 'validation' => 'text', ), array( 'element_name' => 'lastName', 'title' => 'Last Name', 'validation' => 'text', ), );
然后您可以像這樣以編程方式輸出欄位:
foreach($address_vars as $cur_address_field) {
?>
<label><?php echo $cur_address_field['title']; ?>: <input type = "text" value="<?php echo (isset($_POST[$cur_address_field['element_name']]) ? $_POST[$cur_address_field['element_name']] : ''); ?>" id = "<?php echo $cur_address_field['element_name']; ?>" placeholder = "<?php echo $cur_address_field['title']; ?>" name ="<?php echo $cur_address_field['element_name']; ?>"></label>
<?php
}
這不僅大大清理了代碼,而且還使更改/更新甚至添加到欄位變得更加容易(和安全)。
然后,要驗證欄位,您可以使用validation我設定為開關的陣列鍵來瀏覽所有欄位。像這樣的東西:
foreach($address_vars as $validate_field) {
if($validate_field['validation'] == 'text') {
// this is a text-based field, so we check it against the text-only regex
$string_exp = "/^[A-Za-z .'-] $/";
if(!preg_match($string_exp, $_POST[$validate_field['element_name']])) {
$message .= '<li>Please enter a ' . strtolower($validate_field['title']) . ' containing letters and spaces only.</li>';
}
} elseif($validate_field['validation'] == 'email') {
$email_exp = '/^[A-Za-z0-9._%-] @[A-Za-z0-9.-] \.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $_POST[$validate_field['element_name']])) {
$message .= '<li>Please enter a valid ' . strtolower($validate_field['title']) . '.</li>';
}
}
}
您沒有正確連接您的產品欄位。用戶提交的值無法“粘貼”在這里,因此該欄位的值會丟失。首先,我們可以將所有這些放在一個陣列中,類似于我們對地址欄位所做的:
$product_vars = array( array( 'element_name' => 'band', 'title' => 'Band', 'options' => $bands, 'placeholder' => '選擇一個', ), array( 'element_name' => 'color', 'title' => 'Color', 'options' => $colors, 'placeholder' => '選擇一個', ), );
然后我們可以對表單使用以下內容:
foreach($product_vars as $cur_product) {
?>
<label><?php echo $cur_product['title']; ?>: </label>
<select name="<?php echo $cur_product['element_name']; ?>" size="1">
<option><?php echo $cur_product['placeholder']; ?></option>
<?php
foreach($cur_product['options'] as $cur_option)
{
echo "<option value = '".$cur_option."' ".(isset($_POST[$cur_product['element_name']]) && $_POST[$cur_product['element_name']] == $cur_option ? "selected='selected'" : "")."> $cur_option </option>";
}
?>
</select>
<?php
}
希望這會有所幫助!
**由于共享新代碼而進行編輯**
這里又發生了一些事情。主要專案是:$product_varsforeach 在<form>標簽之外,盡管其余的代碼是這樣設定的,但<form>標簽沒有被設定為使用$_POST,并且沒有添加任何驗證型別。我已經粘貼了我擁有的完整代碼,這對我有用。
<?php
//arrays
$bands = array ("ACDC", "Journey", "Modest Mouse", "Band of Horses", "Vampire Weekend", "Of Monsters and Men", "Broken Bells", "Phoenix", "Fleetwood Mac", "AJR",);
$colors = array ("Black", "Navy", "Red", "Orange", "Pink", "Yellow", "Green", "Gray", "White", "Purple",);
$sizes = array ("X-Small", "Small", "Medium", "Large", "X-Large", "XX-Large", "XXX-Large",);
$styles = array ("Tank Top", "T-Shirt", "Long Sleeve", "Hoodie", "Sweatshirt", "Jacket",);
$product_vars = array(
array( 'element_name' => 'band', 'title' => 'Band', 'options' => $bands, 'placeholder' => 'Choose One', ),
array( 'element_name' => 'color', 'title' => 'Color', 'options' => $colors, 'placeholder' => 'Choose One', ),
array( 'element_name' => 'size', 'title' => 'Size', 'options' => $sizes, 'placeholder' => 'Choose One', ),
array( 'element_name' => 'style', 'title' => 'Style', 'options' => $styles, 'placeholder' => 'Choose One', ),
);
$address_vars = array(
array( 'element_name' => 'firstName', 'title' => 'First Name', 'validation' => 'text',),
array( 'element_name' => 'lastName', 'title' => 'Last Name', 'validation' => 'text',),
array( 'element_name' => 'email', 'title' => 'Email Address', 'validation' => 'email',),
array( 'element_name' => 'phone', 'title' => 'Phone Number', 'validation' => 'phone',),
array( 'element_name' => 'address', 'title' => 'Address', 'validation' => 'address',),
array( 'element_name' => 'city', 'title' => 'City', 'validation' => 'text',),
array( 'element_name' => 'state', 'title' => 'State', 'validation' => 'text',),
array( 'element_name' => 'zip', 'title' => 'Zip Code', 'validation' => 'zip',),
);
?>
<form action="" method="post">
<?php
foreach($product_vars as $cur_product) {
?>
<label><?php echo $cur_product['title']; ?>: </label>
<select name="<?php echo $cur_product['element_name']; ?>" size="1">
<option><?php echo $cur_product['placeholder']; ?></option>
<?php
foreach($cur_product['options'] as $cur_option)
{
echo "<option value = '".$cur_option."' ".(isset($_POST[$cur_product['element_name']]) && $_POST[$cur_product['element_name']] == $cur_option ? "selected='selected'" : "")."> $cur_option </option>";
}
?>
</select>
<?php
}
foreach($address_vars as $cur_address_field) {
?>
<label><?php echo $cur_address_field['title']; ?>:
<input
type = "text"
value="<?php echo (isset($_POST[$cur_address_field['element_name']]) ? $_POST[$cur_address_field['element_name']] : ''); ?>"
id = "<?php echo $cur_address_field['element_name']; ?>"
placeholder = "<?php echo $cur_address_field['title']; ?>"
name ="<?php echo $cur_address_field['element_name']; ?>">
</label>
<?php } ?>
<input type="reset" class="buttons">
<input type="submit" class="buttons">
</form>
<?php
foreach($address_vars as $validate_field) {
$message = validate_field_contents($_POST[$validate_field['element_name']], $validate_field['title'], $validate_field['validation'], $message);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>T-Shirt Form</title>
<link type="text/css" rel="stylesheet" href="css/style-prod.css">
<script src="..js/script.js" defer></script>
</head>
<body>
<?php
if($message) {
echo '<p>'.$message.'</p>';
} else {
?>
<!--display processed information here-->
<h3 class="subheaderone">Thank you for your order!</h3><br>
<h3 class = "subheadertwo">Product:</h3>
<div class = "output">
<h3 class = "subheadertwo">Shipping & Contact Information:</h3>
<?php foreach($address_vars as $cur_address) {
echo '<p>' . $cur_address['title'] . ': ' . (isset($_POST[$cur_address['element_name']]) ? $_POST[$cur_address['element_name']] : '') . '</p>';
} ?>
</div>
<div class="another">
<nav><a href="./products.php"> Submit Another Form</a></nav>
</div>
<?php } ?>
</body>
</html>
<?php
function validate_field_contents($content, $title, $type, $message) {
if($type == 'text') {
// this is a text-based field, so we check it against the text-only regex
$string_exp = "/^[A-Za-z .'-] $/";
if(!preg_match($string_exp, $content)) {
$message .= '<li>Please enter a valid ' . strtolower($title) . ' containing letters and spaces only.</li>';
}
} elseif($type == 'email') {
$email_exp = '/^[A-Za-z0-9._%-] @[A-Za-z0-9.-] \.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $content)) {
$message .= '<li>Please enter a valid ' . strtolower($title) . '.</li>';
}
}
return $message;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/327194.html
上一篇:表單輸入驗證ReactJS
下一篇:顫振、溢位、SingleChildScrollView。這是一個螢屏內的表單,這次SingleChildScollViewFix對我有用嗎?
