Problem with a Contact Form

GreyWolf

Elite Member
Joined
Aug 17, 2009
Messages
1,862
Reaction score
5,847
This is such a simple thing, but I just can't get it sorted out for some reason. lol.

OK, I need just a really basic html contact form to send an email to the contact with some requested info and cc the contact info to my email as well.

I found this contact_form.html, css, js, and php online. it should just copy into a folder and work.
It should do exactly what I need if I just change the text message lines in the php, but first I need the script to work. It should just copy the 4 files into a folder and then work as it is, but when I do that nothing happens when I hit the submit button. I don't know if its something wrong with the code, or if I have something not set right on my server.

Can someone take a look at the code and see if everything looks right?

contact_form.html
Code:
<html>
<head>
<title>Simple jQuery Contact Form With Validation</title>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="contact_form.css" />
<script src="contact_form.js"></script>


</head>
<body>

<div id="mainform">
<!-- Required Div Starts Here -->
<form id="form">
<h3>Contact Form</h3>
<p id="returnmessage"></p>
<label>Name: <span>*</span></label>
<input type="text" id="name" placeholder="Name"/>
<label>Email: <span>*</span></label>
<input type="text" id="email" placeholder="Email"/>
<label>Message:</label>
<textarea id="message" placeholder="Message......."></textarea>
<input type="button" id="submit" value="Send Message"/>
</form>
</div>


</body>
</html>

contact_form.js
Code:
$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();

$("#returnmessage").empty(); // To empty previous error/success message.
// Checking for blank fields.
if (name == '' || email == '') {
alert("Please Fill Required Fields");
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("contact_form.php", {
name1: name,
email1: email,
message1: message,
}, function(data) {
$("#returnmessage").append(data); // Append returned message to message paragraph.
if (data == "Your Query has been received, We will contact you soon.") {
$("#form")[0].reset(); // To reset form fields on success.
}
});
}
});
});

contact_form.php
Code:
<?php
// Fetching Values from URL.
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'From:' . $email. "rn"; // Sender's Email
$headers .= 'Cc:' . $email. "rn"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you...! For Contacting Us.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = '<div style="background-color:#7E7E7E; color:white;">'
. $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
// REPLACE YOURMAIL ADDRESS HERE.
mail("[email protected]", $subject, $sendmessage, $headers);
echo "Your Query has been received, We will contact you soon.";
} else {
echo "<span>* invalid email *</span>";
}
?>

contact_form.css
Code:
@import url(http://fonts.googleapis.com/css?family=Fauna+One|Muli);
#mainform{
width:960px;
margin:20px auto;
padding-top:20px;
font-family: 'Fauna One', serif;
}
#form{
border-radius:2px;
padding:10px 20px;
box-shadow:0 0 15px;
font-size:14px;
font-weight:bold;
width:350px;
margin:20px 260px 0 15px;
float:left;

}
h3{
text-align:center;
font-size:20px;
}
input{
width:100%;
height:35px;
margin-top:5px;
border:1px solid #999;
border-radius:3px;
padding:5px;
}
input[type=button]{
background-color:#123456;
border:1px solid white;
font-family: 'Fauna One', serif;
font-Weight:bold;
font-size:18px;
color:white;
}
textarea{
width:100%;
height:80px;
margin-top:5px;
border-radius:3px;
padding:5px;
resize:none;
}
span{
color:red
}
#note{
color:black;
font-Weight:400;
}
#returnmessage{
font-size:14px;
color:green;
text-align:center;
}
 
It looks like your server does not serve php files. Make a simple test.php file containing the following code '<?php phpinfo(); ?>' and query this by browser http://yourdomain/folder/test.php . You should see the php info page, otherwise can be sure your server do not serving php files.

Files looks as workable.
 
It looks like your server does not serve php files. Make a simple test.php file containing the following code '<?php phpinfo(); ?>' and query this by browser http://yourdomain/folder/test.php . You should see the php info page, otherwise can be sure your server do not serving php files.

Files looks as workable.
Thanks. The php is configured fine on the server. That was first thing I tested. jscript is also fine on my client side.
I thought the code looked good too. Maybe it's something stupid like a random quote out of place? It was a copy/pasted code snippet from a website. I've had problems doing that before where a quote looked right but was actually the wrong unicode or something.

It's ok though, I found a simpler script that's php only. It actually works even better for what I need. The form was formatted in tables, but I was able to strip away all the table tags, and then modified the css from the one I posted. It looks exactly the same on the page now, but the new one works. :)

Thanks for your help anyway.
 
Last edited:
Back
Top