Log in

View Full Version : novice (sorry) can someone take a look a my contact form code?



lilibirdy
01-06-2009, 07:37 PM
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!!!

l_kris06
01-06-2009, 07:54 PM
Its not the complete code, this has references to other file functions.
however, an hack would be ,



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

lilibirdy
01-06-2009, 08:06 PM
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?

lilibirdy
01-06-2009, 08:07 PM
ooh, and when the form reloads, the text input is lost - how do I keep that?

l_kris06
01-06-2009, 08:25 PM
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