My Simple Contact.php form is not working - php

I am working on one website and wanted to add a contact form in it but when I tried to create simple contact for testing with fields that I am going to add on the website later it's not working properly.
I have tried some youtube videos for setup my contact.php form I followed every step but when I test it directly shows Error 500.
Specific Error is
This page isn’t working
www.creativeboxx.co.in is currently unable to handle this request.
HTTP ERROR 500
Here is my Testing HTML Form
<form action="contact-form.php" method="post">
<input name="name" placeholder="Full Name">
<input name="mail" placeholder="Email Address">
<input name="mobile" placeholder="Mobile Number">
<input name="reason" placeholder="Reason for Contact">
<textarea name="message" placeholder="message"></textarea>
<button name="submit" type="submit">Send</button>
</form>
I have tried with a few different PHP codes using youtube but nothing working
Here is my last used PHP code for contact.php
<?PHP
if (isset($_POST['submit'])){
$name = $_POST ['name'];
$mailFrom = $_POST ['mail'];
$mobile = $_POST ['mobile'];
$reason = $_POST ['reason'];
$message = $_POST ['message'];
$mailTo = "vishal.pandya#openxcell.info";
$headers = "From: ".$mailFrom;
$txt = "New Inquiry from " .$name.".\n\n".$message;
mail($mailTo, $reason, $txt);
header("Location: index.html");
}
?>
I have learned HTML and CSS only a couple of months ago so I don't have much knowledge about PHP so guys please help me out.
Thanks in Advance.

There could be many reasons for the error, what I can see :
please check if the PHP file name is: contact-form.php and it is in the same directory with your HTML code file (index.html or whatever)
mail($mailTo, $reason, $txt); => this function mail() is inbuild function in PHP.
to use mail() function, you will have to edit php.ini. and this function sometime creates issues and sometime not able to send email.
to send an email please use this library : phpmailer . this is a more widely used and battle-tested library, so it may help you to solve the issue

First, you need to know what error is happening on your page.
This way you can display errors in your page:
error_reporting(E_ALL);
ini_set('display_errors', 1);
when you hit the submit form you need to stop the redirect in order to view the error:
header("Location: index.html"); you need to disable this line to view the error
and most importantly when/if you solve the problem don't forget to remove the display error message (above code) because that would cause problems later on.

Related

PHP Email Form Issue: Cannot POST

I've been working on getting a PHP email form set up on my personal webpage, and I am running into an issue where when I hit submit, the page displays "Cannot post /mail.php".
I've seen several similar questions on this site before, but I haven't found any approaches that have solved the issue. I am new to PHP so I'm not sure how deep the error could be, or if I'm possibly missing something obvious.
I'm using Brackets Live Preview on Mac OS Big Sur.
Things I've tried:
Made sure that the mail.php file is in the same directory as my HTML files
Made sure I'm setting the name attribute in my HTML file so the PHP can pick up on the relevant entered information
Switched POST with GET (POST errors, GET download the PHP file instead of running it)
Downloaded PHP with Homebrew
Redownloaded Brackets
For reference this is my code:
HTML File:
<form action="mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
PHP File:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From $name \n Message: $message";
$recipient = "email.redacted#gmail.com";
$subject = "Contact Form";
$mailheader = "From $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
?>
Any insight into what may be going wrong or some debugging steps to take would be much appreciated!
Here <? php you have whitespace after the ?. Remove it, and see what happens.
It should be <?php to be valid syntax. Otherwise the script breaks.
Edit: I'm not claiming this will solve any logical errors in your code, but that space in there is making the script not even PHP.
Is your form you have method="get". For you to access the information using $_POST you'll need to change it to method="post".
<form action="/mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
With regards to why your Bracket Live Preview is downloading the PHP file instead of running it. It sounds like your Bracket Live Preview isn't configured correctly.
You might be able to get some help from this question.
Using Brackets for PHP files

How to run php files on netlify?

I build a website which has a form. Contact form redirects to contact.php on submit. For some reason whenever I submit, it says page not found.
index.html
...
<form action="contact.php" method="post" enctype="text/plain">
Name:<br>
<input type="text" name="name" class="form-control" required><br>
E-mail:<br>
<input type="email" name="mail" class="form-control" required><br>
Message:<br>
<input type="text" name="comment" size="50" class="form-control" required><br><br>
<button type="submit" value="Send"> Send Message </button>
</form>
...
contact.php
<?php
if($isset($_POST['submit']))
{
$name = $_POST['name'];
$mailFrom = $_POST['mail'];
$message = $_POST['comment'];
$mailTo = "sample#email.com";
$headers = "From: ".$mailFrom;
mail($mailTo, $name, $message, $headers);
header("Location: index.html");
}
?>
I added a build.sh file containing:
#!/bin/bash
php contact.php
I also added ./build.sh in build command. I feel my script is wrong. Please suggest me alternatives to solve this problem.
A Netlify site is deployed to a CDN and serves up static content and although you can run PHP at the time of a deploy, you cannot execute PHP during a page request.
To submit a form, you can use Netlify Forms or some other serverless forms solution.
I recommend using InfinityFree for hosting the PHP site for free. or if your need is just to send an email form and the rest is static pages then i recommend emailjs that will let you send emails for free. so that you can deploy your site in netlify or vercel
You can use 000webhost.com hosting for uploading your php projects.

Trying to create a contact form using PHP with Bluehost [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm new to PHP, but I found some sample code for a web form. I adapted it like this, in a file called contact.php:
<?php
mail('contact#mywebsite.com', $_POST['name'], $_POST['email'], $_POST['subject'], $_POST['message']);
?>
<p>Your message has been sent.</p>
and here's the HTML:
<form action="contact.php" method="post" enctype="text/plain">
Name<br>
<input type="text" name="name" value=""><br><br>
Email<br>
<input type="text" name="email" value=""><br><br>
Subject<br>
<input type="text" name="subject" value=""><br><br>
Message<br>
<textarea name="message" rows="10" cols="50"></textarea><br><br>
<input type="submit" value="SUBMIT">
</form>
It wasn't working, so I did some research and discovered Bluehost requires you to use their own Bluemail for this. So I ditched the PHP and followed their tutorial. That didn't work either, so I did some more research and discovered they discontinued Bluemail. So I went back to the PHP method and changed the email to a bluehost email address (apparently that's required as well).
Long story short, I checked my junk mail folder and found some of the test emails I had tried to send from the form, but they are all blank. No subject, no message content.
So it seems like the contact form is working (in that it sends an email), but the actual information inputted in the form is not coming through. I assume there's a problem with my PHP code?
Any help would be greatly appreciated!
Seems you are using wrong syntax for mail
Try
<?php
$msg = $_POST['name']."<br/>". $_POST['email']."<br/>". $_POST['message'];
mail("contact#mywebsite.com",$_POST['subject'],$msg);
?>
You can try this code (perhaps will work for you):
<?php
$msg = $_POST['name']."<br/>". $_POST['email']."<br/>". $_POST['message'];
if (isset($_POST["email"])) {
mail($_POST["email"],$_POST['subject'],$msg);
echo "Your message has been sent.";
}else{
echo "N0, mail is not set";
}
?>

Saving Form data dynamically

I am using the following code to save the e-mails entered in the form. But on clicking on "Submit", it is giving an internal server error. Please help in rectifying it.
Code for form.php:
<html>
<body>
<form action="email_script.php" method="POST">
<p> Email Address: <input type="text" name="email" size="30"/> </p>
<input type="submit" name="submit" value="Submit"/>
</body>
</html>
Code for email_script.php:
<? php
$email = $_POST('email');
$file = "file.html"
file_put_contents($file, $email . PHP_EQL, FILE_APPEND);
print("...");
?>
You have a gap between <? and php. This should be <?php.
Also, $email = $_POST('email'); should be $email = $_POST['email']; (with square brackets)
Also, as pointed out below, the second line needs a semi colon at the end.
Also, I have never heard of PHP_EQL. Perhaps you mean PHP_EOL?
If you are working in a local environment, consider putting error_reporting(-1); at the top of your php file (after the <?php). This will give more detailed errors than just 'Internal Server Error'. BUT REMEMBER to remove the line before putting the script in a live environment.
ALSO - be aware that just adding data from a form into a file on the server, without validating or cleaning, is very very dangerous. I won't go into more detail as that's not what your question asked, but maybe look into that a little as well.

PHP scripts not running on my website

I am currently going through elithecomputerguy's youtube playlist for php programming. I am on part 7 of his 11 part series, this section talks about sending email with php. He writes out all the php code on notepad++ and uploads it to a standard godaddy web hosting account. I am using the same exact method, copying his code line by line, setting up the files in the same location and when I upload my email_form.php, it displays correctly
<HTML>
<HEAD>
</HEAD>
<BODY>
<form action="email_script.php" method="POST">
<p>Email Address: <input type="text" name="email" size="30"></p>
<p>Subject: <input type="text" name="subject" size="30"></p>
<p>Message: </p>
<p><textarea rows="10" cols="20" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</BODY>
</HTML>
Now I uploaded my email_script.php file just fine and it looks like so:
<?PHP
$from="test#mikesmtgadvice.com";
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
mail($email, $subject, $message,"From:".$from);
print "Your message has been sent: </br?$email</br>$subject</br>$message</p>
?>
so when I go to mysite.com/test/email_form.php, it works and displays this form the way it should. However, when I fill in the form and hit submit, when it tries running the script, I get an 500 Internal Server Error. I have no idea where to look for this error and cannot find the error log, and therefore cannot figure this out. In the video, he does this exact same process and it works, is there a setting I need to change in my server or?
Thank you for your help in advance, I'm sorry for the long winded version but I wanted to be as specific as possible.
You are missing a double quote and ; at the end of the print statement:
print "Your message has been sent: <br />$email<br />$subject<br />$message</p>";
Replace that with your print line and it will fix it.

Categories