Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
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



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.