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

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

`

The best solution to check if everything is filled out is via javascript, but I suggest using jQuery
I have uploaded similar code here : http://codepad.org/nqc9vtOK
Should be enough to figure out how to make your one work

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.