I have a code and I want to validate it before the submitting it. When I say before submitting it I mean that the form does not submit the information filled till everything is filled correctly. I was thinking of using the if statement but I believe if I do the information will still be sent to my database even the information might be incorrect. I also know that I probably have to use the empty() function.
Anyways, does anyone know how I can accomplish this? If they click on the submit button, I want to make sure everything is filled before sending the information to my database.
here is the html file:
`</head>
<body>
<h1>Please enter your information</h1>
<form method= "post" action = "output_hw2.php">
<table width="500">
<tr>
<td><strong>First Name:</strong> </td>
<td><input type= "text" name = "fname" /></td>
<tr/>
<tr>
<td><strong>Last Name:</strong></td>
<td><input type="text" name = "lname" /></td>
</tr>
<tr>
<td><strong>Gender:</strong></td>
<td></td>
</tr>
<tr>
<td> <input type="radio" name = "genderRadio" value="Female" />Female</td>
</tr>
<td> <input type="radio" name = "genderRadio" value="Male"/>Male</td>
</tr>
<tr>
<td><strong>Address:</strong> </td>
<td><input type="text" name = "address" size="50"/></td>
</tr>
<tr>
<td><strong>City:</strong> </td>
<td><input type="text" name = "city" size="30"/></td>
</tr>
<tr>
<td><strong>State:</strong></td>
<td><select name="state">
<option value=" ">Pick a state</option>
<option value="Ar">Arizona</option>
<option value="GA">Georgia</option>
<option value="TN">Tennessee</option></td>
</select>
</tr>
<tr>
<td><strong>Zip Code:</td>
<td></strong> <input type="text" name="zip" size= "5" /></td>
</tr>
</table>
<p><strong>Comments:</strong></p>
<textarea name="comments" rows="5" cols="40" ></textarea> <br/><br/>
<input type="submit" name="submit" value ="submit" /> <br/>
</form>
</body>
</html>
here is the php file:
`<?php
$firstName = $_POST['fname'];
$lastName = $_POST['lname'];
$genderRadio = $_POST['genderRadio'];
$address = $_POST['address'];
$state = $_POST['state'];
$city = $_POST['city'];
$zip = $_POST['zip'];
echo "Thank You $firstName $lastName", "<br/>";
echo "<strong>This is what you entered </strong>: ". "<br/>";
echo "Address: <b>$address </b>". "<br/>";
echo "State: <b>$state</b> ". "City: <b>$city </b>". "Zip: <b>$zip</b> ". "<br/>";
echo "You are: $genderRadio ";
print "We are very please that you decided to submit your information. If you find anything
wrong please go back and re-enter the correct information and please post a comment
saying that you're correcting your information in addition to the comment you already
add(if you did alread or want to add one)";
?>`
Also
- How can I get my $genderRadio to display
- How can I make sure that everything is filled`
`