PHP 404 contact form on SPA - php

New to PHP-
ran through tutorials and several other threads regarding this issue and I'm struggling quite a bit. I am creating a single page application without a framework and currently running on localhost. Upon submitting the form, I am greeted with a message that says Cannot POST /mail.php
I figure I have the mail.php file in the wrong spot, so I moved it around in every single directory until I decided to just put a copy of it in EVERY directory to try and get something to work. I even posted it in the contact.js file.
contact.js
import AbstractView from "./AbstractView.js";
export default class extends AbstractView {
constructor() {
super();
}
async getHtml() {
return `
<div class="container">
<div class="contact">
<form action="mail.php" method="post">
<table>
<tr>
<td>
<input type="text" name="name" placeholder="Name"><br />
<input type="text" name="email" placeholder="Email"><br />
<input type="text" name="subject" placeholder="Subject"><br />
</td>
<td>
<textarea name="message" rows="5" cols="25" placeholder="Message"></textarea><br />
</td>
</tr>
</table>
<div style="float: right"><button type="submit" name="submit">Send</button> </div>
</form>
</div>
</div>
`;
}
}
mail.php
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$header = "From: ".$name."<".$email.">\r\n";
$recipient = "MYEMAIL#gmail.com";
mail($recipient, $subject, $message, $header)
or die("Error sending mail");
echo "messsage sent";
}
?>
file structure
Any help or guidance would be greatly appreciated

You need to use an HTTP server which can execute PHP programs.
You appear to have written your own server using Node.js then it won't support PHP unless you have designed it - explicitly - to do so (and you'd be better off using server-side JS instead of PHP if you were already in a Node.js ecosystem).

Related

Trying to send an email with PHP from HTML form resulting in an HTTP Error 405

So I wanted to make a contact form for a website that can actually send emails, but ended up with this error :
Here's my html code:
<div class="contact-fast">
<div class="contact-form">
<form id="contact-form" method="POST" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required> <br>
<input name="email" type="email" class="form-control" placeholder="Your Email" required> <br>
<textarea name="message" class="form-control" placeholder="Message" rows="8" required></textarea> <br>
<input type="submit" class="form-control submit" value="SEND MESSAGE">
</form>
</div>
</div>
And here's my PHP code:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'skeremobiel#gmail.com';
$email_subject = 'New Mail From Website';
$email_body = "Username: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "said444b#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
if ($visitor_email!=NULL) {
mail($to,$email_subject,$email_body,$headers);
}
header("Location: http://127.0.0.1:5500/social.html");
die()
?>
Here's how my page looks like before and after submitting the form:
I think the problem is that when i click on the submit button i get directed to 127.0.0.1:5500/contact-form-handler.php (as you can see in the last image in my question). It doesn't execute the php file, but it just opens it.
Any help would be appreciated!
Many things can go wrong.
Make shure php error reporting is on. Google for that.
Try to find where the error code is coming from by reducing your problem.
For instance, comment out mail() and see if it works.
Or replace your php code by a simple text to see if the code runs, if the text appears on your browser screen.
Or Google for error 405. You will learn it is an http error, something in the data exchange between your browser and the server.
Do you have characters before <?php because if you have, the server will not be able to output the HTTP header any more.
Is your php file located at the webserver root and called contact-form-handler.php?

Simple form using HTML and PHP - page not found

I'm looking to send data from an HTML form to an email address using PHP. I have the following code in index.html:
<form class="container" name="rsvp" method="POST" action="form-to-email.php">
<input type="text" class="name" name="name" placeholder="Your Name" />
<input type="email" class="email" name="email" placeholder="Your Email" />
<input type="submit" class="btn btn-primary" value="Send"/>
In the same directory I have form-to-email.php:
<?php
if(isset($_POST['submit'])) {
$to = "myemail#gmail.com";
$subject = "RSVP";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$body = "From: $name_field\n E-Mail: $email_field\n";
echo "<h1>Your form has been submitted</h1>";
mail($to, $subject, $body);
} else {
echo "<h1>Error</h1>";
}
?>
When I click the submit button, I get a 'Page not working' error.
I am extremely new to PHP so I can't understand what is going wrong. I'm using Live Server with VS code and the website is currently being hosted on port 5500.
Should I have another server running in order for PHP to work? I believe I have PHP installed on my machine (Mac), but I don't know if I need to start up XAMMP or something similar even for a simple form like this to work? And if this is the case, how would the form then function on a live server? I usually use Netlify to host my projects.

Issue with contact form (html and php): 405 not allowed

I'm trying to make a very basic contact form using HTML and PHP. For some reason, however, when I click the "submit" button I get the error "405 Not Allowed". Why? How could I fix this? (I'm hosting my website on GitHub)
my HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="send_form_email.php" method="POST">
<input type="text" name="name" placeholder="Full Name">
<input type="text" name="mail" placeholder="Your e-mail">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit" name="submit">Send e-mail</button>
</form>
</body>
</html>
my PHP:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "example#gmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
(PS. I wrote "example#gmail.com" just because I wanted to keep my personal e-mail private on here.
PPS. I have been trying to make a very simple contact form (you write your name mail and message and the owner of the website receives it in the inbox) but none of my (desperate) attempts seems to work. Can anyone help me out?)
Github is a hosting service for static web pages. PHP is not static. Deploying PHP to GitHub Pages - is it possible?

How do I tell my PHP script to run a Javascript function?

I know this has been posted several times, but I just can't seem to figure it out. Here's an overview of what I'm trying to do:
I have an HTML form located in a PHP file. On form submit, the action calls the same PHP page where the form is located. In doing this, my server runs a bit of PHP code at the top of that page that gathers the information inserted into the form and sends it to me in an email. After the email is sent, I want to hide the DIV containing my form and replace it with a hidden one that will appear, thanking the user for submitted the form, etc.
My question is, how do I get the PHP script to run the Javascript function? My email sends just fine, I just can't get the DIV to replace.
Here's my code.
HTML
<form id="contact-form" name="contact-form" action="contact.php" method="post">
<table>
<tr>
<td width="79"><p><font style="color:#ebe775;">*</font>Name:</p></td>
<td width="309"><input type="text" name="name" id="name" placeholder="First Last" size="45" required></td>
</tr>
<tr>
<td><p><font style="color:#ebe775;">*</font>Email:</p></td>
<td><input type="email" name="email" id="email" size="45" required></td>
</tr>
<tr>
<td><p><font style="color:#ebe775;">*</font>Message:</p></td>
<td><textarea name="message" id="message" cols="42" rows="8" required></textarea></td>
</tr>
<tr>
<td style="text-align:center;" colspan="2"><input type="submit" value="Submit" name="submit" id="submit"><input type="hidden" name="parse_var" id="parse_var" value="contact-form"></td>
</tr>
</table>
</form>
</div>
<div id="hidden_div" style="display:none;">
<h2>Thank You!</h2>
<p>Your form was submitted. We'll review it and get back with you as soon as we can.</p>
</div>
</div>
</div>
PHP Script
<?php
if ($_POST['parse_var'] == "contact-form"){
$emailSubject = 'New CONTACT Form Submission';
$to = 'my.email#server.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EOD
The following information was submitted using the CONTACT form at JakesCreativeDesign.com.
Name: $name
Email: $email
Message: $message
EOD;
$headers = "From: contact#jakescreativedesign.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail("$to", "$emailSubject", "$body", "$headers");
}
?>
Javascript Function
<script type="text/javascript">
function showHide() {
var div = document.getElementById("hidden_div");
if (div.style.display == 'none') {
div.style.display = '';
}
else {
div.style.display = 'none';
}
var div = document.getElementById("form");
if (div.style.display == '') {
div.style.display = 'none';
}
else {
div.style.display = '';
}
}
</script>
You don't. PHP runs on the server. Javascript runs on the client. At most PHP could do somethign like
<?php
echo <<<EOL
<script type="text/javascript">
yourFunction();
</script>
EOL;
?>
so that the function's called when the page is loaded by the browser. But otherwise, once the page is sent off to the client, there is NO WAY for PHP to reach out and tell that page to do something again.
You are looking at it backwards. PHP cannot run javascript because PHP runs on the server and javascript runs in the user's browser. You can call PHP from javascript (via AJAX) but you cannot call javascript from PHP.

Do not understand why my Contact Form isn't sending emails

I am trying to make a email send in a pop up on my site that you can see with the link below:
http://www.madaxedesign.co.uk
However it redirects perfectly to the thank you message however after it has redirected it does not implement the PHP. Below I have shown the PHP, HTML and Jquery used for this contact form.
HTML:
<form id="submit_message" class="hide_900" action="/send.php" method="post">
<div id="NameEmail">
<div>
<label for="name">Name*</label>
<input type="text" title="Enter your name" name="name"/>
</div>
<div>
<label for="email">Email*</label>
<input type="text" title="Enter your email address" name="email"/>
</div>
</div>
<div id="MessageSubmit">
<div>
<textarea maxlength="1200" title="Enter your message" name="message"></textarea>
<label for="message">Message</label>
</div>
<div class="submit">
<input type="submit" value="Submit"/>
</div>
</div>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "maxlynn12#gmail.com";
$subject = "Email From Madaxe";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: /thanks.html');
exit();
?>
Jquery:
$('form#submit_message').live('submit', function() {
$('#popup').load('/thanks.html');
return false;
});
I was wondering if anyone could quickly look and see if I am missing anything obvious that I can quickly fix or even point me in the right direction.
Thanks
Your jQuery is interfering.
I think this might help, using AJAX to post your form: jQuery Ajax POST example with PHP
You do not need to your jquery code. actually it prevents your default action form action=send.php. and it does not pass your inputs to send.php
$.live() is deprecated in jQuery 1.9 and that's what you appear to be using. Please either downgrade or use an alternative function like .submit().
$('#submit_message').submit(function (e) {
e.preventDefault();
$('#popup').load('/thanks.html');
}
Also, it is crucial that you sanitise your $_POST inputs before using them for your e-mail headers, otherwise a hacker can inject bad things into your headers.
if you are trying to send mail through localhost you need to change some setting in php.ini file. Refer below link to do this.
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/

Categories