PHP Form is not functioning - php

I'd greatly appreciate some help with my PHP contact form. I've followed a number of tutorials to try and get this working and I am still without luck. The code is as follows.
N.B. I am using styling elements defined within the Twitter Bootstrap Framework and the form appears in a modal-style popup (although I can't see why this would affect the form).
Is there anything else I might have to activate/configure on my hosting side apart from having PHP enabled.
FORM CODE:
<form method="POST" action="mail.php">
<fieldset>
<input type="text" name="first_name" placeholder="First Name">
<input type="text" name="last_name" placeholder="Last Name">
<input type="email" name="email" placeholder="Email Address">
<div class="controls controls-row">
<input type="date" name="check_in" placeholder="Check in Date"><span class="help-inline">Check-in</span>
</div>
<div class="controls controls-row">
<input type="date" name="check_out" placeholder="Check out Date"><span class="help-inline">Check-out</span>
</div>
<input type="number" name="rooms" min="1" max="7" placeholder="Number of Rooms">
<input type="number" name="occupants" min="1" max="14" placeholder="Number of Occupants">
<textarea rows="3" name="additional" input class="input-xparge" placeholder="Additional requirements" class="span5"></textarea>
</fieldset>
<div class="modal-footer">
<button type="submit" input type="submit" id="submit" class="btn btn-block btn-large btn-success" value="submit">Submit</button>
</div>
</form>
PHP CODE:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$check_in = $POST['check_in'];
$check_out = $POST['check_out'];
$rooms = $POST['rooms'];
$occupants = $POST['occupants'];
$additional = $_POST['additional'];
$from = "From: $first_name";
$to = "lloyd.rees09#bathspa.ac.uk";
$subject = "New Booking Enquiry";
$body = "First Name: $first_name\n Last Name: $last_name\n Email: $email\n Check In: $check_in\n Check Out: $check_out\n Number of Rooms: $rooms\n Number of Occupants: $occupants\n Additional Information: $additional";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Thank you kindly in advance!

don't you need to add name="submit" to the 'button' tag?
in your php, you are testing 'if ($_POST['submit']) {'
and $_POST['submit'] might not being getting set without name="submit" in that tag

Related

HTML form showing the php code after submit

I have a simple html form what I use in all of my websites in different servers.
In a new server I found a problem I cant solve by my own.
The problem is that after hit the submit button php code doesnt send the email to the server email and doesnt direct to the thankyou.html page. But it does direct to mydomain.com/contactform.php and shows the php code. Firstly I though something wrong with the server configuration, but i didnt find anything. Could anyone give my any advice where to search? Thanks in advance!
PHP code:
<?php
if($_SERVER["REQUEST_METHOD"] === "POST") {}
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$place = $_POST['place'];
$number = $_POST['number'];
$csoportjelleg = $_POST['csoportjelleg'];
$message = $_POST['message'];
$recaptcha_secret = "secret-key";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true);
if($response["success"] === true){
$mailTo = "info#serveremail.com";
$headers = "From: ".$mailFrom;
$txt = "Feladó: ".$name."\nInnenjönnek: ".$place."\nLétszám: ".$number."\nCsoportjelleg: ".$csoportjelleg."\n\nÜzenet: ".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: thankyou");
}else{
header("Location: error");
}
}
?>
HTML code:
<form action="contactform.php" method="post" class="ajanlatkeres-form">
<h2 class="centered-h2">Általános adatok</h2>
<input type="text" name="name" placeholder="Teljes név*" class="feedback-input" required>
<input type="text" name="mail" placeholder="E-mail*" class="feedback-input" required>
<h2 class="centered-h2">További adatok</h2>
<input type="text" name="place" placeholder="Honnan jöttök?" class="feedback-input">
<input type="text" name="number" placeholder="Hány fős a csoport?" class="feedback-input">
<div class="feedback-input-div">
<p class="form-p">Csoport jelleg?</p>
<label class="container">Munkahelyi
<input type="radio" name="csoportjelleg" required>
<span class="checkmark"></span>
</label>
<label class="container">Iskolai
<input type="radio" name="csoportjelleg">
<span class="checkmark"></span>
</label>
<label class="container">Családi
<input type="radio" name="csoportjelleg">
<span class="checkmark"></span>
</label>
<label class="container">Egyéb
<input type="radio" name="csoportjelleg">
<span class="checkmark"></span>
</label>
</div>
<h2 class="centered-h2">Üzenet</h2>
<input type="hidden" name="subject" value="Csoportos ajánlatkérés">
<textarea name="message" placeholder="Ajánlatkérés üzenete:*" class="feedback-input" required></textarea>
<div class="g-recaptcha" id="rcaptcha" data-sitekey="6LdBDf4UAAAAAJ50InC0WKfVep4263x3Bmuz9-60"></div><br/>
<div class="button-form">
<button id="submit_form" class="contactbutton" type="submit" name="submit">Küldés</button>
</div>
UI.: PHP form location: I use thankyou and error without .html extension because there are no extensions in my ULR-s thanks to .htaccess manipulations. In other servers that was absolutly ok.
Updated, thanks kerbh0lz!

Form action throws 'POST /mail.php Error (404): Not found'

I want to send an email via html form with the help of php. However, it keeps
throwing same error every time claiming it couldn't find php file. I've tried
everything:
Installing NodeJs and running the code in localhost.
changing name="name" name="email" to something else.
Linking to another php script in case
something wrong with it.
Reading every SO page on this issue and trying every solution from there.
No luck at all.
Still POST /mail.php Error (404): "Not found". What am I missing here?
Can you make it work on your system?
HTML:
<form name="contactform" method="POST" action="mail.php">
<div>
<input type="text" name="user_name" placeholder="name" required />
<input type="email" name="user_email" placeholder="email" required />
<textarea type="text" name="comments" placeholder="message" required></textarea>
</div>
<button type="submit">SEND</button>
</form>
PHP:
<?php
$name = $_POST['user_name'];
$email = $_POST['user_email'];
$message = $_POST['comments'];
$formcontent=" From: $name \n Message: $message";
$recipient = "********#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Use this :-
<form name="contactform" method="POST" action="mail.php">
<input type="text" name="user_name" id="user_name" placeholder="name" required />
<input type="email" name="user_email" id="user_email" placeholder="email" required />
<textarea type="text" name="comments" id="comments" placeholder="message" required></textarea>
<input type="submit" id="submit" value="SEND" />
</form>

PHP contact form

I've looked through a lot of pages to find an answer for my problem. But I can't figure out why my form isn't working.
I tried different versions of codes from premade examples but I can't get it to work.
This is my php above my form
<?php
$name = $_POST['name'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'me#email.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $email)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>';
}
}
?>
and this is my form
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="Your Name">
<label>Email</label>
<input name="email" type="email" placeholder="Your Email">
<label>Subject</label>
<input name="subject" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" name="message" placeholder="Your Message"></textarea>
<input id="submit" name="submit" type="submit" value="Send">
</form>
I've only changed my email in this example here the rest is the same. I'm testing this live on a modern server with php 5+ support
Basicly everything works fine except that I don't get an email.
I can't find out how to make it work sadly. Any ideas would be cool :/
Edit: GOD DAMNIT IM A TOTAL IDIOT
Gmail Spam filter is strong in this one.
You have not defined $from.
That is why it is not sending mail.
Also, please check SMTP settings for your machine/server.
SMTP ports may not be configured, that is why mail is not sending.
Try this for the form:
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" type="text" placeholder="Your Name">
<label>Email</label>
<input name="email" type="text" placeholder="Your Email">
<label>Subject</label>
<input name="subject" type="text" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" type="text" name="message" placeholder="Your Message"></textarea>
<input id="submit" name="submit" type="submit" value="Send">
Also try following this working format:
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
You could just replace the value inside the " " with the post function.
Edit ( This will surely work ):
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $email)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>';
}
}
The fourth Parameter for the Mail function is header. Where You can set Mail form and others (http://php.net/manual/en/function.mail.php ). Please set that, and also check the SMTP configuration as "Programming Student" told you.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'to#test.com';
$from = 'from#test.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again! </p>';
}
?>
You have missed to assign the $from variable
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" type="text" placeholder="Your Name">
<label>Email</label>
<input name="email" type="text" placeholder="Your Email">
<label>Subject</label>
<input name="subject" type="text" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" name="message" placeholder="Your Message"> </textarea>
<input id="submit" name="submit" type="submit" value="Send">
</form>
Make sure the php file name "contact.php"
Now My HTML goes this way -->
<body> Fields with * are mandatory.
<form action="mail.php" method="POST">
<fieldset>
<legend>Contact Information</legend>
<p>Name*</p> <input type="text" name="name">
<p>Email*</p> <input type="text" name="email">
</fieldset>
<br />
<br />
<fieldset>
<legend>Other Information</legend>
<p>Website</p> <input type="text" name="website">
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Contact Us</option>
<option value="change">Information Change</option>
<option value="addition">Other</option>
</select>
<br />
</fieldset>
<br />
<fieldset>
<legend>Your Message</legend>
<p>Message*</p><textarea name="message" rows="8" cols="29"></textarea><br /><p>
<input type="submit" value="Send" class="but"> <input type="reset" value="Clear" class="but">
</fieldset>
</form>
</body>
and now the main thing that is PHP goes this way ->
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "youremail#domain.com";
$subject = "Contact Form";
$mailheader = "$name submited the form.";
if (filter_var("$email", FILTER_VALIDATE_EMAIL)) {
if ($email === "" || $message === "" || $name === "") {
echo "ERROR! Email and message and Name are Mandatory. <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
else {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, "Name of Form Here", "Your Form has been submitted. Your problem will be noticed soon. This is a no reply mail address. Please dont reply back. - WeBoosters India", "Thankyou") or die("Error!");
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
}
}
else {
echo "The email is not valid. Please write a proper email address. - <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
?>
Hope you like it! Best of luck.

Contact form Error when submit

Hi guys i'm having a issue i hope you guys can help with, i'm typing in all the fields and then upon pressing submit i'm getting just "Error!" on my screen.
Please see the code:
HTML
<h2 class="formhead">Contact Form</h2>
<br>
<form class="form" action="mail.php" method="POST">
<p class="name">
<input type="text" name="name" id="name" placeholder="John Doe" />
<label for="name">Name</label>
</p>
<br>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" />
<label for="email">Email</label>
</p>
<br>
<p class="number">
<input type="text" name="number" id="number" placeholder="0774XXXXXXX" />
<label for="name">Contact Number</label>
</p>
<br>
<p class="web">
<input type="text" name="web" id="web" placeholder="www.example.co.uk" />
<label for="name">Website</label>
</p>
<br>
<p class="message">
<textarea name="message" id="message" placeholder="Write something to us" /> </textarea>
</p>
<br>
<p class="submit">
<input type="submit" value="Send"/>
</p>
</form>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$website = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Any help would be much appreciated!
Thanks
Sam
Your script always reporting 'Error!' because the mail() function always fails. That's because some index you're using in the php file doesn't match to the input names in your form:
Change these:
$website = $_POST['website'];
to:
$website = $_POST['web'];
Or change it in your form.
Also you have to specify a name for the message textarea:
<textarea name="message" id="message" placeholder="Write something to us" />
This may fail again if it can't connect to mailserver. This is probably you're case if The SMTP is Disabled.
As per my comment, here's an example of a better die statement:
<?
$your_function or die("Error! a") // Just replace the letter a with anything. It serves as a simple link to your function that only you know. so you can go back and check it

Why isn't my php script working?

EDIT: Apparently I have to pay to register another domain on their site to have email capabilities. Sorry for the wasted time, and thanks for the code fixes.
I don't have any experience with php. I am just starting to figure it out. I am trying to get my form to send the entered information to my email. I set an else tag but that is all that happens. If you see any errors please let me know. I really want this to work. See for yourself: Website The codes are:
HTML:
<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="comment" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
<input id="send" name="send" type="hidden" value="1" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['comment'];
$from = 'From: '. $email;
$to = 'powersjesse#yahoo.com';
$subject = 'WEBSITE';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['send'] == "1") {
if (mail($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}}
?>
I get Something went wrong, go back and try again! when I'm trying to submit the form.
Updated code.
<form action="post_comment.php" method="post" id="commentform">
rather than method="request"
edited to add
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
edited (in response to edited question code...) to add http://php.net/manual/en/function.mail.php
your mail() parameters are in the wrong order; should be $to, $subject, $message [, $additional_headers, etc.]
According to your code:
if ($_REQUEST['submit']) {
if (mail($to, $subject, $body, $from))
{
echo '<p>Your message has been sent!</p>';
}
else
{
echo '<p>Something went wrong, go back and try again!</p>';
}
}
Getting the output of:
Something went wrong, go back and try again!
Means that something went wrong with the mail() function, it returned false , otherwise we would see the Your message has been sent! message.
The problem with the mail() function is that it doesn't show any errors or warning , it just returns false.
How to locate the problem?
Try writing something like:
if(mail("your.working.email#gmail.com" , "A subject for example" , "the content of this email","From: no-reply#yourdomain.com"))
echo "We are good";
else
echo "Something not workin";
If it works , check out the value of any of your posted variables right after declaring them.
echo $name;
echo $email;
echo $message;
Make sure that those variables are not empty and that the $email variable contains a legal and validated email address.
If the basic mail usage didn't work - it's something related to your php settings (php.ini) or a limitation by your server (contact your hosting company).
EDIT1: About your html form , the type attribute of the input fields should be text and not name or email.
Instead of <input type="email"...
Write <input type="text"...
<form action="post_comment.php" method="get" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="comment" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
</form>
method must be get/post
Your field "submit" is a HTML button to submit the form and will not be added to the form. Add another input field which is not visible:
<input id="send" name="send" type="hidden" value="1" />
and access the field in PHP with:
if ($_REQUEST['send'] == "1") {
I hope this helps you.
the name of the textarea is comment and you're using it as message in this line,
$message = $_REQUEST['message'];
Needs to be,
$message = $_REQUEST['comment'];
Also, the method of the form submission, needs to be POST like this,
<form action="post_comment.php" method="post" id="commentform">
please change form as follows
<form action="post_comment.php" method="post" id="commentform">
In the form you can use only get/post
Try this
<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="comment" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
and in your post page..
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '. $email;
$to = 'powersjesse#yahoo.com';
$subject = 'WEBSITE';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from \r\n";
//$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
}
-> Use POST instead of REQUEST
-> comment chaged to message
HTML
<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="message" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '. $email;
$to = 'powersjesse#yahoo.com';
$subject = 'WEBSITE';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}}
?>

Categories