Jump to content

Contact form driving me crazy


robolist

Recommended Posts

Hey guys,

 

I really hope someone can help me out here. I have been working on my new website all week and am now almost finished. Just need to complete the contact page and touch up a few things...

 

I really dont know php at all to be honest and just found a code somewhere on the net to help me.

 

Heres my problem

 

I have finally got the form to work but i am not recieving the correct data. I only get the email add, subject and messge. I am not getting the name of the sender.. also i really want to add a website field to the code because i do have that on the contact form...  please can someone tell me where i am going wrong? the following is the php code i am using...

 

<?php

// Contact subject

$subject ="Website enquiry";

// Details

$message=$_POST[detail];

 

// Mail of sender

$mail_from=$_POST[customer_mail];

// From

$header="from: $name <$mail_from>";

 

// Enter your email address

$to ='robin@rdosolutions.com';

 

$send_contact=mail($to,$subject,$message,$header);

 

// Check, if message sent to your email

// display message "We've recived your information"

if($send_contact){

echo "We've recived your contact information";

}

else {

echo "ERROR";

}

?>

 

please help me.. i am hoping to put the site live tomorrow...

 

many thanks in advance..

rob

Link to comment
Share on other sites

Here's what I use on my website:

 

<form id="contact-form" action="send-mail.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" id="name" />

<label for="email">Email</label>
<input type="text" name="email" id="email" />

<label for="message">Message</label>
<textarea name="message" id="message" rows="5" cols="35"></textarea>

<input type="submit" class="submit" value="Send" />
</form>

 

<?php
function validate_email($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

if (isset($_POST['name'])) {
// The form was submitted
$n   = trim(stripslashes(strip_tags($_POST['name'])));
$e   = trim(stripslashes(strip_tags($_POST['email'])));
$m   = trim(stripslashes(strip_tags($_POST['message'])));
$eol = "\r\n";

if (validate_email($e) && !empty($n) && !empty($m)) {
	// Form was filled in correctly
	$s    = "Enquiry from $n"; // Subject
	$t    = "YOUR EMAIL ADDRESS"; // Your email address

	$headers.= "To: YOUR NAME <$t>".$eol;
	$headers.= "From: $n <$e>".$eol;

	if(mail($t, $s, $m, $headers)) {
		// Everything went fine
		echo '<p class="response-good">You email has been sent, thanks.</p>';
	} else {
		// Mail function failed
		echo '<p class="response-bad">There was a problem sending the email, please try again later.</p>';
	}
} else {
	// Email didn't validate or name or message fields were empty after processing
	echo '<p class="response-bad">Please fill out all fields & ensure that your email is valid.</p>';
}
} else {
// Form wasn't filled out
echo '<p class="response-bad">Please fill out the form.</p>';
}
?>

 

You can easily modify it to your needs.

Link to comment
Share on other sites

Thanks guys.. yeah i just want a very simple php script.. no need for validation or anything..

 

here is my form

 

<form id="myform" action="send_contact.php" method="post" enctype="multipart/form-data">

<div>

<div class="field">

<label for="name" >Name</label>

<input class="inputfield textfield" name="name" type="text" />

</div>

<div class="field">

<label for="customer_mail" >E-mail</label>

<input class="inputfield textfield" name="customer_mail" type="text" />

</div>

<div class="field">

<label for="website" >Website</label>

<input class="inputfield textfield" name="website" type="text" />

</div>

<div class="field area">

<label for="detail" >Details</label>

<textarea class="inputfield textarea1" name="detail" ></textarea>

</div>

</div>

<!--div class="clear"></div-->

<input class="submitbutton" type="submit" value="Submit" name="Submit"/>

</form>

 

and again here is my script

 

<?php

// Contact subject

$subject ="Website enquiry";

// Details

$message=$_POST[detail];

 

// Mail of sender

$mail_from=$_POST[customer_mail];

// From

$header="from: $name <$mail_from>";

 

// Enter your email address

$to ='robin@rdosolutions.com';

 

$send_contact=mail($to,$subject,$message,$header);

 

// Check, if message sent to your email

// display message "We've recived your information"

if($send_contact){

echo "We've recived your contact information";

}

else {

echo "ERROR";

}

?>

 

as i said it s working except the name doesnt get sent and i also want to add the website field..

 

thanks in advance..

Link to comment
Share on other sites

no need for validation or anything..

 

Oh, you don't know how bad you need validation.

 

Try something like:

<?php
// Contact subject
$subject ="Website enquiry";
// Details
$message=trim(strip_tags($_POST['detail']));
$name = trim(strip_tags($_POST['name']));
$webste = trim(strip_tags($_POST['website']));
// Mail of sender
$mail_from=trim(strip_tags($_POST['customer_mail']));
// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='robin@rdosolutions.com';
$message = $name . PHP_EOL . $website . PHP_EOL . $mail_from . PHP_EOL . $message;
$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>

Link to comment
Share on other sites

Hey guys thanks so much for all your help... jcbones thank you so much... you have solved my problem... yes i am sure i will need validation at some point but my site doesnt get so many visiters just yet..

 

thanks again for all your help...

Link to comment
Share on other sites

One more thing actually.. If i want to direct it to one of my pages if processed successfully of not. so i would design another two pages for this.. but what code do i use?

 

many thanks n advance..

 

rob

Link to comment
Share on other sites

Instead of simply echoing the success/failure messages, you can use a header() redirect at that point.

 

Hi Pikachu,

 

Thanks for the advice on that. But I have no idea how to do what you just said.. hahaha.. So sorry but I am very novice with php... do you think you would be able to just give me the code for that? I would be very grateful.

 

Cheers

Rob

Link to comment
Share on other sites

Dear all concerned,

 

I would like to thank each and every one of you who have so kindly given me your time and knowledge in helping me with my contact form. Thanks to all of you it is now finally complete and in working order.

 

Please feel free to browse through my website and give me your professional opinions on my design and content. All comments are much appreciated.

 

http://www.rdosolutions.com

 

Many thanks

Rob

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.