Results 1 to 5 of 5

Thread: novice (sorry) can someone take a look a my contact form code?

  1. #1
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default novice (sorry) can someone take a look a my contact form code?

    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:


    <?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="">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!!!

  2. #2
    Join Date
    Dec 2008
    Location
    Fremont, CA
    Posts
    30
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default

    Its not the complete code, this has references to other file functions.
    however, an hack would be ,

    PHP Code:
    function show_error($myError)
    {
     
    $_SESSION['error'] = $myError
     
    header('Location: contact.php'); // your initial form page
     
    exit();

    $_SESSION['error'] ideally will have the error message returned by show_error
    function. you can print it on the contact screen.

    The codes pretty stiff for a contact form. You can simplify it even further. post you questions back, and somebody would be able to help .

    Rgds,
    Kris

  3. The Following User Says Thank You to l_kris06 For This Useful Post:

    lilibirdy (01-06-2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hmm, yes - so that will reload the form if the email field is incorrectly filled - perfect.

    How do I get a message 'please enter an email address' to show on the form?

  5. #4
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    ooh, and when the form reloads, the text input is lost - how do I keep that?

  6. #5
    Join Date
    Dec 2008
    Location
    Fremont, CA
    Posts
    30
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default

    you need to store those inputs which the user types in a session and
    use that inside the value of the input type, for instance:

    <input type="text" name='user' id="user" <?php if(isset($_SESSION['user']){?> value='<?php echo $_SESSION['user'];?>'<?php }?> />

    Thats the simple part.

    You need to poke around in show_error() function to see wht its capable of retrieving, and use that to dump the returned value into an appropriate session.

    Rgds,
    Kris

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •