localhost contact form not working? - php

It saying i have a mistake and I've done everything right i hope. I'm using localhost for now.. may that be the problem? if not what is.
I get an error on this row
$name = $_POST['name'];
code:
<?php
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
?>
<form action="submit.php" method="post" name="contact_form">
<input name="name" id="name" type="text">
<input type="submit" name="submit" id="sumitbtn" value="submit">
</form>

There could be a number of factors as to why you are having difficulties with your mail form.
Check to see if mail() is installed and properly configured on your system.
Use additional headers(); one being a From: which is missing in your code, although not mandatory.
Mail will still attempt to send, however it could end up being sent to SPAM or ignored.
Use conditional statements for your submit button and mail() success, including checking if the field is left empty or not.
The following has been successfully tested, sent and received in my INBOX.
PHP
<?php
if (isset($_POST['submit']) && !empty($_POST['name'])){
$name = $_POST['name'];
$to = "email#example.com";
$subject = "add this";
$headers="From: $name <email#example.com>";
$sent = mail($to, $subject, $name, $headers);
if($sent) {
echo "Success.";
} else {
echo 'Sorry, your message could not be sent.';
}
} // brace for submit conditional statement
?>
You can also use the following:
<?php
if (isset($_POST['submit']) && !empty($_POST['name'])){
$name = $_POST['name'];
$from = "user#example.com";
$to = "your#example.com";
$subject = "add this";
$headers="From: $name <$from>";
$sent = mail($to, $subject, $name, $headers);
if($sent) {
echo "Success";
} else {
echo 'Sorry, your message could not be sent.';
}
} // brace for submit conditional statement
?>
Footnotes:
Check your server logs.

mail() doesn't work on localhost without an external library.
Also, you need to do an isset on $_POST['name'];
if (isset($_POST)){
$name = $_POST['name'];
$to = "email#email.com";
$subject = "this";
mail ($to, $subject, $name);
}
Also don't forget to look at headers!

Make this work for you first:
<?php
mail("Lennyperez#mail.com", "add this", "name");
, and ask question again.

Do the following: until the form is not submitted, the $_POST['name'] variable is not set
<?php
if (isset($_POST)) {
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
}
?>

When the page is loaded , it doesn't find post variables and that's normal because you have not clicked the submit button,your $_POST['name'] will exist only after the submit button is pressed.
Solution 1:
Add a condition , mail will be sent only if a button submit is clicked.
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
}
?>
Solution 2:
Separate HTML file containing the form from Your action page

I am assuming you are receiving undefined notice if so please fallow one of these steps.
Add #$name= $_POST['name'];
OR
error_reporting(E_ALL ^ E_NOTICE);

Related

405 Not Allowed Github PHP

I hosted my website on github.My contact form doesn't work.I used PHP and HTML to make it.Here are my php code lines
PHP:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$emailFrom = $_POST['email'];
$mobile = $_POST['number'];
$message = $_POST['message'];
$mailTo = "vukstamenkovic9#zohomail.eu";
$headers = "From: ".$emailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html?mailsent");
}
?>
When I want to contact,I always get this: 405 Not Allowed
How can I resolve this?I searched it and nothing showed up.I would really appreciate help.

php, check if mail is valid

I know that there is a ton of solutions to this problem but i need to validate email in a contact form.
this is my php mailer so far:
<?php
if (isset($_POST['send'])){
$to = "mail#mail.something";
$subject = "new message";
$firstname = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{echo "<script>alert('thanks for the message:) ');</script>";
}else
{echo "<script>alert('sorry, message wasn't send');</script>"; }
}
?>
I have tried with filter_var
$result = filter_var( 'something#something.something', FILTER_VALIDATE_EMAIL );
but, doesn't work.
If my way to sent mail through contact form isn't the "best practice way" feel free to correct me and send me on the right path :D
If you show us your HTML we can help further, but with just your PHP we don't have much to work with.
Add error_reporting(E_ALL); and ini_set('display_errors', 1); to the top of your php page to print your errors.
I am not sure what is causing your errors. But i would like you to try the following code. Replace your if statement with this.
Future note, to add a javascript alert in php you should format it this way,
if(mail($to, $subject, $message, $headers))
{
echo '<script language="javascript">'; //echo open tag
echo 'alert("Your message has been sent")'; // echo alert
echo '</script>'; //echo close tag
}else{
echo '<script language="javascript">';
echo 'alert("Your message was not successful")';
echo '</script>';
}

PHP get the email that was filled in and send a message to that email?

So what I want to do is get the email they filled the forum in with and then send a message to that email they filled in here's what I have: it says its sent but I never get the message:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: My Contact Form';
$to = '$email';
$subject = 'Folio Message';
$body = "Hello";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<center><font color="lightgray"><p>Message Sent Successfully!</p><script type="text/javascript"> function leave() { window.location = "http://webdesign.about.com"; } setTimeout("leave()", 3000); </script></center>';
} else {
echo '<center><font color="lightgray"><p>Ah! Try again, please?</p></font></center>';
}
}
?>
So i have $to = '$email'; so by doing that it should read the email that was filled in right? Idk my PHP skills are weak
any idea's? or advice?
Thank you in advance kind people!
Remove the apostrophes from the $to = '$email';
It should be $to = $email;
With the apostrophes, your $to variable is being set to the string "$email" and not the email that the user entered.
Drop the single quotes around $email, so your code looks like this: $to = $email;.
PHP evaluates content in single quotes as a literal string, without interpreting any variables. So, it is literally using the text $email as the recipient's email address.
Two things here:
From header should contain valid mail address. E.g.
$from = 'From: youraddress#domain.com';
As others said fix the single quotes. You can just use $email in mail call like
mail ($email, $subject, $body, $from)
On a different note, consider sanitizing your POST parameters. Especially if your script is on publicly available site.

How to redirect to success / fail pages after php form submission? [duplicate]

This question already has answers here:
php redirect after form success
(3 answers)
Closed 9 years ago.
I have successfully integrated the following form submission code into my site, and it works great. However I would like to have the code redirect the user to one page if the form submission is successful, and a different page if it fails. How could I adapt the following code to do that? It's really starting to get on my nerves! :-P
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "email#email.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
EDIT:
Ok I have changed the code to:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "email#email.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: mailer-success.htm");
}else{
header("Location: mailer-fail.htm");
}
exit;
?>
This works however it never goes to the fail page. I'm guessing that's because the email is always sent even if fields are empty. I have jquery verification in place (which I have disabled for testing purposes) but that obviously only works for users with javascript enabled. How could I alter the code to only show the success page if the form fields contain data? Any help is appreciated.
add a redirect to the page:
// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($phone) && !empty($enquiry)){
// Test to see if the mail sends successfully:
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: success.php");
}else{
header("Location: error.php");
}
}else{
header("Location: back_to_form.php");
}
mail function returns true/false when succeeds/fails. So it's rather simple:
if (mail($recipient, $subject, $formcontent, $mailheader)) {
header('location: success.php');
} else {
header('location: fail.php');
}
Add
header("Location: successpage.html");
to the bottom of your code and remove echo "Thank you!";
Get rid of the echo and then use a header:
header("Location: success.php");
If it fails, redirect to error.php
header("Location: error.php");
If you want to go to the page the form was on, but show an error or success message, do this:
header("Location: original.php?status=error")
Or change error to success if appropriate, you can then use $_GET['status'] to determine whether or not the form failed / succeeded.
Rather than send the client a redirect, resulting in another call to your web server, I believe a better way of doing this would be to use a PHP include.
if (mail($recipient, $subject, $formcontent, $mailheader))
include 'success.php';
else
include 'fail.php';
if (failure) {
header("Location: success.php");
exit;
} else if (success) {
echo "thank you";
}

php sending email

mailer.php
<?php
if(isset($_POST['submit'])) {
$to = "abc#gmail.com";
$subject = "Contact via website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "1";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "0";
}
?>
jquery code
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
$('span.msg').text((data == "1") ? 'Your Message has been received. Thank you' : "Could not send your message at this time").show();
});
return false;
});
I am always getting
Could not send your message at this time
I have almost no knowledge in php, whats i am doing wrong?
Your php script never prints just 1 it prints 0 or 1Data has been submitted to... thus (data == "1") can never be true.
btw: Your script could at least use the return value of mail() to decide whether it signals success or failure.
First of all, check server's mail settings. If your script resides on a server, which you don't administer, the simplest way is to do this is creating a simple PHP file with the following content:
<?php
mail("youraccount#example.com","test","works"); <br>
?>
Run it and check your e-mail.

Categories