Simple form using HTML and PHP - page not found - php

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.

Related

PHP 404 contact form on SPA

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).

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?

How to fix this php file for sending email

i have created this php form for enquiry purpose but when i clicked on the submit button it's not working. can you tell me what have i done wrong in this code.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type ="text" name="subject" placeholder="subject">
<input type ="email" name="email" placeholder="email">
<textarea rows="5" name="message" cols="30"></textarea>
<input class="btn" type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$to = "rizwandesigns19#gmail.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST['email'];
$headers = "From : $from";
mail($to, $subject, $message, $headers);
}
echo "Mail Sent";
?>
or can you give me valid php script for this purpose.
To get through google's and other's spam filters you need an address which the mail is sent from. For this you could create a new Gmail-address and use the phpmailer-library.
Edit: Setup with gmail is actually pretty complicated and it will disable your access once in a while which renders it almost unuseable. I started to do it by installing a real mail server on the host machine (Install a Complete Mail Server with Postfix and Webmail in Debian 9) and use the php mail() function (phpmailer can still be used tho).

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?

PHP form not redirecting, going to form post

im using this contact form below. When I was running it on my local machine it was working. But on my server if the form fails it does the right thing and goes to fail.php but when all the fields are filled it goes to send_contact2.php after clicking send instead of success.php
This is the send_contact2.php
if (empty($_POST['name'])
|| empty($_POST['number'])
|| empty($_POST['email'])
|| empty($_POST['messagearea'])
){
header('Location: fail.php');
}
else {
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$messagearea = $_POST['messagearea'];
$to = 'example#gmail.com';
$subject = "Website Message: Contact form";
$message = '$messagearea';
$headers = "From: WebsiteMessage";
mail($to, $subject, $message, $headers);
header("Location: success.php");
}
?>
This is my form
<form name="form1" method="post" action="send_contact2.php">
<input name="name" type="text" placeholder="Your Name"/> <br/>
<input name="email" type="email" placeholder="Your Email"/> <br/>
<input name="number" type="tel" placeholder="Your Number"/> <br />
<textarea name="messagearea" cols="" rows="" id="messagearea" placeholder="Your Message"/></textarea> <br/>
<input name="sumbit" type="submit" value="SEND" id="button2" />
</form>
I have this setting in my cPanel
Any help would be greatly appreciated
if the page stay on send_contact2.php it is because, header("Location: success.php") do not redirect. read this post, it could help. Php header location redirect not working
In my opinion PHP must be outputing some text (char, error or warning?) or you have used a char before <?php in your send_contact2.php file. That's why header is not working. Remember that header() must be called before any actual output is sent.
OP here, managed to sort it in the end
Was a mail server issue, so if anyone in the future gets this problem check your php settings on your hosting and make sure it allows mail() and make sure you set up an email address where you're sending the email to on your server.
thanks to everyone that helped me debug

Categories