So I having issues with validating a form I have. Everything else seems to work except for the validation process. When I click submit even with none of the fields submitted it proceeds to act as it is valid and takes me to my linked page. This is my form on my php page:
<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>
Next is my validation script that should check that all fields are entered and if the fields are not empty it sends to another page to display the information.
<?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="";
}
?>
When I submit it, it should send me to another php page where I can display the information and that part is working. But it also works if the fields are left empty. My validation is not working. Any ideas how I can solve this please.
You have a typo at the end. Change
if($valid = true)
to
if($valid) // or if($valid==true)
At the moment it is setting $valid to true and then executing the code in the if statement.
You can use empty to check blank fieldname
empty($_POST["playlists"])
change your code. Check with $valid==true
if ($valid==true) {
header("Location:submitpage.php?albumID=$albumID&artistname=$artistname");
exit();
}