How do I get my php code to work

I have made a simple contact form with HTML and PHP. I uploaded both files to my server but it still does not send when I try to send a e-mail. How are they supposed to be linked? For example I know how to link a html and css file together but I cannot see anywhere where it shows how the PHP file is linked to the HTML file. I have attached my code below. Where am I going wrong?
Thanks ion advance

HTML-

<script>
function _(id){ return document.getElementById(id); }
function submitForm(){
	_("mybtn").disabled = true;
	_("status").innerHTML = 'please wait ...';
	var formdata = new FormData();
	formdata.append( "n", _("n").value );
	formdata.append( "e", _("e").value );
	formdata.append( "m", _("m").value );
	var ajax = new XMLHttpRequest();
	ajax.open( "POST", "example_parser.php" );
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			if(ajax.responseText == "success"){
				_("my_form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>';
			} else {
				_("status").innerHTML = ajax.responseText;
				_("mybtn").disabled = false;
			}
		}
	}
	ajax.send( formdata );
}
</script>
</head>
<body>
<form id="my_form" onsubmit="submitForm(); return false;">
  <p><input id="n" placeholder="Name" required></p>
  <p><input id="e" placeholder="Email Address" type="email" required></p>
  <textarea id="m" placeholder="write your message here" rows="10" required></textarea>
  <p><input id="mybtn" type="submit" value="Submit Form"> <span id="status"></span></p>
</form>
</body>
</html>

PHP

<?php
if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['m']) ){
	$n = $_POST['n']; // HINT: use preg_replace() to filter the data
	$e = $_POST['e'];
	$m = nl2br($_POST['m']);
	$to = "john@tvisionfitness.com";	
	$from = $e;
	$subject = 'Contact Form Message';
	$message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$m.'</p>';
	$headers = "From: $from\n";
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\n";
	if( mail($to, $subject, $message, $headers) ){
		echo "success";
	} else {
		echo "The server failed to send the message. Please try again later.";
	}
}
?>

Isn’t this a continuation of your other thread, @Johned22? Why did you start a new one?

I was asked why I didnt post it in the php section so I posted it again in the php section

Okay, I see. Next time, instead of restarting a thread in a different category, you can just ask one of the mods to move it for you. That way you can avoid the confusion of having people responding to both threads.

Is the name of PHP file correct like that in the HTML? Its name should be example_parser.php

Here:

ajax.open( "POST", "example_parser.php" );

So as @littlebirdy asked, is the PHP contained in a file with that name?

Other questions:

  1. Is it correct to use _ throughout the JS code? I am not particularly familiar with Ajax, still learning, but all the examples I’ve seen use a $ sign in that position.
  2. Does it call the PHP script but not send the email, or does it not call the PHP script? I think you can enable logging to confirm exactly what’s going on.
  3. Does your host support PHP scripts?
  4. Does your host allow you to send emails as if you were someone else? I noticed that you’re trying to use the form-fillers email address as the from-address, which is something that a lot of hosts (not that I’ve researched how many) will not allow. I get that you’re not, but if they allow this, it’s no different than allowing you to send a load of spam emails with a seemingly-legit from-address.

the function _() is defined, so no problem there.

$ is typically an alias for the jQuery() function from the jQuery library.

1 Like

OK, of course, I see the function definition now. Thanks. OP, ignore those bits then.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.