Jump to content

Recommended Posts

I have at quite large form at one page, and there fore the validation is placed in the page that handles the form.

 

Now.. if the validation sendt an error I have made a new page with the errormessage where from you can choose to correct the form data or cancel.

 

If the user want to correct the form data I sent him back with '<a href="javascript:javascript:history.go(-1)">' .

 

Is this a bad way? and are there a smarter way?

The script which creates the form should also include the validation logic and the form should post to itself. Then if validation fails you can repopulate the form with the POSTed values. If validation passes you can then include the script wich processes the data. Of course you can separate any of the above logic into individual files. If you have a large form then you will probably want to have separate files for the form and validation as well.

 

Here is an example of a possible file structure:

contact.php (This page determines if the form data has been posted. If not, the form script is included. If post data has been submitetd (and fails) then the form script is included (and previous values populated). If validation passes, then the processing page is included)

validate.php this would contain the validation logic

form.php this would contain just the form with variables to populate previous data if available

process.php This would be called when validation passes an would process the actual data

thankyou.php This is the page to display after processing has been complete. If you call the page by using a header() to redirect, then the POST data will be cleared. This prevents data from beng submitted twice if the user hits the refresh button

Here are some example scripts to give you an idea:

 

contact.php (main script called by user)

<?php
if(isset($_POST['name']))
{
    //Form was submitted, pre-process data
    $name    = trim($_POST['name']);
    $email   = trim($_POST['email']);
    $phone   = trim($_POST['phone']);
    $message = trim($_POST['message']);
    
    //include validation script
    include('validate.php');

    if($valid)
    {
        //Submission was valid include processing script
        include('process.php');
        exit();
    }
    
    //Validation failed, form will be displayed
}
    
//No post data or validation failed, include form
include('form.php');
?>

 

form.php

<html>
<body>
<div style="color:#ff0000;"><?php echo $error_message; ?></div>
Contact us:<br />
<form name="contact" action="" method="POST">
    <b><label for="message">Name: </label></b>
    <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br />

    <b><label for="message">Email: </label></b>
    <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br />

    <label for="message">Phone: </label>
    <input type="text" name="phone" id="name" value="<?php echo $phone; ?>" /><br />

    <b><label for="message">Message</label></b>
    <textarea name="message"><?php echo $message; ?></textarea>
<br /><br />
<button type="submit">Submit</button>
</form>

</body>
</html>

 

validate.php

<?php
    
$errors = array();
    
//Validate the data
if(empty($name))
{
    $errors[] = " - Name is required.";
}
if(empty($email))
{
    $errors[] = " - Email is required.";
}
if(empty($message))
{
    $errors[] = " - Message is required.";
}

if(count($errors)==0)
{
    $valid = true;
}
else
{
    $error_message = "<b>The following errors occured:</b><br>\n";
    $error_message .= implode("<br />\n", $errors);
    $valid = false;
}
    
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.