My php webform was working until the point I added an if(isset) and also a reCaptcha.
Help?
Tried to debug the code - could not find an issue
html
<form class = "form-inline d-flex justify-content-center" action="contact.php" method = "post">
<div class="mc-field-group-1">
<input name="name" type="text" placeholder="Name" id="mce-LNAME"/>
</div>
<div class="mc-field-group-2">
<input name = "email" type="text" placeholder="Email address*" class="required email" id="mce-EMAIL" required/>
</div>
<div class="mc-field-group-3">
<input name = "subject" type="text" placeholder="Subject" class="required email" id="mce-EMAIL"/>
</div>
<div class="mc-field-group-4">
<textarea name="message" rows="10" cols="39" placeholder="Your message"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="my_key(have the real key here in my code)"></div>
<div class="clear">
<input type="submit" name="submit" id="mc-embedded-subscribe" class="button" value="Send">
</div>
</form>
php
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "realemail#gmail.com";
$mailheader = "From: $email \r\n";
$secretKey = "my_key(have the real key here in my code)";
$responseKey = $_POST['g-recaptcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response ->success)
{
mail($recipient, $subject, $formcontent, $mailheader);
echo "Thank You!";
}
else
{
echo "Invalid Captcha, Please Try Again";
}
}
?>
When I click 'Send' in the form takes me to the contact.php page but email not sent to my gmail inbox. Has nothing to do with gmail as I said was working before I tweaked the code to add the reCaptcha.
Related
So the PHP contact form for my website doesn't work. No idea why. Tried many different templates. Only one worked for a while, before I tried to insert captcha there, and then it also started making 500 error.
No idea how PhP works from the word du tout - I just need a simple form for my personal website to have a captcha and that's all I ever want from PHP at this stage.
Below is the code that doesn't work (supposedly it should, as I have done it from this tutorial https://www.youtube.com/watch?v=4cr85kvM8I0
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$email_from = 'contact#mywebsite.com';
$email_subject = 'New email that you have received';
$email_body = "Name: $name.\n".
"Subject: $subject.\n".
"Email: $email_address.\n".
"Message: $message.\n";
$to_email = "contact#mywebsite.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-to: $email_address\r\n";
//your site secret key
$secretKey = 'mykey';
$responseKey = $_POST['g-captcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response-> success)
{
mailto($to_email,$email_subject,$email_body,$headers);
echo "Message Sent Successfully";
}
else {
echo "Invalid Captcha, try again";
}
}
?>
and this is the contact form (part of it on the html)
Head code:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
...
<div class="container">
<h3>Contact Form</h3>
<form action="contact-form-handler.php" method="POST"
enctype="multipart/form-data" name="contact_form">
<div class="nameemail">
<label for="name">Name</label>
<input name="name" type="text" required placeholder="Name"/>
<label for="subject">Subject</label>
<input name="subject" type="text" required placeholder="Subject"/>
<label for="email">Email</label>
<input name="email" type="email" required placeholder="you#domain.com"/>
</div>
<br>
<label for="message">Message</label><br>
<textarea name="message" cols="30" rows="10" placeholder="Enter your message here ..." required> </textarea>
<div class="center">
<div class="g-recaptcha" data-sitekey="mykey"></div>
<input type="submit" value="Submit">
</div>
</form>
</div>
So why in the mother of God this doesn't work?
I'm writing to create a contact form on my website and then get the information sent to my inbox, but it is not working. Please take a look at my code below (PHP is not my thing) and let me know where I've gone wrong.
here is my html form
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" placeholder="Name"class="form-control" required>
</div>
<div class="col-md-6">
<input type="text" name="number" class="form-control" placeholder="Phone"required>
</div>
</div>
<input type="email" class="form-control" placeholder="Email" name="email" required>
<textarea name="message" class="form-control"style="resize:none;" placeholder="Message" rows="8" cols="30" required></textarea>
<input type="submit" name="submit" value="contact us">
</form>
This is my php code
<?php
$message ='';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['message'];
$number = $_POST['number'];
$msg = $name ."<br>".$email."<br>".$comments."<br>".$number;
$to = "karunagoyal7#gmail.com";
$subject = "Email from Contact Us from";
if(mail( $to, $subject, $msg)){
$message = "<div class='alert alert-success'> your message has been send to saisoftlinktechnologies </div>";
}else{
$message = "<div class='alert alert-primary'> your message has not been send to saisoftlinktechnologies </div>";
}
}
?>
Both html from and php script is in same file.After submitting the form it is not even displaying the message of success or failure.
I have been having trouble getting my php mail script to work. I am fairly new at this so please stay with me.
My php mail form only seems to be returning the headers of the input fields, not the actual information that is typed in them..
Here is what my code is right now. Any help would be appreciated.
<form id="contact_form" class="cf" role="form" name="contact_form" method="post" action="mailscript.php">
<div id="left_form_container">
<div class="half_left_cf">
<input type="text" name="name" id="input_name" placeholder="Name">
<input type="email" name="email" id="input_email" placeholder="Email address">
<input type="text" name="subject" id="input_subject" placeholder="Subject">
</div>
</div>
<div id="right_form_container">
<div class="half_right_cf">
<textarea name="message" id="input_message" type="text" placeholder="Message"></textarea>
<div id="submit_container">
<input type="submit" name="submit" value="Submit" id="input_submit">
</div>
</div>
</div>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "mail#website.com";
$body = "From: ".$name."\n E-Mail: ".$email."\n Message:\n" .$message;
if(empty($_POST['name'])){
$nameErr = "Name is required";}
if(empty($_POST['email'])){
$nameErr = "Email is required";}
if(empty($_POST['subject'])){
$nameErr = "Subject is required";}
if(mail ($to, $subject, $body)){
echo "<script>
alert('Thank you for sending your message!');
window.location.href='index.php'; </script";}
else {
echo "Mail was not sent!";}
?>
As I said, for some reason it just doesn't return any input from the boxes, only the headers..
Thanks for the help.
I am new to PHP but have a site that I need to go live and I cant get the form to work.
Basically what it does is send JUST whats in the "message" field. All other fields are blank for some reason. Also, if possible, I would like help getting it to put a confirmation of "sent mail" without redirecting to a new page, but rather just a pop up message in the form itself.
So here is the code.
<form method="post" action="index.php">
<div class="form-group">
<input class="form-control" name="name" placeholder="Name" type="text">
</div>
<div class="form-group">
<input class="form-control" name="email" placeholder="Email" type="text">
</div>
<div class="form-group">
<input class="form-control" name="text" placeholder="Which class are you interested in?" type="text">
</div>
<div class="form-group">
<input class="form-control" name="phone" placeholder="What is the best number to you reach you?" type="text">
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message" rows="3"></textarea>
</div>
<input class="btn btn-block" type="submit" name="submit" value="Submit">
</form>
and here is the PHP
<?php
$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['class'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site';
$to = '**********#gmail.com';
$subject = 'Message from **** Site';
$body = "From: $Name\n E-Mail: $Email\n Class: $Class\n Phone: $Phone\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>';
}
}
?>
Any help here would be greatly appreciated.
Made one change to this code,
<html>
<form method="post" action="mail.php">
<div class="form-group">
<input class="form-control" name="name" placeholder="Name" type="text">
</div>
<div class="form-group">
<input class="form-control" name="email" placeholder="Email" type="text">
</div>
<div class="form-group">
<input class="form-control" name="class" placeholder="Which class are you interested in?" type="text">
</div>
<div class="form-group">
<input class="form-control" name="phone" placeholder="What is the best number to you reach you?" type="text">
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message" rows="3"></textarea>
</div>
<input class="btn btn-block" type="submit" name="submit" value="Submit">
</form>
</html>
And check the PHP file,
$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['class'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site';
$to = '**********#gmail.com';
$subject = 'Message from **** Site';
var_dump($_POST);
?>
All the variables are being passed.
For having the answer in the same page without redirecting, use jQuery AJAX requests like this example. Use Javascript. http://www.formget.com/submit-form-using-ajax-php-and-jquery/
I'll post the answer for your solution if you still have issues with the AJAX.
Assign the $_POST values inside the if ($_POST['submit']) check.
<?php
if ($_POST['submit']) {
$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['text'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site';
$to = '**********#gmail.com';
$subject = 'Message from **** Site';
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>';
}
}
?>
For submit the form without page refresh use jQuery.post().
http://api.jquery.com/jquery.post/
I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();
You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.