所以我在验证我的表格方面遇到了问题。除验证过程外,其他所有内容似乎都有效。当我单击提交时,即使没有提交任何字段,它也会继续执行,因为它有效并将我带到我的链接页面。这是我在php页面上的表单:
<form method="post" action="submitpage.php">
<h3>General Information</h3>
<p>
<label for="albumID">Album ID</label>
<input type="text" name="albumID" id="albumID" value="<?php echo $albumID; ?>">
</p>
<p>
</br>
<label for="artistname">Artist Name</label>
<input type="text" name="artistname" id="artistname" value="<?php echo $artistname; ?>">
</p>
<p>
<label for="albumname">Album Name:</label>
<input type="text" name="albumname" id="albumname" value="<?php echo $albumname; ?>">
</p>
<p>
<label for="price">Price $ </label>
<input type="text" name="price" id="price" value="<?php echo $price; ?>">
</p>
<h3> Media Type</h3>
<p>
<input type="radio" name="mediatype" id="mpeg" value="mpeg"
<?php
// test the value of the form input to see if the radio button should be checked
if ($type == "mpeg") {echo "checked";}
?>
>
<label for="mpeg">MPEG audio file</label>
</br>
<input type="radio" name="mediatype" id="protectedacc" value="protectedacc" <?php
if ($type == "protectedacc") {echo "checked";}
?>>
<label for="protectedacc">Protected AAC audio file</label>
<br>
<input type="radio" name="mediatype" id="protectedmpeg" value="protectedmpeg" <?php
if ($type == "protectedmpeg") {echo "checked";}
?>>
<label for="protectedmpeg">Protected MPEG-4 video file</label>
</br>
<input type="radio" name="mediatype" id="purchasedaac" value="purchasedaac" <?php
if ($type == "purchasedaac") {echo "checked";}
?>>
<label for="purchasedaac">Purchased AAC audio file</label>
<br>
<input type="radio" name="mediatype" id="aac" value="acc" <?php
if ($type == "acc") {echo "checked";}
?>>
<label for="acc">AAC audio file</label>
<br>
</p>
<p>
<h3>Playlists</h3>
<input type="checkbox" name="playlists[]" id="rainydayjams" value="rainydayjams"
<?php
// loop through the array to see if the checkbox value is found and the checkbox should be checked
foreach ($playlists as $playlist) {
if ($playlist == "rainydayjams") {echo "checked";}
}
?>
>
<label for="rainydayjams">Rainy Day Jams</label>
</br>
<input type="checkbox" name="playlists[]" id="workout" value="workout"
<?php
// loop through the array to see if the checkbox value is found and the checkbox should be checked
foreach ($playlists as $playlist) {
if ($playlist == "workout") {echo "checked";}
}
?>
>
<label for="workout">Workout</label>
</br>
<input type="checkbox" name="playlists[]" id="feelgoodvibes" value="feelgoodvibes"
<?php
foreach ($playlists as $playlist) {
if ($playlist == "feelgoodvibes") {echo "checked";}
}
?>
>
<label for="feelgoodvibes">Feel Good Vibes</label>
</br>
<input type="checkbox" name="playlists[]" id="rocking" value="rocking"
<?php
foreach ($playlists as $playlist) {
if ($playlist == "rocking") {echo "checked";}
}
?>
>
<label for="rocking">Rocking Out</label>
</br>
<input type="checkbox" name="playlists[]" id="clubdance" value="clubdance"
<?php
foreach ($playlists as $playlist) {
if ($playlist == "clubdance") {echo "checked";}
}
?>
>
<label for="clubdance">Club Dance Music</label>
</p>
<h3>Genre</h3>
<p><label for="genre">Genre: </label>
<select name="genre" id="genre">
<option value="">Please select a genre</option>
<option value="rock"
<?php
// check to see if the county is selected
if ($genre=="rock") { echo "selected";}
?>
>Rock</option>
<option value="jazz"
<?php
if ($genre=="jazz") { echo "selected";}
?>
>Jazz</option>
<option value="metal"
<?php
if ($genre=="metal") { echo "selected";}
?>
>Metal</option>
<option value="altpunk"
<?php
if ($genre=="altpunk") { echo "selected";}
?>
>Alternative and Punk</option>
<option value="newjazz"
<?php
if ($genre=="newjazz") { echo "selected";}
?>
>New Jazz</option>
<option value="blues"
<?php
if ($genre=="blues") { echo "selected";}
?>
>Blues</option>
<option value="latin"
<?php
if ($genre=="latin") { echo "selected";}
?>
>Latin</option>
<option value="reggae"
<?php
if ($genre=="reggae") { echo "selected";}
?>
>Reggae</option>
<option value="pop"
<?php
if ($genre=="pop") { echo "selected";}
?>
>POP</option>
<option value="soundtrack"
<?php
if ($genre=="soundtrack") { echo "selected";}
?>
>Soundtrack</option>
</select>
</p>
<p>
<label for="track">Track Number</label>
<input type="text" name="track" id="track" value="<?php echo $track; ?>">
</p>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
接下来是我的验证脚本,它应检查是否输入了所有字段,如果字段不为空,则会发送到另一个页面以显示信息。
<?php
require_once 'functions.php';
writeHead("Form Test");
if (isset($_POST['submit'])) {
// set the validation flag
$valid = true;
$albumID = htmlspecialchars(trim($_POST['albumID']));
if (empty($albumID)) {
echo "<p class='error'>Please enter Album ID</p>";
$valid = false;
}
$artistname = htmlspecialchars(trim($_POST['artistname']));
if (empty($artistname)) {
echo "<p class='error'>Please enter your last name</p>";
$valid = false;
}
$albumID = ucfirst(strtolower($albumID));
$artistname = ucfirst(strtolower($artistname));
$albumID = htmlspecialchars(trim($_POST['albumname']));
if (empty($albumname)) {
echo "<p class='error'>Please enter an Album Name</p>";
$valid = false;
}
$price = htmlspecialchars(trim($_POST['price']));
if (empty($price)) {
echo "<p class='error'>Please enter a price</p>";
$valid = false;
}
$track = htmlspecialchars(trim($_POST['track']));
if (!is_numeric($track)) {
echo "<p class='error'>Track code must be numeric</p>";
$valid = false;
}
if (!preg_match('/\d{5}(-\d{4})?/',$track)) {
echo "<p class='error'>Invalid track.</p>";
}
if (isset($_POST['mediatype'])) {
$type = $_POST['mediatype'];
} else {
echo "<p class='error'>Please select a media type</p>";
$valid = false;
$type="";
}
if (isset($_POST['playlists'])) {
$playlists = $_POST['playlists'];
} else {
echo "<p class='error'>Please select at least one playlist</p>";
$valid = false;
$playlists[0]="";
}
$genre = $_POST['genre'];
if ($genre=="") {
echo "<p class='error'>Please select a genre</p>";
$valid = false;
}
$valid = false;
}
}
if ($valid=true) {
header("Location:submitpage.php?albumID=$albumID&artistname=$artistname");
exit();
}
} else {
$albumID="";
$artistname="";
$albumname="";
$price="";
$type ="";
$playlists[0]="";
$genre="";
$track="";
}
&GT;
当我提交它时,它应该将我发送到另一个php页面,在那里我可以显示信息并且该部分正在工作。但是如果字段留空,它也可以工作。我的验证无效。任何想法我都可以解决这个问题。
答案 0 :(得分:0)
你最后有一个错字。变化
if($valid = true)
到
if($valid) // or if($valid==true)
目前它将$ valid设置为true,然后在if语句中执行代码。
答案 1 :(得分:0)
您可以使用空来检查空白字段名
empty($_POST["playlists"])
更改您的代码。请查看 $ valid == true
if ($valid==true) {
header("Location:submitpage.php?albumID=$albumID&artistname=$artistname");
exit();
}