Jump to content

Can someone take a look at my contact form? - newbie (big apologies!)


lilibirdy

Recommended Posts

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

 

 

<form action="contact.php" method="post">

<p><b>Your Name:</b> <input type="text" name="yourname" /><br />

<b>E-mail:</b> <input type="text" name="email" /><br />

Contact Number: <input type="text" name="phone"></p>

 

<p>How would you prefer us to contact you?

<input type="radio" name="howcontact" value="email" checked="checked" /> Email

<input type="radio" name="howcontact" value="phone" /> phone

<input type="radio" name="howcontact" value="either" /> either</p>

 

<p><b>Your Enquiry:</b><br />

<textarea name="enquiry" rows="10" cols="40"></textarea></p>

 

<p><input type="submit" value="Send it!"></p>

<input name="reset" type="reset" value="Reset Form">

 

<input type="text" name='keepinginfo' id="keepinginfo"

 

<?php if(isset($_SESSION['keepinginfo']){?> value='<?php echo $_SESSION['keepinginfo'];?>'<?php }?> />

 

</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.