Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGremyko Coleman
9,756 PointsEmpty Fields Error Not Display On The Screen (PHP)
My error messages for if a form field is blank, doesn't show on the page instead it submits the form. I am validating it from the server side, because I am trying to prevent blank fields from being submitted into my MySQL database. I also have JavaScript turned off.
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$email = $_POST["email"];
$address1 = $_POST["address1"];
$address2 = $_POST["address2"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip_code = $_POST["zip"];
$phone = $_POST["phone"];
$distance = $_POST["distance"];
//form php validation blank error messages
if($Fname == "" || $Lname == "" || $email == "" || $address1 == "" || $city == "" || $state == "" || $zip_code == "" || $phone == ""){
$msg = "<p class='error'>The required fields cannot be blank</p>";
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !isset($msg)){ //email validation
$msg = "<p class='error'>You must place a real email address</p>";
}else{
require_once("inc/admin_login.php"); //log in database file
$query = "INSERT INTO runner(fname,lname,email,address1,address2,city,state,postcode,phone,distance) VALUES('$Fname','$Lname','$email','$address1','$address2','$city','$state','$zip_code','$phone','$distance')";
$result=mysqli_query($con,$query);
if($result){ //if successfull logining in to database message
$msg = "<h2 class='notice'>Thank You For Signing Up</h2>";
}else{
$error_message = mysqli_error($con);
$result = "There was an error: $error_message";
exit($result);
}// end if
}//end else
header("Location:signup.php?status=thankyou"); //redirects you when comments are entered in correctly
}// end of main 1st if
$pageTitle="Sign Up";
include("inc/header.php");
include("inc/navigation.php");
<div class="signupWrapper">
<h1>Sign up to run</h1>
<?php if(isset($msg)){
echo $msg;
}if(!isset($msg) && isset($_GET['status']) && $_GET['status'] == "thankyou"){ // msg that displays on the redirect page if comments are entered correctly
echo "<p class='notice'>Thank you for submitting your comment</p>";
}else{
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="signUpForm" name="signUpForm" title="Sign To Race Form">
<table class="signUp">
<tr>
<th>
<label for="Fname">First Name:</label>
</th>
<td>
<input type="text" id="Fname" name="Fname"></input>
</td>
</tr>
<tr>
<th>
<label for="Lname">Last Name:</label>
</th>
<td>
<input type="text" id="Lname" name="Lname"></input>
</td>
</tr>
<tr>
<th>
<label for="email">Email:</label>
</th>
<td>
<input type="email" id="email" name="email"></input>
</td>
</tr>
<tr>
<th>
<label for="address1">Address 1:</label>
</th>
<td>
<input type="text" id="address1" name="address1"></input>
</td>
</tr>
<tr>
<th>
<label for="address2">Address 2:</label>
</th>
<td>
<input type="text" id="address2" name="address2"></input>
</td>
</tr>
<tr>
<th>
<label for="city">City:</label>
</th>
<td>
<input type="text" id="city" name="city"></input>
</td>
</tr>
<tr>
<th>
<label for="state">State:</label>
</th>
<td>
<input type="text" id="state" name="state"></input>
</td>
</tr>
<tr>
<th>
<label for="zip">Zip Code:</label>
</th>
<td>
<input type="text" id="zip" name="zip"></input>
</td>
</tr>
<tr>
<th>
<label for="zip">Phone:</label>
</th>
<td>
<input type="text" id="phone" name="phone" title="enter phone number"></input>
</td>
</tr>
<tr>
<th>
<label for="">Distance:</label>
</th>
<td>
<fieldset>
<label for="distance1">1 Mile</label><input type="radio" id="distance1" name="distance" value="1mile" ></input>
<label for="distance2">5K</label><input type="radio" id="distance2" name="distance" value="5K" checked></input>
<label for="distance3">10K</label> <input type="radio" id="distance3" name="distance" value="10K" ></input>
</fieldset>
</td>
</tr>
<tr>
<th>
</th>
<td>
<label for=""></label><input type="submit" id="submitForm" name="submitForm" value="Submit" title="submit button"></input>
<label for=""></label><input type="reset" id="clear" name="clear" value="Clear" title="clear button" ></input>
</td>
</tr>
</table>
</form>
<?php }?> <!-- ends the else statement above in the start of the form, to only show the thank you message/ thank redirect page if
comments are entered in correctly -->
</div>
1 Answer
Antonio De Rose
20,885 PointsAre the above 2 files or 1 file.
Gremyko Coleman
9,756 PointsGremyko Coleman
9,756 PointsIts all 1 file, I dont know why It came out as 2 different files, I found out how to edit the question