PHP Form Validation... HELP!

He has: 1,758 posts

Joined: Jul 2002

Hiya,

I've got a fairly basic understanding of PHP but i'm learning all the time.

I have a really basic form, that i'd like to submit by email using php, i know how to send the email, but i want to ensure that things are filled in before i send the form.

I know i need to submit the form to itself so i can put the bits that ARE filled in back in and highlight the bits that havent been.

What i'm wondering is how can i make the script only send the results by email IF all the fields are filled in? and just display the form again if they're something missing? (i know how to do the validation).

I might be having a slow day or something, cos i'm sure this should be fairly simple.

Thanks.

Andy

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

<?php
if($_POST['email'] == \"\" || $_POST['name'] == \"\")
{
echo \"<p>Please fill in all required fields</p>\";
}
else
{
echo \"<p>Message has been sent</p>\";
}
?>

I just created this on the fly, obviously if you have alot of things to validate then the if function is going to get very long. Maybe someone can think of someway to loop through all your $_POST variables Smiling

...I'm assuming you know of the "||"...

He has: 1,758 posts

Joined: Jul 2002

Hi,

Thanks... i sorted it in the end... i was just having trouble working out the logic... my codes pretty long, but it does the job, so i'm happy!

http://www.djrich.co.uk/contact.php

Thanks anyway!

Andy

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

you may want to search this forum -- I think Mark has answered this before, and provided a nice script as well.

He has: 1,758 posts

Joined: Jul 2002

lol thanks... i'll remember that in future. I feel better for sussing it out myself anyway... much more rewarding than nicking someone elses code.

Its amazing what a cup of coffee and 5 minutes away from the screen can do for your mind! Laughing out loud

Andy

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.