/    Sign up×
Community /Pin to ProfileBookmark

PHP contact form not working

I would like to have a contact form on my that requires the *fullname, *email address, and *subject and *message. I followed a tutorial to develop my form but for some reason it is not sending my test message.

I’m not experienced enough with PHP to figure out what I am doing wrong, so I am hoping to get suggestions on how to resolve this issue. All questions, suggestions, and possible solutions are welcome. Thanks.

contact form php:

[code]
<?php

// EDIT THE FOLLOWING LINE BELOW AS REQUIRED

$send_email_to = “[email protected]”;

function send_email($name,$email,$email_subject,$email_message)
{
global $send_email_to;

$headers = “MIME-Version: 1.0” . “rn”;
$headers .= “Content-type:text/html;charset=iso-8859-1” . “rn”;
$headers .= “From: “.$email. “rn”;

$message = “<strong>Email = </strong>”.$email.”<br>”;
$message .= “<strong>Name = </strong>”.$name.”<br>”;
$message .= “<strong>Message = </strong>”.$email_message.”<br>”;
@mail($send_email_to, $email_subject, $message,$headers);
return true;
}

function validate($name,$email,$message,$subject)
{
$return_array = array();
$return_array[‘success’] = ‘1’;
$return_array[‘name_msg’] = ”;
$return_array[’email_msg’] = ”;
$return_array[‘message_msg’] = ”;
$return_array[‘subject’] = ”;

if($email == ”)
{
$return_array[‘success’] = ‘0’;
$return_array[’email_msg’] = ’email is required’;
}
else
{
$email_exp = ‘/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/’;
if(!preg_match($email_exp,$email)) {
$return_array[‘success’] = ‘0’;
$return_array[’email_msg’] = ‘enter valid email.’;
}
}

if($name == ”)
{
$return_array[‘success’] = ‘0’;
$return_array[‘name_msg’] = ‘name is required’;
}
else
{
$string_exp = “/^[A-Za-z .’-]+$/”;
if (!preg_match($string_exp, $name)) {
$return_array[‘success’] = ‘0’;
$return_array[‘name_msg’] = ‘enter valid name.’;
}
}

if($subject == ”)
{
$return_array[‘success’] = ‘0’;
$return_array[‘subject_msg’] = ‘subject is required’;
}

if($message == ”)
{
$return_array[‘success’] = ‘0’;
$return_array[‘message_msg’] = ‘message is required’;
}
else
{
if (strlen($message) < 2) {
$return_array[‘success’] = ‘0’;
$return_array[‘message_msg’] = ‘enter valid message.’;
}
}
return $return_array;
}

$name = $_POST[‘name’];
$email = $_POST[’email’];
$message = $_POST[‘message’];
$subject = $_POST[‘subject’];

$return_array = validate($name,$email,$message,$subject);
if($return_array[‘success’] == ‘1’)
{
send_email($name,$email,$subject,$message);
}

header(‘Content-type: text/json’);
echo json_encode($return_array);
die();

?>
[/code]

Contact Form HTML:

[code]
<fieldset id=”contact_form”>
<div id=”msgs”> </div>
<form id=”cform” name=”cform” method=”post” action=””>
<input type=”text” id=”name” name=”name” value=”Full Name*” onfocus=”if(this.value == ‘Full Name*’) this.value = ””
onblur=”if(this.value == ”) this.value = ‘Full Name*'” />
<input type=”text” id=”email” name=”email” value=”Email Address*” onfocus=”if(this.value == ‘Email Address*’) this.value = ””
onblur=”if(this.value == ”) this.value = ‘Email Address*'” />
<input type=”text” id=”subject” name=”subject” value=”Subject*” onfocus=”if(this.value == ‘Subject*’) this.value = ””
onblur=”if(this.value == ”) this.value = ‘Subject*'” />
<textarea id=”msg” name=”msg” onfocus=”if(this.value == ‘Your Message*’) this.value = ””
onblur=”if(this.value == ”) this.value = ‘Your Message*'”>Your Message*</textarea>
<button id=”submit” class=”button”> Send Message</button>
</form>
</fieldset>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@jacjil2Sep 06.2012 — You may need to check your email spam folder. I use Yahoo and I tested a PHP mailer and the message went in there. Script automated emails have been abused therefore they are suspicious. The email might not show right away either, I know mine didn't. I think Yahoo had to review it first.

EDIT: Also if you are using Mamp or Xamp, you may need to edit your relayhost.
Copy linkTweet thisAlerts:
@Jerrell_BauthorSep 06.2012 — Hi Jacjil2 - I am receiving the following message when i send a message. "Could not complete your request. See the errors below!

message is required undefined" . This message is appearing even though I have input a message in the message field. ?
Copy linkTweet thisAlerts:
@Jerrell_BauthorSep 06.2012 — The PHP form script is still not working. I went back to the websites where i downloaded the script and it doesn't even work on there!!! So it looks like I will have to write a PHP form script.. sigh. Can anyone point me to a good tutorial on creating a php form script with validation? Also out of curiosity doesn't a php form have to be within a .php file or can it be in .html file?
Copy linkTweet thisAlerts:
@holyhttpSep 07.2012 — Is the form data even submitted?

You have the tag

<button id="submit" class="button"> Send Message</button>

but the form has no idea what to do when the button is click.

You should add the attribute type="submit"

<button id="submit" class="button" type="submit"> Send Message</button>

Your textarea is named "msg" and yet your PHP is expecting $_POST['message']
×

Success!

Help @Jerrell_B spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...