Hi all
Having worked on various tutorials I am trying to build a mock-dating site to cover learning aspects of PHP including: basics, database manipulation, string handling, forms, validation and sessions.
I have gotten quite far in that I have written a basic script where I can enter various details about a user, eg. forename, surname etc., and have them submitted to a database.
Now I am at my second stage where I have written some validation in PHP (for the form), and want the form to be submitted in the first instance. If there are any errors, to then display the form again for the user to correct their errors and re-submit.
What I have is as follows:
<?php
if(!isset($_POST['_SubmitForm'])) {
// FORM NOT SUBMITTED SO DISPLAY FORM
// Drop-down box queries
// START FORM
<FORM TYPE="SUBMIT" NAME="_SubmitForm_" ACTION="<?php echo($_SERVER['PHP_SELF']); ?>" METHOD="POST">
... etc
</FORM>
// END FORM
else {
// FORM SUBMITTED
// Get user inputs from $_POST
# FORM has been sent so need to look at values
$Forename = (string) $_POST['Forename'];
$Surname = (string) $_POST['Surname'];
... etc
// Create ErrorArray Variable
$ErrorArray = array();
// Check each input form user
// START FORM VALIDATION
if (!ereg ("[a-zA-Z]{1,}", $_POST["Forename"])) {
$ErrorArray[] = "Please enter a Forename consisting of alphabetical characters.";
}
// Validate Surname
if (!ereg ("[a-zA-Z]{1,}", $_POST["Surname"])) {
$ErrorArray[] = "Please enter a Surname consisting of alphabetical characters.";
}
... etc
// END FORM VALIDATION
// Test to see if ErrorArray has any values
if(empty($ErrorArray)) {
# no errors so log user in....
# this is where sessions come in!!!
// Enter record into database
}
else {
// Found one or more errors
$nerrors = count($ErrorArray);
foreach($nerrors as $error) {
echo('You have this error:'.$error.' Please correct it.');
}
// Now same FORM here again
?>
<FORM TYPE="SUBMIT" NAME="_SubmitForm_" ACTION="<?php echo($_SERVER['PHP_SELF']); ?>" METHOD="POST">
... etc
</FORM>
<! ------------------- END FORM ------------------- >
<?php
}
}
?>
This actually works in that the form displays and I can press 'Submit'. However, nothing else happens. i.e. if I submit a blank form, no messages are displayed in regards to validation.
I think I am on the right track more or less but just need a helping hand.
Thanks.
Mak 🙂