404 Error with PHP send form

404 Error with PHP send form
0

#1

I’ve recently put my first site online - https://glaucomaspecialistsofsanfrancisco.com/
The issue I’m having is that after filling out the contact form and hitting send, the console throws a 404 error and no email is sent.

html -

<form id="my_form" onsubmit="submitForm(); return false;">
                                           <p> <label>First Name</label>
                                              <input type="text" name="first_name" id="firstName" required> </p>
                                              <p> <label>Last Name</label>
                                                <input type="text" name="last_name" id="lastName" required> </p>
                                                <p> <label>Phone Number</label>
                                                  <input type="text" name="phone_number" id="phoneNumber" required> </p>
                                                  <p> <label>Email Address</label>
                                                    <input type="email" name="email_address" id="emailAddress" required> </p>
                                                    <p class="full"> <label>Message</label>
                                                      <textarea name="message" rows="5" id="message" required></textarea> </p>
                                                      <p class="full">
                                                        <button id="button" type="submit" value="Submit Form"><span id="status"></span>Email</button>
                                                      </p>
                                                    </form>

PHP -

<?php


if( isset($_POST['name']) && isset($_POST['lastName']) && isset($_POST['phoneNumber']) && isset($_POST['emailAddress']) && isset($_POST['message']) ){
	$name = $_POST['name'];
  $lastName = $_POST['lastName'];
  $phoneNumber = $_POST['phoneNumber'];
	$emailAddress = $_POST['emailAddress'];
	$message = nl2br($_POST['message']);
	$to = '[email protected]';
	$from = $emailAddress;
	$subject = 'Schedule Inquiry';
	$message = "<b>Name:</b> '.$name.' <b>LastName:</b> '.$lastName.' <br><b>Phone:</b> '.$phoneNumber' <b>Email:</b> '.$emailAddress.' <p>'.$message.'</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.";
	}
}
?>

JS -

function _(id){ return document.getElementById(id); }
function submitForm(){
	_("button").disabled = true;
	_("status").innerHTML = 'please wait...';
	var formdata = new FormData();
	formdata.append( "firstName", _("firstName").value );
  formdata.append( "lastName", _("lastName").value );
	formdata.append( "phoneNumber", _("phoneNumber").value );
  formdata.append( "emailAddress", _("emailAddress").value );
  formdata.append( "message", _("message").value );
	var ajax = new XMLHttpRequest();
	ajax.open( "POST", "index.php" );
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			if(ajax.responseText == "success"){
				_("my_form").innerHTML = '<h2>Thank you '+_("name").value +', your message has been sent.</h2>';
			} else {
				_("status").innerHTML = ajax.responseText;
				_("button").disabled = false;
			}
		}
	}
	ajax.send( formdata );
}

#2

There is a 404 message, because you have specified in the following line to post the form results to a file named index.php

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

I assume you do not have a file in the same directory as the html file where the form is located, because when I try to go to https://glaucomaspecialistsofsanfrancisco.com/index.php, I get a 404 message.

Maybe the actual file is supposed to be contact.php? I ask, because if I go to https://glaucomaspecialistsofsanfrancisco.com/contact.php, I do not get a 404 error message, so that file does exist.


#3

Thank you for the input Randall. You were completely right about (“POST”, “index.php”), just needed to correct that stupid mistake on my end. I’m no longer receiving the error message, however, it seems the info from the contact form is still not being sent to the designated address. I have checked in Cpanel to verify it’s the correct address. Not sure if my code is missing something.


#4

Are you getting the “success” response back from the php script, meaning you are seeing the Thank you message created by the JavaScript?


#5

I am not receiving either the success or the error message I put in the php


#6

Your if statement is checking for a field name from the form which does not exist in the formdata object you submit to contact.php

You just need to compare your form fields submitted and the form fields you are checking in your if statement.


#7

Randall you’re a gentleman and a scholar, thank you for your help.