Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 21st, 2004, 02:16 AM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Form Validation

Hi,

I'd like to know how to validate a form. When the form is submitted and there are errors in the form fields, i'd like to see the same form again, but with the wrong field turned red and empty, and the proper filled field should contain their data. this should all happen on the same page. I've done this before using ASP but PHP still has a lot of for me.

Thanks

HammR
 
Old June 21st, 2004, 02:34 AM
Authorized User
 
Join Date: Sep 2003
Location: Cambridge, , United Kingdom.
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Probably the easiest way to do this is with javascript. Below is some javascript that uses functions to validate separate things. Just assign each input an ID AND a name.
At or near the top of the page use
Code:
<?php
if (isset($var))
{
  //execute PHP script required (Update, insert etc)
}

else
{
?>
<script language="javascript">
//Copied from martin.f2o.org/javascript/form-validation
//Validates a number
function validateNumber(field, msg, min, max) {
    if (!min) { min = 0 }
    if (!max) { max = 255 }

    if ( (parseInt(field.value) != field.value) ||
             field.value.length < min ||
             field.value.length > max) {
        alert(msg);
        field.focus();
        field.select();
        return false;
    }

    return true;
}

//Validates a string
function validateString(field, msg, min, max) {
    if (!min) { min = 1 }
    if (!max) { max = 65535 }

    if (!field.value || field.value.length < min || field.value.max > max) {
        alert(msg);
        field.focus();
        field.select();
        return false;
    }

    return true;
}

//Validates an email address
function validateEmail(email, msg, optional) {
    if (!email.value && optional) {
        return true;
    }

    var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
    if (!re_mail.test(email.value)) {
        alert(msg);
        email.focus();
        email.select();
        return false;
    }

    return true;
}
</script>
<?php
} //close off else isset("some variable")
?>
---
David Thorne, Student
UK
 
Old June 21st, 2004, 06:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or, if you really want to validate on the server, here's one approach you could take.

1. post the page to itself
2. On reloading the page, (i.e. the page ahs been posted once - usually done by seeing if the submit button's field isset) obtain the posted values from the $_POST[] array
3. check the values using your validation rules
4. If valid, push the values from the $_POST array into the $_SESSION array and redirect using a header("location ; page.php") directive
5. Otherwise, reprint the form. Each field in the form will have a value attribute along the lines of:
value="<?php echo $_POST['this_field_value']; />"

HTH
Take it easy
Dan
 
Old July 21st, 2004, 07:17 AM
Authorized User
 
Join Date: Jul 2004
Location: glasgow, , United Kingdom.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dan

i have a form, and am getting variables ok.

now i want to add checking if the fields are valid.

what syntax do i need to check its minimum length ?

is it just "if ! ($myvar)" ?

then i want to put up a message - and after 5 seconds put them BACK to where they were WITHOUT blanking the form.

I can do the message ok, but what about hitting their back button.
the form is an html form sitting on the users local pc and sent by email to them.

regards







Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Form Validation Stanny Access VBA 5 December 20th, 2005 08:13 PM
Validation of Form qazi_nomi Javascript 5 August 2nd, 2004 03:49 PM
Form not refreshing after form validation Mimi Javascript How-To 0 August 25th, 2003 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.