Log in

View Full Version : php contact form error



samar470
07-21-2011, 07:04 AM
hello

I'm a new beginner in php programing and need essential help

i have a contact form which have to send data to specific email and redirect to than you page but when submitting it displays the php code in IE page and never redirect to thank you page , please help me and correct for me

below is the code:

<html>
<head>
<title>Contact us</title>
<!-- define some style elements-->
<style>
h1
{
font-family : Arial, Helvetica, sans-serif;
font-size : 16px;
font-weight : bold;
}
label,a
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}

</style>
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>
</head>

<body>
<h1>Contact us</h1>
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
______________________

<script language="JavaScript">
// Code for validating the form
var frmvalidator = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email","Please enter a valid email address");
</script>
_______________________________________

bluewalrus
07-21-2011, 01:57 PM
Your host supports PHP, correct? If so please provide the code of 'contact-form-handler.php'

griffinwebnet
07-21-2011, 06:32 PM
Could you please give us the code to contact-form-handler.php? It doesnt look like anything is wrong with your code there on a quick glance, so your error may be in contact-form-handler.php

samar470
07-21-2011, 07:17 PM
hi this is mycontact- form-handler.php code

<?php
$errors = '';
$myemail = 'yourname@website.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";

$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

__________________

samar470
07-21-2011, 07:18 PM
this is my HTML Form code:

<html>
<head>
<title>Contact us</title>
<!-- define some style elements-->
<style>
h1
{
font-family : Arial, Helvetica, sans-serif;
font-size : 16px;
font-weight : bold;
}
label,a
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}

</style>
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>
</head>

<body>
<h1>Contact us</h1>
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>

<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email","Please enter a valid email address");
</script>
<!--


</body>
</html>

_____________________________

bluewalrus
07-21-2011, 10:31 PM
Is there an error message? Do you have other PHP pages that function?

samar470
07-22-2011, 12:35 AM
noo threre is more any error messages

bluewalrus
07-22-2011, 01:22 PM
Do you have other PHP pages that function? If it's outputting all the PHP code it is a server error, or PHP is not set up.

samar470
07-22-2011, 08:04 PM
i reinstalled php on my computer again and put all contact-form-handler.php and contact-form and thank you page in (www folder) and
//redirect to the 'thank you' page

edit redirect page to
header('Location: c:/AppServ/www/contact-form-thank-you.html');

and when my form on localhost it never geos to thank you page but gives me this messages:


Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\AppServ\www\contact-form-handler.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\contact-form-handler.php:32) in C:\AppServ\www\contact-form-handler.php on line 34

bluewalrus
07-22-2011, 08:07 PM
So SMTP isn't set up on your local computer, or isn't configured with your PHP that is the first error. The second error is because of the first error, nothing can be outputted before the header is called and that error message is being outputted. You could reverse the order of that and you won't see the error message but once you bring it to a server with SMTP set it still won't send because the header will redirect before it gets there.

samar470
07-23-2011, 06:18 AM
i don,t understand exactly what to do please explain to me i'm a beginner so tell me exactly what to do

do i have to change thelocalhost server

and what to do about second error

bluewalrus
07-23-2011, 02:14 PM
Comment out the "mail(..." line and the second error should go away and it should redirect you. You'll have to configure the SMTP on your local server for it to be able to send emails, or if it is already set alter the settings PHP has in the configuration file (php.ini).

samar470
07-24-2011, 07:06 AM
heloo

i had to make a fake example to be more comfort when working with you so i signed up with free server
i edit my php.ini file i changed server name to :server19.000webhost.com

this is my IP website: 31.170.160.83

i made a fake email so we can work on:isa-nov@hotmail.com

but when i went to:server19.000webhost.com/contact-form

i got nothing do i have to change the server root on my pc from c:/AppServ/www
and to what ?????

please help me i need this form in my work :(

samar470
07-24-2011, 07:15 AM
actually i still see localhost/contact-form in my IE

although i cahnged the server and edit php.ini and httpd.conf
:mad:

pleeeeeeeeeaaaaaaaaaaaasssssssssseeeeeeeeee hhhhhhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllpppppppppp me:eek: