I am using this code for my form but it only sends one variable which in this case is "type". What seems to be my mistake?
php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n subject: $subject \n email: $email \n Type: $type \n Message: $message";
$recipient = "ashele#diloptic.co.za";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "We will call you as soon as possible!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
this is the corresponding html5 code
<form action="mail.php" method="post" role="form" class="contactForm">
<div class="s-12 l-7"><input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" /></div>
<div class="validation"></div>
<div class="s-12 l-7"><input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" /></div>
<div class="validation"></div>
<div class="s-12"><input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at cell number plus code" /></div>
<div class="validation"></div>
<div class="s-12 l-7"><p placeholder="Subject" data-rule="minlen:4">type
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">New Products</option>
</select></p></div>
<div class="validation"></div>
<div class="s-12 l-7"><textarea class="form-control" name="message" id="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea><br/></div>
<div class="validation"></div>
<div class="s-12 m-6 l-4"><button type="submit" name="submit" value="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button></div>
</form>
Try printing out all the post array by using this:
print_r($_POST);
I tried your html form and received all parameters properly.
There you go!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent = " From: $name \n subject: $subject \n email: $email \n Type: $type \n Message: $message";
$recipient = "ashele#diloptic.co.za";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (!mail($recipient, $subject, $formcontent, $mail_header)) {
echo "error";
} else {
echo "We will call you as soon as possible!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
print_r($_POST);
}
}
?>
Tested and works fine.
Example can be found here!
Try it out and let me know the results. Always happy to help!
Related
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.
I have created a contact form and it does work but now I want to display "Message Sent" after submitting the form right below the "Send" button.
HTML file
<form action="send_mail.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback">
<label class="sr-only" for="name2">Name</label>
<input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" />
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="email2">Email address</label>
<input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" />
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="message2">Message</label>
<textarea class="form-control" id="message2" name="message2" placeholder="Message" required="" rows="8"></textarea>
</div>
<div class="6u 12u$(small)">
<input id="copy" name="copy" type="checkbox" />
<label for="copy">Email me a copy of this message</label>
</div>
<input class="btn btn-default" type="submit" value="Send" />
</form>
PHP Code (different file)
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$formcontent ="From: $name \nMessage: $message";
$recipient = "hello#domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
You can simply redirect your php to your html with a parameter like success=true after your mail() function
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: index.php?success=true');
rename your .html to .php
and in your html (now php) under the
<input class="btn btn-default" type="submit" value="Send" />
<?php
if ( isset($_GET['success']) && $_GET['success']==true )
echo "Message Sent";
?>
Just as Funk Forty Niner said:
Use a if/else statement:
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$formcontent ="From: $name \nMessage: $message";
$recipient = "hello#domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)) {
die("Error!");
} else {
echo "Success!";
}
?>
if/else Documentation: http://php.net/manual/en/control-structures.elseif.php
Real simple, use a conditional statement with an if/else.
if( mail($recipient, $subject, $formcontent, $mailheader) ){
echo "Sent";
} else {
echo "Error, check your logs";
}
Plus, if your entire code is in the same file, you best be using a conditional statement for all inputs/POST arrays and checking for empty() fields.
Trying to add recaptcha to my contact form. I cannot get the contact form to redirect to a thank you page. The form does send the email correctly.
<?php
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "my key";
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo 'You are spammer';
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$formcontent="From: $name \n Message: $message \n Email:$email \n Phone:$phone";
$recipient = "example#example.com";
$subject = "Website Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location:https://www.example.com/thank-you.html');
exit();
}
?>
Form Code: Adding site form code as requested
<div class="col-md-4">
<form id="quote-form" action="inc/sendmail.php" method="post">
<div class="sec-title text-center">
<h1>Request for Quote</h1>
<span class="border center"></span>
</div>
<div class="row">
<div class="col-md-12">
<input type="text" name="name" value="" placeholder="Your Name*" required="">
<input type="email" name="email" value="" placeholder="Your Mail*" required="">
<input type="text" name="phone" value="" placeholder="Your Phone*" required="">
<select class="selectmenu" name=message>
<option selected="selected">Select Service</option>
<option value="Need Quote on a prod 1</option>
<option value="Need Quote on a prod 2</option>
<option>Other</option>
</select>
<div class="g-recaptcha" data-sitekey="sitekeygoeshere-removed-forposting"></div>
<button class="thm-btn bg-clr1" name="submit" type="submit">Get a Quote</button>
</div>
</div>
</form>
</div>
I've removed the site key in the code.
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 have a contact form in my website and it doesn't work properly. I get the email but the email it's empty, I don't get the information from the form.
the part of the html in my index.html
<form role="form" form name="contactform" method="post" action="send_form_email.php">
<div class="form-group">
<label>Nombre</label> <input type="text" name="nombre" class="form-control" placeholder="Introduce tu nombre" >
</div>
<div class="form-group">
<label>Apellidos</label> <input type="text" name="apellidos" class="form-control" placeholder="Introduce tus apellidos" >
</div>
<div class="form-group">
<label>Email</label> <input type="emal" name="email" class="form-control" placeholder="Introduce tu email" >
</div>
<div class="form-group">
<label>Telefono</label> <input type="text" name="telefono" class="form-control" placeholder="Introduce tu telefono" >
</div>
<div class="form-group">
<label>Mensaje</label> <textarea name="mensaje" class="form-control" rows="7"></textarea>
</div>
<button type="submit" class="btn btn-default" value="Submit"> Enviar
</form>
</div>
the part of the php file called "send_form_email.php"
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent=" $nombre $apellidos $email $telefono $mensaje";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
<input type="emal" name="email" class="form-control" placeholder="Introduce tu email" > change to
<input type="email" name="email" class="form-control" placeholder="Introduce tu email" >
you have typo in type attribute.
Try this
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent = "{$nombre} {$apellidos} {$email} {$telefono} {$mensaje}";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
if(PHP_OS == "Linux" || PHP_OS == "Darwin") $new_line = "\n"; /* if Linux or Mac */
elseif(PHP_OS == "WINNT") $new_line = "\r\n"; /* if Windows */
$mailheader = "MIME-Version: 1.1" . $new_line;
$mailheader .= "Content-type: text/html; charset=utf-8" . $new_line;
$mailheader .= "From: ".$recipient.$new_line;
$mailheader .= "Return-Path: " . $recipient . $new_line;
$mailheader .= "Reply-To: " . $recipient . $new_line;
mail($recipient, $subject, $formcontent, $mailheader, "-r" . $recipient) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
Perhaps I can help you with an example I used myself:
The HTML contact Form:
<div class="row">
<!-- Content -->
<div id="content" class="8u skel-cell-mainContent">
<section class="12u">
<header>
<h2>Contact</h2>
</header>
<form method="post" action="mail.php">
<div class="row half">
<div class="6u">
<input name="subject" placeholder="Name" type="text" class="text" />
</div>
<div class="6u">
<input name='email' placeholder="Email" type="text" class="text" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<!--a href="mail.php" class="button">Submit</a-->
<input type="submit" class="button" value="Submit">
</div>
</div>
</form>
</section>
</div>
The PHP processing of the form:
<?php
//mail(to,subject,message,headers,parameters)
// Check if the "from" input field is filled out
//if (isset($_POST['from']))
//{}
$to = 'mail#xxxxxx.com'; // my email address
$subject = $_POST['subject']; //name client
$feedback = 'thank you, we will reply soon.';
$header = $_POST['email']; //Email client
$message = $_POST['message'];
$message = wordwrap($message, 70);
$message = <<<EMAIL
Dear (YOUR WEBSITE NAME HERE),
My name is: $subject
$message
Email Client: $header
EMAIL;
// send mail
if($_POST){
mail($to, $subject, $message, $header);
echo $feedback;
}
?>
Remember very carefully, the part from Dear... to EMAIL has to be against the left side of the screen, without any tabs or spaces. Else it won't work!
Hopefully you can use this to your success :)