Beginner (sorry) Can someone check my contact form please?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lilibirdy
Forum Newbie
Posts: 1
Joined: Tue Jan 06, 2009 12:55 pm

Beginner (sorry) Can someone check my contact form please?

Post by lilibirdy »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hey. I'm a true novice. Web designer by day, i'm doing a website for myself now (finally) which means i've turned developer overnight and i'm way out of my comfort zone.

It's a very simple contact form - here's the code:

Code: Select all

<?php
$myemail    = "";
$subject    = "Website Enquiry";
 
 
$yourname   = check_input($_POST['yourname']);
$email      = check_input($_POST['email'],"Enter your Email address");
$phone      = check_input($_POST['phone']);
$howcontact = check_input($_POST['howcontact']);
$enquiry    = check_input($_POST['enquiry']);
 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("Email address not valid");
}
 
$message = "New Enquiry:
 
Name: $yourname
Email Address: $email
Phone Number: $phone
Contact By: $howcontact
 
Enquiry:
$enquiry
";
 
mail($myemail, $subject, $message);
 
header('Location: thanks.html');
exit();
 
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
 
function show_error($myError)
{
?>
 
 
    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>
    
    <a href="/fripperies_contact.html">back to contact form</a>
 
    </body>
    </html>
<?php
exit();
}
?>

This works, but it doesn't work exactly how i'd like it to since it's essentially a cobbled together first attempt. Currently if there's an error you're directed to a new page. Instead, I'd like the error to be highlighted in the form page, with the other fields retaining the text already input. I searched until the wee small hours of the morning for some answers, and found nothing I could get my head around. If someone doesn't mind helping me out i'd be so grateful! Thankyou!!!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
nvartolomei
Forum Newbie
Posts: 11
Joined: Tue Jan 06, 2009 3:46 pm

Re: Beginner (sorry) Can someone check my contact form please?

Post by nvartolomei »

Try to use javascript for validating.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Beginner (sorry) Can someone check my contact form please?

Post by pickle »

It seems that this is not the whole page code - I imagine your HTML form is also on this page?

There are a few things I'd fix here - all novice problems - certainly nothing to be ashamed of. One of the biggest problems early users of PHP have is spaghetti code: the mixture of business logic (code that works on data) and display logic (code that displays something to the user). There's a little bit of that here. To me at least, it looks like you're jumping around a bit. Here's how I'd re-arrange the file:
  1. Declare your variables to be used to send the email and to insert into the form. 1 variable per datum is sufficient. Also declare an $error string for storing your error message. All these variables will be used to transfer information from the business logic, to the display logic.
  2. Check if the form has been submitted. If Yes:
    • Yes: clean up any slashes that have been added. get_magic_quotes_gpc() will need to be used - a standard call to stripslashes() could remove legitimate slashes. Store the cleaned up strings in your variables you declared above.
    • Call checkInput (convention says use 'camel case' rather than your check_input format). Have it return boolean TRUE if the input was fine, FALSE if not. checkInput() should be business logic & not concern itself with communicating to the user.
    • Where there any problems with the input? If yes, generate your error message, but don't display it.
    • If no, send your email, forward the user, and exit() [It is not necessary to exit() all the time. Some browsers don't behave 100% nicely with headers though, so it's always a good idea to do what you've done & put an exit() statement after any forwarding headers].
  3. If the form hasn't been submitted, no special action needs to be done
  4. Now, into the display logic. Your page can be split up like this:

    Code: Select all

    <?php
    //Business logic here
    ?>
    <html>
    The HTML can go here
    </html>
    In the display logic area, for each of your fields, do this:

    Code: Select all

    <input type = "text" name = "blah" value = "<?php echo $blah_variable_that_you've_stored ?>" />
    You can also output the $error message (which will be empty if no error was encountered)
Phew - well I got into that more than I was planning, but hopefully it helps you out a bit without being overwhelming.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply