...

View Full Version : Help please with PHP contact form



jmar4c
04-26-2012, 01:56 AM
I am editing a "Contact Us" PHP script, I know nothing about PHP so it's just trial and error. Right now after I fill out the form and hit send, it sends to the proper email address but everything included in the form disappears.

I get this error:


notice: Undefined index:cf_name in contact.php on line 2

notice: Undefined index:cf_email in contact.php on line 3

notice: Undefined index:cf_message in contact.php on line 4


Here is the PHP script:


<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'WITHHELD';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index-4.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon@template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>

Here is the relevant form code:

<h2>Contact Form</h2>
<form action="contact.php" method="post" id="contacts-form" class="contact-form">
<fieldset>
<div class="rowElem">
<label>Name:</label>
<input type="text" class="text" name="cf_name">
</div>
<div class="rowElem">
<label>E-mail:</label>
<input type="email" class="text" name="cf_email">
</div>
<div>
<label>Message:</label>
<textarea cols="1" rows="1" name="cf_message">
</textarea>
</div>
<div class="button"><a href="#" onClick="document.getElementById('contacts-form').submit()">send</a><a href="#" onClick="document.getElementById('contacts-form').reset()">clear</a></div>
</fieldset>
</form>

I feel like the fix is really simple, but I am not a coder so I can't find the solution. Hopefully one of you brilliant individuals will be able to. Thanks in advance!

abduraooft
04-26-2012, 08:51 AM
Enclose the above php code inside the following block

if(isset($_POST['cf_name'])){
...
}

jmar4c
04-26-2012, 05:17 PM
I will try this and report back ASAP! Thanks for the reply!

jmar4c
04-28-2012, 10:49 PM
OK I tried it and the error message disappears, but it no longer sends any email. Also it never redirects back to the contact page. What's wrong? The new code looks like this:

<?php
if(isset($_POST['cf_name'])){
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'blah@blah.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your interest in Sigma Lambda Beta. We will contact you shortly.');
window.location = 'index-4.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon@template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
}
?>


Thanks

WolfShade
04-28-2012, 11:15 PM
Just a thought: replace the # in the link href to javascript:void(0);

Just curious as to why use an anchor to submit the form instead of a submit button??

firepages
04-29-2012, 10:12 AM
your original code in your first post works fine for me in firefox/apache2/php5
is there more code involved which we are not seeing there ?

_Aerospace_Eng_
04-29-2012, 12:55 PM
Also I would remove your actual email address from your posts unless you want to get a lot spam.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum