Why isn't my PHP form working? - php

It always redirects to the success page, but I never receive the email. I've tried rewriting both the PHP and the HTML, but it won't fix anything and I can never find google pages about how to fix this situation (everything appears to be fine, and most people have problems with receiving an email and not a redirect page). I'm not very familiar with PHP but can't find any errors. This is a contact page for a business website.
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$to = "arsilhavy#gmail.com";
$subject = "Contact From $name";
$mailheader = "From: $email \n Phone: $phone \n Message $message \r\n";
mail($recipient, $subject, $mailheader, $formcontent);
if (mail($to, $subject, $body))
header('Location: success.html');
else
print "Something went wrong" ;
?>
HTML:
<div id="copy">
<form action="mail.php" method="POST" name="contact">
<div class="name">Name</div>
<input required type="text" name="name" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="email">Email</div>
<input required type="email" name="email" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="tel">Phone</div>
<input type="tel" name="phone" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="message">Message</div>
<textarea required name="message" rows="6" cols="25" style="border: solid 1px #081B26; width: 200px;" value=""></textarea>
<br />
<input type="submit" value="Send" class="send">
<input type="reset" value="Clear" class="clear">
</form>
</div>

If you want to send Mails from PHP you will never be happy with this way. I strongly recommend to you to use something like PHPMailer (Download and Instructions here: https://github.com/PHPMailer/PHPMailer ) If you have an existing and properly working E-Mail use this one and configure PHP Mailer to log in with this adress and send Mails via SMTP. If you need an example I can provide one to you, but it is pretty strqaight foreward and very well explained on the given link.
You did not receive any E-Mail because (most likely) your E-Mail did NOT pass the spam filters on your destination e-mail adress. There are quite a lot of meta information and in general also certificates required for your mail to pass the spamfilters of most to all e-mail provider.

Are you trying to run this from your development computer? Chances are your PHP installation isn't configured to send mails.
Open your php.ini file.
Search for [mail function]
Edit settings to your needs

Related

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.

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

PHP Running in some directories but not others

I have a few simple html websites with a contact form that sends emails via PHP. They all works great with no issues at all. I am trying to build another website now using the same contact form PHP script. However, when the user submits the form, instead of getting a success message, they get a "internal server error" message. The PHP script is identical (with the exception of the captcha key). The HTML is identical (with the exception of the captcha key) and some css styling.
Below is the HTML:
<form action="scripts/contact.php" method="post" target="resultMsg" style="width:300px; margin:auto;">
<input class="formInput" type="text" name="name" id="name" placeholder="Name"/>
<br/><br/>
<input class="formInput" type="text" name="email" id="email" placeholder="Email"/>
<br/><br/>
<textarea class="formInput" name="message" id="message" placeholder="Message"></textarea>
<br/><br/>
<div style="margin:auto;width:fit-content">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxx"></div>
</div>
<br/><br/>
<input class="send" type="submit" value="Send"/>
</form>
<br /><iframe name="resultMsg" id="resultMsg" scrolling="no" style="border:0; height:30px; width:100%"></iframe>
Below is the PHP code in the scripts/contact.php file:
<?php
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo("<p style='color:white; width:100%; text-align:center;'>Error: Please check the captcha box.</p>");
exit;
}
$secretKey = "xxxxxxxxxxxxxxxxxxxxxxxx";
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
if($responseKeys["success"]) {
$myemail = "myemail#ymail.com";
$subject = "Contact Form Submission";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$sendMessage = "CONTACT FORM SUBMISSION
NAME:
$name
EMAIL ADDRESS:
$email
MESSAGE:
$message";
mail($myemail, $subject, $sendMessage);
echo("<p style='color:white; width:100%; text-align:center;'>Thank you for your email!</p>");
} else {
echo("<p style='color:white; width:100%; text-align:center;'>Error: message not sent, please refresh the browser.</p>");
}
exit();
?>
I have had issues with this last week, but the hosting company added AddHandler application/x-httpd-ea-php56 .php to the htaccess file and it fixed it. They also added extra htaccess files with this line all over the website directories for some reason. Then I think I may have accidentally deleted or changed some of the htaccess files and now it is not working again. What exactly is needed in which htaccess file for this to work correctly? Any ideas why it doesn't work anymore? Thanks
It turns out the issue was as simple as incorrect file permissions of the PHP script. Changing the permissions of the script to 644 fixed the problem, no more "internal server error".

Contact us 404 error

I am trying to make simple tabs. I am having trouble with the contact us tab. I keep getting a 404 error saying the page can not be found. I have copied some supposed to be working examples from online. I know the page exists because it is a email and i emailed it from my other email. This is my first time building a site. I am putting my PHP inside my html. I have tried making my file index.html and index.php and neither worked. It don't matter to me how i do it, ajax or php. I know i did not need to use jQuery to do what i am trying to do, but it was just easier for me. Thanks again for your help, it is much appreciated!
jQuery
$("#phillya11").click(function () {
$("#soon4").show();
$("#soon").hide();
$("#soon1").hide();
$("#soon2").hide();
$("#soon3").hide();
$('#close4').click(function () {
$("#soon4").hide();
});
});
HTML
<div id="phillya11"><a href="#"
style="text-decoration:none;color: #FFF5ee">soon4</a> </div>
</div>
<div id="soon4">
<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="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</div>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail#myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
CSS
#soon4{
display:none;
width:380px;
margin:25px;
height: 346px;
font-style: normal;
font-weight: 500;
font-size:1em;
background-color:#F10B78
}
#close4{
color: white;
margin:90px
}
Where are you putting your PHP? In the index.html file or a separate file?
If you note on your form tag, you've defined the action as mail.php, which is a file that must exist for the form to post it's data to. This is probably where the 404 is coming from.
You can either leave your PHP in the index page (make sure you change it to index.php) and change the form action to index.php, or move your PHP code to a separate mail.php file.
Try to add the event.preventDefault() in your click function.
$("#phillya11").click(function (event) {
event.preventDefault()
$("#soon4").show();
//....
});
http://api.jquery.com/event.preventDefault/

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