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:



                <h1>Please enter your information</h1>

                        <form method= "post" action = "output_hw2.php">
                        <table width="500">
                        <td><strong>First Name:</strong> </td>
                        <td><input type= "text" name = "fname" /></td>
                        <td><strong>Last Name:</strong></td>
                        <td><input type="text" name = "lname" /></td>

                        <td> <input type="radio" name = "genderRadio" value="Female" />Female</td>
                        <td> <input type="radio" name = "genderRadio" value="Male"/>Male</td>

                        <td><strong>Address:</strong> </td>
                        <td><input type="text" name = "address" size="50"/></td>

                        <td><strong>City:</strong> </td>
                        <td><input type="text" name = "city" size="30"/></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>

                        <td><strong>Zip Code:</td>
                        <td></strong> <input type="text" name="zip" size= "5" /></td>

                        <textarea name="comments" rows="5" cols="40" ></textarea> <br/><br/>

                        <input type="submit" name="submit" value ="submit" /> <br/>




here is the php file:


$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)";



  1. How can I get my $genderRadio to display
  2. How can I make sure that everything is filled`


Edited by Transcendent

5 Years
Discussion Span
Last Post by almostbob

For each value that you want to check:

$errors = 0;
$error_msg = '';
if (!isset($_POST['fname']) || empty ($_POST['fname']))) { $errors++: $error_msg = 'Errors Text'; }
// And so on for each posted field you want to check and then echo out the errors in the page
if ($errors ==0) {
 // Run your insert / update querry

the google search would be self processing php form
a php page thqat submits to itself, the php and the html contained in one file
add simplypixie's validation to the file and change each input to something like

<input type="text" name="zip" size= "5" value="<?php if(isset($_post['zip'])) echo $_post['zip'];?>"/>

& clean up the code

<td><strong>Zip Code:</td><td></strong> <input type="text" name="zip" size= "5" /></td>
is invalid, and may cause compliant browsers to fail to display
improperly nested td strong

let alone opinion on tables and inline styles,,, the 1990s ended years ago, css style and positioning produces a much smaller(faster) page that displays more reliably

Edited by almostbob

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.