PHP Contact Form not submitting and shows a white screen [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm new to PHP and I'm making a contact form for my website. However whenever I fill out and submit the form it leads me to a blank page and I never receive an email. I have tested it when my website is live. My host is infinity free if it matters.
I tried troubleshooting such as making minor modifications to the code (changing a few names, etc.) Nothing worked.
<html>
<section id="contact">
<div class="container-contact">
<div style="text-align:center">
<h3> Contact Me </h3>
<p> I will get back to you shortly. </p>
</div>
<div class="row">
<div class="column">
<form id="contact-form" method="post" action="contactform.php">
<label for="firstname">First Name</label>
<input type="text" id="firstname" placeholder="Your first name..." required>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" placeholder="Your last name..." required>
<label for="email">Email Address</label>
<input type="email" id="email" placeholder="Your email..." required>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write your message..." style="height:170px" required></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
</div>
</section>
</html>
<?php
$firstname = $_POST['firstname']
$lastname = $_POST['lastname']
$user_email = $_POST['email'];
$message = $_POST['message']
$email_from = 'myemail#domain.com'
$email_subject = "New Form Submission from $firstname.\n";
$email_body = "First Name: $firstname.\n".
"Last Name: $lastname.\n".
"Email: $user_email.\n".
"Message: $message.\n";
$to = "myemail#domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply To: $user_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: contact.html");
?>
Τhe HTML file name is contact.html and the PHP file is contactform.php
The form doesn't submit and I don't receive an email. Am I doing something wrong? Do I need to setup something else?
Thanks in advance,
Thomas

Guessing you are getting undefined index errors and your environment is not set to display errors. I would add the name attribute to the input fields that are missing it as a start.
For example the first name field
<input type="text" id="firstname" placeholder="Your first name..." required>
should be
<input type="text" name="firstname" id="firstname" placeholder="Your first name..." required>
In addition you should look at turning errors on or viewing the log file.

Due to PHP parse error, you are getting blank page. You need to use semicolon(;) in PHP, in order to end a statement.
For example, instead of,
$firstname = $_POST['firstname']
it should be,
$firstname = $_POST['firstname'];

Related

Question about contact form HTTP ERROR 405 crash

so I am trying to make a link to contact form but it seems to be crashing and I do not know why is this happening. I double checked the link and variable name, but seems like error 405 happens without stopping every time I click.
This is my html part
<h3>Contact Form</h3>
<div class="containerofcontact" style="text-align: left !important;">
<form method="POST" action="contactform.php">
<label for="name">Name</label>
<input type="text" id="Name" name="Name" placeholder="Your name..">
<label for="email">Email</label>
<input type="text" id="Email" name="Email" placeholder="Your Email">
<label for="subject">Subject</label>
<textarea id="Subject" name="Subject" placeholder="Place Detail of Concern" style="height:200px"></textarea>
<input type="submit" value="Send">
</form>
</div>
This is php part
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$text = $_POST['Subject'];
$mailTo = "blankforshare";
$headers = "From: ".$email;
$txt = "You have received an e-mail from ".$name."\n\n".$text;
mail($mailTo, $headers, $txt) or die("Error!");
header("Location: index.html");
?>
Thank you for the help.

Why do I get a blank white screen when my form is submitted?

I am creating a contact form that uses bootstrap HTML framework and then PHP to validate the form fields and send the data to an email. I just asked a question about why I was getting a 404 Error when I hit the submit button which was solved by changing out some of the wrong quotation marks I had around my action and method elements. After changing those, and fixing a couple of PHP code errors, my form now takes me to a blank white screen on button submit (address bar reads myexamplewebsite.com/php/contact-form.php), and I have tried finding a solution to this issue as it seems pretty common but was not successful in solving my own problem. For further information, I am hosting my site through NameCheap and have been using the cPanel to upload all web assets. I currently have my index file saved as HTML, but after watching many youtube videos wonder if I need to change it to a .php extension for the form to work. Lastly, before I post my code, I have not tried adding the invalid_class_name to my code yet.
My HTML:
<form name="contact_form" action="php/contact-form.php" method="POST" class="row g-4">
<div class="col-md-6">
<label for="first-name" class="form-label">First Name</label>
<input type="text" name="first-name" class="form-control" id="first-name" placeholder="John" required>
</div>
<div class="col-md-6">
<label for="last-name" class="form-label">Last Name</label>
<input type="text" name="last-name" class="form-control" id="last-name" placeholder="Smith" required>
</div>
<div class="col-md-6">
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" id="email-address" required>
</div>
<div class="col-md-12">
<label for="notes" class="form-label">Notes</label>
<textarea class="form-control" name="notes" id="notes" rows="4" placeholder="Include any additional information"></textarea>
</div>
<div class="col-12">
<button id="btnsubmit" type="submit" name="btnsubmit" class="pcs-cta-button form-submit-button">Submit</button>
</div>
</form>
My PHP:
<?php
$message_sent = false;
if (isset($_POST['email']) && $_POST['email'] != '') {
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
//submit the form
$userFirstName = $_POST['first-name'];
$userLastName = $_POST['last-name'];
$userEmail = $_POST['email'];
$message = $_POST['notes'];
$messageSubject = "New Concrete Job";
$to = "myemail#gmail.com";
$body = "";
$body .= "From: ".$userFirstName." ".$userLastName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Notes: ".$message. "\r\n";
mail($to,$messageSubject, $body);
$message_sent = true;
}
else {
$invalid_class_name = "form-invalid";
}
}
?>
If anyone has any tips for getting forms to work with NameCheap/cPanel I would appreciate the advice!
If your index page contains some php code, then you have to change the extension to '.php'.
Either ways, you may need to add
//previous codes
mail($to,$messageSubject, $body);
$message_sent = true;
header('Location: myexamplewebsite.com');
to redirect back to the index page

Contact form with reCaptcha [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I know this may have been submitted before (sorry)
I have basic form, these are the details id like to be sent, however i cannot get the reCaptcha to work with it. I have googled all day, but when i try other peoples code (amending to fit mine) it doesnt seem to work.
I would like: Name, Email, Number, newsletter (yes/no) and recaptcha to be sent/work.
Can someone please give me an idea where i may be going wrong? what i may need to add?
Thanks in advance!
Here is my Form (html)
<form method="POST" action="Form_Activation.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" value="" placeholder="you#example.com" required/>
</div>
<div class="form-group">
<label for="number">Number:</label>
<input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
</div>
<div class="form-group">
<input type="checkbox"/> <b> Subscribe to Newsletter</b>
</div>
<div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
<button type="submit" class="btn btn-default sendbutton">SEND</button>
</form>
Here is my php (basic)
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
//$password = $_POST['password'];
//$keyy = $_SERVER['UNIQUE_ID'];
$msg = "Name: $name\r\n \r\n";
$msg .= "Email: $email\r\n \r\n";
$msg .= "Number: $number\r\n \r\n";
$msg .= "Message: $message\r\n \r\n";
$recipient = "info#islandwebdesign.co.uk";
$subject = "New Website Request";
$mailheaders = "From:$email";
//$mailheaders .= "Reply-To:$email";
mail($recipient,$subject,$msg,$mailheaders);
header("Location: contactus.php?msg=1");
?>
First of all make sure that you've included the necessary JavaScript resource to render reCAPTCHA widget properly, like this:
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the reference:
Displaying the widget
Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,
Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,
g-recaptcha-response - a POST parameter in the submitted form
grecaptcha.getResponse(widget_id) - will provide the response after the user completes the captcha.
A string argument to the callback function specified in the config object passed to the render method.
Here's the reference:
Verifying the user's response
For your purpose use g-recaptcha-response to get the user's response. So your code should be like this:
HTML
<form method="POST" action="Form_Activation.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" value="" placeholder="you#example.com" required/>
</div>
<div class="form-group">
<label for="number">Number:</label>
<input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
</div>
<div class="form-group">
<input type="checkbox"/> <b> Subscribe to Newsletter</b>
</div>
<div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
<button type="submit" name="submit" class="btn btn-default sendbutton">SEND</button>
</form>
Add a name attribute in your submit button.
Form_Activation.php
<?php
if(isset($_POST['submit'])){
//your site secret key
$secret = 'XXXXXXX_Secret-key_XXXXXXX';
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);
if($responseData->success){
// success
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
// so on
}else{
// failure
}
}
}
?>
Don't forget to add your secret key in $secret variable.

Guidance Needed with Basic Contact Form?

I'm totally new to web design and have gotten stuck creating a contact form. FWIW The original 'frame' of the site came from a template I downloaded.
This is what my form looks like on the index.html side of things:
<div class="col-sm-6 col-md-6">
<form action="contact.php" method="post">
<div class="form-group">
<!--<label for="name">name</label>-->
<input type="text" id="name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="email">email</label>-->
<input type="text" id="email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="message">message</label>-->
<textarea id="message" class="form-control" rows="7" placeholder="Write a message">
</textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
And this is what the contact.php looks like:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "exampleemail#gmail.com";
$subject = "New Contact Us Enquiry";
$text = "A visitor of ninadaviesopticians.co.uk has submitted the following enquiry.\n\nName:$name\n\nEmail: $email\n\nMessage: $message\n\nPlease respond to this enquiry within 24 hours";
mail ($to, $subject, $text);
header("Location:index.html");
?>
When I fill in the contact form, it sends the email to the desired address and gives me the 'A visitor of.....' text without an issue. However, it leaves 'Name', 'Email' and 'Message' fields blank, as if the user never entered them.
Any ideas? Appreciate the help in advance.
Your fields require the "name" attribute to be filled to be sent in POST or GET data, not ID.
So for example, your name field would be this :
<input type="text" name="name" class="form-control" placeholder="Name" />
You can have both an id and a name, though the id is not used for posting data.
Ref : http://www.php.net//manual/en/reserved.variables.post.php
To post data via forms, the fields need to have the name attribute:
<input type="text" id="name" name="name" class="form-control" placeholder="Name" />
The ID is only really useful for client side tools.

Website Contact Form Not Sending Email

So I recently uploaded my first website to an iPage server. The website runs perfectly with the exception of the Contact Form which for some reason refuses to send any email whatsoever from the form.
I use a PHP Script with the Post Method, and tried to fix my code multiple times to correct any error I might have entered, but so far to no avail. Here is the code:
HTML:
<
form action = "js/mailer.php" form method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
mail($to, $subject, $body);
// redirect to confirmation
header('Location: confirmation.html');
} else {
echo "Failure";
}
?>
Can someone help me with this? It might be an error with my hosting server, or an error with my code. Your help is very much appreciated
Additional Comments: Followed the help provided (thank you for that) I made the necessary changes to the HTML and PHP, but the form is still not functional. It does not echo failure or redirect to the confirmation page, and upon inspecting the element with Firefox, I notice that upon hitting the submit button a subdivision appears under it saying "Sending....". But no email is sent, no message is echoed or page opened.
From what i see in the code you posted, the PHP mailing script won't work as you are checking if a POST variable with the name 'submit' exists which it does not as in your form the submit button does not have a name attribute.
Try giving the submit button a name and put that name in the PHP if statement.
Submit button name is missing in your form, You need to add name into your submit button,
<input type="submit" name="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
^^^^^^^^^^^^
instead of
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
Your HTML part should be something like below,
<form action = "js/mailer.php" method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="sendemail" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
In above, I have added name="sendemail" in submit button.
And your PHP script that sends email should be like
<?php
if(isset($_POST['sendemail'])) { // <-- variable name changed
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
$headers = "From: " . $email_field; // <-- Code added
mail($to, $subject, $body, $headers); // <-- Code added
// redirect to confirmation
header('Location: confirmation.html');
exit;
} else {
echo "Failure";
}
?>

Categories