Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Contact Form Not Working, Help please

the mail() function

Moderators: macek, egami, gesf

Contact Form Not Working, Help please

Postby soc86 » Thu Sep 08, 2011 1:33 pm

Hi, I am a bit of a novice in PHP, but i am having blank emails sent to my email address, is anyone able to tell me where i am going wrong? My codes below:

PHP

<?php
$field_name = $_POST['full_name'];
$field_email = $_POST['email'];
$field_telephone = $_POST['telephone'];
$field_message = $_POST['message'];

$mail_to = 'shaun_o_c@yahoo.co.uk';
$subject = 'Contact form'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$cf_email."\r\n";
$headers .= 'Reply-To: '.$cf_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank You.');
window.location = 'index-4.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Fail ba');
window.location = 'index-4.html';
</script>
<?php
}
?>



HTML

<h2>Contact Form</h2>
<form id="ContactForm" action="contact.php" enctype="multipart/form-data">
<div>
<div class="rows"><input name="full_name" class="input" type="text" value="Name:" onblur="if(this.value=='') this.value='Enter your Name:'" onFocus="if(this.value =='Enter your Name:' ) this.value=''" /></div>
<div class="rows"><input class="email" type="text" value="E-mail :" onblur="if(this.value=='') this.value='Enter your E-mail:'" onFocus="if(this.value =='Enter your E-mail:' ) this.value=''" /></div>
<div class="rows"><input class="relephone" type="text" value="Telephone (optional):" onblur="if(this.value=='') this.value='Enter your Phone:'" onFocus="if(this.value =='Enter your Phone:' ) this.value=''" /></div>
<div class="textarea_box"><textarea name="message" cols="1" rows="1" onBlur="if(this.value=='') this.value='Enter your Message:'" onFocus="if(this.value =='Enter your Message:' ) this.value=''" >Skriv inn din melding:</textarea></div>
<a href="#" class="link1" onClick="document.getElementById('ContactForm').reset()">Reset</a>
<a href="#" class="link1" onClick="document.getElementById('ContactForm').submit()">Send</a>
</div>

</form>
soc86
New php-forum User
New php-forum User
 
Posts: 9
Joined: Thu Sep 08, 2011 1:29 pm

Re: Contact Form Not Working, Help please

Postby Nullsig » Fri Sep 09, 2011 4:33 am

You need to check if the user has actually submitted the form. Right now it looks like you are submitting the form to itself, but not checking what cause the page load. So every time someone goes to your page it causes an e-mail to be sent.

Code: Select all

<?php
if(isset($_POST)){//This line will check to see if the user actually sent
                        //variables to this page before sending an email
$field_name = $_POST['full_name'];
$field_email = $_POST['email'];
$field_telephone = $_POST['telephone'];
$field_message = $_POST['message'];

$mail_to = 'shaun_o_c@yahoo.co.uk';
$subject = 'Contact form'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$cf_email."\r\n";
$headers .= 'Reply-To: '.$cf_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank You.');
window.location = 'index-4.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Fail ba');
window.location = 'index-4.html';
</script>
<?php
}
}
?>
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Contact Form Not Working, Help please

Postby soc86 » Fri Sep 09, 2011 1:36 pm

I am getting the email when the send button is clicked, i dont have a problem with visitors automatically sending the email when the page loads.

My problem is that i am not getting thier information they typed, so my email is blank like below:

From:
E-mail:
Message:
soc86
New php-forum User
New php-forum User
 
Posts: 9
Joined: Thu Sep 08, 2011 1:29 pm

Re: Contact Form Not Working, Help please

Postby Nullsig » Thu Sep 15, 2011 6:29 am

*slaps himself in the face*

You don't have a declared method in your form tag it should say:

<form action="afdsafds.php" method="POST" ...
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.