I used this tutorial as a guide to create a simple contact form. I am new to using php and this seemed easy enough to figure out, but there seems to be some bugs in the code. After implementing the code on my site, the submit button brings me to a page displaying all of my php code in the browser and no email is sent.
$email_to = 'skustrimovic@gmail.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l=document.getElementById("__cf_email__");a=l.className;if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e {}})();
/* ]]> */
</script>'; //the address to which the email will be sent
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
/*the $header variable is for the additional headers in the mail function,
we are asigning 2 values, first one is FROM and the second one is REPLY-TO.
That way when we want to reply the email gmail(or yahoo or hotmail...) will know
who are we replying to. */
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
}else{
echo 'failed';// ... or this one to tell it that it wasn't sent
}
Should I just try to create my own PHP to work the existing HTML/jquery?
Any help would be much appreciated.
The
send_email.phpscript on that tutorial site doesn’t have a<?phpopening . . . that might be your issue?Alternatively, you’ll want to make sure that basic PHP code can run on your server, e.g. a simple helloworld.php:
If not, you’ll need to troubleshoot as to why PHP is not running – e.g. is the webserver running, is php installed, etc.