我在我的HTML表單中有2組4個輸入字段。我預計最終會有20多套,但在這個例子中只提供了2套。如果輸入字段不完整,則從變量中刪除數據 - PHP
是否有可能如果一個領域的「設置」是不完整的,我可以只從我得到的所有$變量刪除該行。因此,而不是與數據'|行| | |」它只是完全空白。
它只會幫助清理事情。
這會很容易實現嗎?
這是我當前使用的stuff.php腳本。
<html>
<head>
<title>Test PHP</title>
</head>
<body>
<?php
if (isset($_POST['sendform'])) {
$ierrors = array();
$all = '';
// Loop over the values 1 through 2
foreach(range(1, 2) as $i)
{
// Create an array that stores all of the values for the current number
$values = array(
'p' . $i . 'height' => $_POST['p' . $i . 'height'],
'p' . $i . 'width' => $_POST['p' . $i . 'width'],
'p' . $i . 'length' => $_POST['p' . $i . 'length'],
'p' . $i . 'weight' => $_POST['p' . $i . 'weight']
);
// Validate every value
foreach($values as $key => $value)
{
if(empty($value))
{
$ierrors[] = "Value $key is not set";
}
// You can add more validation in here, such as:
if(!is_numeric($value))
{
$ierrors[] = "Value $key contains an invalid value '$value'";
}
}
// Join all of the values together to produce the desired output
$all .= implode('|', $values) . "\n\n";
}
var_dump($all);
}
?>
<form action="stuff.php" method="post">
<div id="npup0" class="hidden">
<div class="parcel-group">
<div class="parcel-title">
<label for="p1weight">Parcel 1</label>
</div>
<div class="minis weight">
<input type="text" id="p1weight" name="p1weight" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1weight']))) { echo htmlspecialchars($_POST['p1weight']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
<div class="minis length">
<input type="text" id="p1length" name="p1length" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1length']))) { echo htmlspecialchars($_POST['p1length']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
<div class="minis width">
<input type="text" id="p1width" name="p1width" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1width']))) { echo htmlspecialchars($_POST['p1width']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
<div class="minis height">
<input type="text" id="p1height" name="p1height" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1height']))) { echo htmlspecialchars($_POST['p1height']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
</div>
</div>
<div id="npup1" class="hidden">
<div class="parcel-group">
<div class="parcel-title">
<label for="p1weight">Parcel 2</label>
</div>
<div class="minis weight">
<input type="text" id="p2weight" name="p2weight" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2weight']))) { echo htmlspecialchars($_POST['p2weight']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
<div class="minis length">
<input type="text" id="p2length" name="p2length" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2length']))) { echo htmlspecialchars($_POST['p2length']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
<div class="minis width">
<input type="text" id="p2width" name="p2width" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2width']))) { echo htmlspecialchars($_POST['p2width']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
<div class="minis height">
<input type="text" id="p2height" name="p2height" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2height']))) { echo htmlspecialchars($_POST['p2height']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
</div>
</div>
</div>
<p><input type="submit" value="click me" id="sendform" name="sendform" /></p>
</form>
</body>
</html>
非常感謝任何指針。一直在努力與此一段時間。
爲什麼使用範圍?相反,你有兩個分開的名字綽綽有餘提交 – 2012-03-10 06:26:13
喜山姆。我使用了範圍,因爲我預計最終會有超過20套這4個輸入字段。 – michaelmcgurk 2012-03-10 06:37:56