I'm trying to send an email via HTML5 with bootstrap + php.
HTML:
<form name="frmContacto" class="form-horizontal" method="post" action="mail/contact_me.php">
<fieldset>
<legend class="text-center header">Formulario de contacto</legend>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="fname" name="name" type="text" placeholder="Nombre" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="lname" name="name" type="text" placeholder="Apellidos" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-envelope-o bigicon"></i></span>
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="Correo electrónico" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-phone-square bigicon"></i></span>
<div class="col-md-8">
<input id="phone" name="phone" type="text" placeholder="Teléfono" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
<div class="col-md-8">
<textarea class="form-control" id="message" name="message" placeholder="Introduce aquí tu mensaje." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<a href="mailto:xxx#example.com?Subject=Hello%20again" target="_top">
<button type="submit" class="btn btn-primary btn-lg">Enviar</button>
</a>
</div>
</div>
</fieldset>
</form>
PHP:
<?php
// Check for empty fields
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
) {
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'xxx#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Formulario de contacto web: $name";
$email_body = "Ha recibido un nuevo mensaje desde la web.\n\n" . "Detalles:\n\Nombre: $name\n\nEmail: $email_address\n\Teléfono: $phone\n\Mensaje:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
return true;
?>
I don't know if I have to import something in the HTML. It seems that the php mail() function is not being called, since I have a breakpoint in the function line and nothing happens when I click the "Submit" button. Also, the email is the same in $to php function and in the HTML "mailto".
Thanks a lot.
Check the following things-
1. In your HTML you have declared Name Line of form twice, which is not required.
2. You should have setting in your server/PHP configuration file configured to a mail Server. Without Mail server connected, you can't send mails
Have you validate your settings in php.ini (sendmail parameter) ?
You might read this if needed:
https://blog.codinghorror.com/so-youd-like-to-send-some-email-through-code/
The Code is below is well commented so there is no point writing much. Just read through the Comments. It is quite self-explanatory:
PHP
<?php
// GET ALL POSTED FIELD-VALUES AND ASSIGN THEM DEFAULT VALUES OF NULL (IF THEY ARE NOT YET SET...
$lastName = isset($_POST['last_name']) ? htmlspecialchars(trim($_POST['last_name'])) : null;
$firstName = isset($_POST['first_name']) ? htmlspecialchars(trim($_POST['first_name'])) : null;
$email = isset($_POST['email']) ? htmlspecialchars(trim($_POST['email'])) : null;
$phone = isset($_POST['phone']) ? htmlspecialchars(trim($_POST['phone'])) : null;
$message = isset($_POST['message']) ? htmlspecialchars(trim($_POST['message'])) : null;
// VALIDATE THE FIELDS: BUT FIRST CREATE AN ARRAY
// TO HOLD ERROR MESSAGES FOR FIELDS THAT FAILED THE VALIDATION.
$arrErrorBag = array();
$strErrorMessage = "";
// VALIDATE E-MAIL:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$arrErrorBag["E-Mail"] = "The Email you entered in not valid.";
}
// VALIDATE FIRST NAME:
if(!preg_match('#(^[a-zA-z])?([\w\.\-\ ])*\w*$#i', $firstName)){
$arrErrorBag["First Name"] = "First Name Should not be Empty and should contain only alpha-numeric Characters.";
}
// VALIDATE LAST NAME:
if(!preg_match('#(^[a-zA-z])?([\w\.\-\ ])*\w*$#i', $lastName)){
$arrErrorBag["Last Name"] = "Last Name Should not be Empty and should contain only alpha-numeric Characters.";
}
// VALIDATE PHONE:
if(!preg_match('#^(\+\d{1,3})?\s?(\d{2,4})\s?(\d{2,4})\s?(\d{2,4})\s?(\d{2,4})$#', $phone)){
$arrErrorBag["Phone"] = "Telephone Number should be Numeric and Should not be Empty.";
}
// VALIDATE MESSAGE:
if(!preg_match('#([\w\.\-\d\ ])*\w*$#si', $name)){
$arrErrorBag["Message"] = "Message Should not be Empty and should contain only alpha-numeric Characters only.";
}
if(!empty($arrErrorBag)){
// WE HAVE SOME ERRORS SO WE BUILD A STRING MESSAGE BASED ON THE ERRORS WE HAVE GATHERED ABOVE...
$strErrorMessage .= "<h6 class='error-heading'>There are Errors in the Form Please, correct them to continue.</h6>";
foreach($arrErrorBag as $fieldName=>$errorMessage){
$strErrorMessage .= "<div class='error-block'><strong>{$fieldName}: </strong>";
$strErrorMessage .= "<span>{$errorMessage}: </span></div>";
}
}else{
// THE FORM IS CLEAN AND VALID SO WE SEND THE E-MAIL:
$name = $firstName . " " . $lastName;
$to = 'xxx#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Formulario de contacto web: $name";
$email_body = "Ha recibido un nuevo mensaje desde la web.\n\n" . "Detalles:\n\Nombre: {$name}\n\nEmail: {$email}\n\Teléfono: {$phone}\n\Mensaje:\n{$message}";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: {$email}";
mail($to, $email_subject, $email_body, $headers);
// THE MAIL WAS SENT SO YOU CAN NOW REDIRECT TO ANOTHER PAGE... MAYBE A THANK YOU PAGE...
$redirectURL = "thank-you.php"; //<== THIS IS ONLY AN EXAMPLE... THE URL SHOULD RATHER BE ANY PAGE OF YOUR CHOOSING.
header("location: " . $redirectURL);
}
?>
HTML
<!-- BEFORE THE FORM, CREATE A DIV TO HOLD ERROR MESSAGE, IN CASE ERRORS OCCUR... -->
<!-- IN THIS SCENARIO, IT IS IDEAL TO HAVE BOTH THE PHP AND THE HTML INSIDE THE SAME FILE... -->
<!-- THE PHP CODE SHOLD BE AT THE TOP OF THE FILE & THE HTML MARKUP BELOW... -->
<!-- NOTICE THAT THE ACTION ATTRIBUTE OF THE FORM IS NOW EMPTY... THIS FORCES THE FORM TO SUBMIT BACK TO ITSELF: THIS PAGE... -->
<div class="error-msg-wrapper"><?php echo $strErrorMessage; ?></div>
<form name="frmContacto" class="form-horizontal" method="post" action="">
<fieldset>
<legend class="text-center header">Formulario de contacto</legend>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-user bigicon"></i>
</span>
<div class="col-md-8">
<input id="fname" name="first_name" type="text" placeholder="Nombre" value="<?php echo $firstName; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-user bigicon"></i>
</span>
<div class="col-md-8">
<input id="lname" name="last_name" type="text" placeholder="Apellidos" value="<?php echo $lastName; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-envelope-o bigicon"></i>
</span>
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="Correo electrónico" value="<?php echo $email; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-phone-square bigicon"></i>
</span>
<div class="col-md-8">
<input id="phone" name="phone" type="text" placeholder="Teléfono" value="<?php echo $phone; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-pencil-square-o bigicon"></i>
</span>
<div class="col-md-8">
<textarea class="form-control" id="message" name="message" placeholder="Introduce aquí tu mensaje." rows="7"><?php echo $message; ?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary btn-lg" value="Enviar" />
</div>
</div>
</fieldset>
</form>
Related
Hi I have the same issue. Just want to print out the value that was selected on the calendar and include it to send to my email. It prints out to the page but it does not include to the email.
PHP code:
<?php
if (isset($_POST['send'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Attendance Form";
$date = $_POST['date[]'];
$message = "Bro./Sis. ".$name." "."attendance will be at"." ".print_r(date($date['d,m,Y']));
$emailTo = "me#domain.com";
$mailHeaders = "From: " . $name . "<". $email .">\r\n";
if (mail($emailTo, $subject, $date, $mailHeaders)) {
$message ='<div class="alert alert-success d-inline-block" role="alert">Your message has been sent.</div>';
$type = "success";
} else {
$error = '<div class="alert alert-danger d-inline-block" role="alert">Something went wrong, please try again.</div>';
}
}
?>
Hi guys thank you for your comments. I used the datepicker but whenever I use the jqueryui i cannot pick the value so i put the date as a type. This is my HTML:
<div class="container-fluid text-center" id="attendform" style="top: 50%;transform: translateY(25%) !important;position: relative;">
<!--Form for join us-->
<div class="container bg-light p-5 mt-5 d-inline-block text-center">
<h2 class="join-title text-center display-4 mb-0">Join us!</h2><br><br>
<div class="mt-0 mb-3" id="error"><? echo $error.$message; ?></div>
<div class="container text-center">
<form class="" method="post">
<label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
<input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>
<label for="email">Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="user#domain.com" required> <br>
<!---->
<label for="date" class="d-inline-block">Date: </label>
<input type="date" id="datepicker" name="date[]" class="form-control" value=""></input><br>
<input class="btn btn-dark" id="sendbtn" type="submit" name="send" value="Send">
</form>
</div>
</div>
</div>
</div>
I disable the jquery datepicker first and try with the normal calendar
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<!--script-->
<script src="js/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
<!-- <script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
});
</script> -->
I finally figure the answer:
PHP code
<?php
if (isset($_POST['send'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Attendance Form";
$date = $_POST['date'];
$member = $_POST['member'];
$message = "Bro./Sis. ".$name." "."($member)"."\n\n"."Schedule:"." "."($date)";
$emailTo = "me#domain.com";
$mailHeaders = "From: " . $name . "<". $email .">\r\n";
if (mail($emailTo, $subject, $message, $mailHeaders)) {
$message ='<div class="alert alert-success d-inline-block" role="alert">Your message has been sent.</div>';
$type = "success";
} else {
$error = '<div class="alert alert-danger d-inline-block" role="alert">Something went wrong, please try again.</div>';
}
}
?>
I edited my form and put the jquery datepicker
<form class="" method="post">
<label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
<input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>
<label for="email">Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="user#domain.com" required> <br>
<!---->
<label for="date" class="d-inline-block">Date: </label>
<input type="text" id="datepicker" name="date" class="form-control" value=""></input><br>
<p>Are you already a member of the youth?</p>
<label for=member>
<input type="radio" name="member" value="Yes member">Yes
<input type="radio" name="member" value="Non member">No
</select><br><br>
<input class="btn btn-dark" id="sendbtn" type="submit" name="send" value="Send">
</form>
I am trying to create a contact form in php. But the problem I am facing is that when ever I try to submit the form it shows 404 exception rather than the actual file which has the contact form. The theme is custom made. I have added my code below
<?php
if(isset($_POST)) {
if(trim($_POST["name"]) === "") {
$nameError = "Please enter your name.";
$hasError = true;
} else {
$name = trim($_POST["name"]);
}
if(trim($_POST["email"]) === "") {
$emailError = "Please enter your email address.";
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST["email"]))) {
$emailError = "You entered an invalid email address.";
$hasError = true;
} else {
$email = trim($_POST["email"]);
}
if(trim($_POST["phone"]) === "") {
$phoneError = "Please enter your phone address.";
$hasError = true;
} else {
$phone = trim($_POST["phone"]);
}
if(trim($_POST["comments"]) ==="") {
$commentError = "Please enter a message.";
$hasError = true;
} else {
if(function_exists("stripslashes")) {
$comments = stripslashes(trim($_POST["comments"]));
} else {
$comments = trim($_POST["comments"]);
}
}
if(!isset($hasError)) {
$emailTo = get_option("tz_email");
if (!isset($emailTo) || ($emailTo == "") ){
$emailTo = get_option("admin_email");
}
$subject = "[PHP Snippets] From ".$name;
$body = "Name: $name \n\nEmail: $email \n\nPhone:$phone\n\nComments: $comments";
$headers = "From: ".$name." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="row" style="margin-top:150px;">
<div class="bg-container"></div>
<div class="col-md-4">
<div class="card contact-form-card">
<form action="<?php esc_url(the_permalink());?>" method="post">
<fieldset>
<legend>Give us your details</legend>
<div class="form-group label-floating">
<label class="control-label" for="user-name">Enter your full name</label>
<input class="form-control" id="user-name" name="name" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-email">Enter your email id</label>
<input class="form-control" id="user-email" name="email" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-phone">Enter your phone number</label>
<input class="form-control" id="user-phone" name="phone" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="commenets">Comments</label>
<textarea class="form-control" id="commenets" name="comments" type="text"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-raised col-xs-12">Submit</button>
</fieldset>
</form>
</div>
</div>
<div class="col-md-8">
<h1 class="text-center">We are currently maintaining over 1000 properties in north Bangalore</h1>
<div class="owl-carousel">
<div class="item card">
<p class="statement">"Poorna is an immaculate real estate consulting professional, and possess several unique skills up his sleeves like property consulting report writing that are rare to find in a professional framework."</p>
<p class="by">Shariq Saleem, Director, Armchair Solutions India</p>
</div>
<div class="item card">
<p class="statement">"Square Capital Assets understands your needs clearly and they are very easy to work with. Once they taken over managing my property, it was a smooth ride"</p>
<p class="by">Manoj</p>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top:15px;">
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/sign_the_agreement.png";?> alt="Sign the agreement">
<h5>Sign with us</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/choose_your_tenants.png";?> alt="Choose your tenants">
<h5>We will find reliable tenants.</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/collect_security_deposit.png";?> alt="Collect Security Deposit">
<h5>Rental Agreement</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/rent_assured.png";?> alt="Rent Assured">
<h5>Collecting rent and keeping your home safe</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/repairs_under_2000.png";?> alt="Repairs Under Rs.2,000">
<h5>Regular check up and maintenance</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/property_tax.png";?> alt="Property Taxes?">
<h5>Managing taxes and bills</h5>
</div>
</div>
</div>
<?php get_footer(); ?>
Just change the fields name like below
<input class="form-control" id="user-name" name="Fname" type="text">
<input class="form-control" id="user-email" name="Femail" type="text">
You will not get 404 error
:)
Naming a field in the form "name" caused that problem.
I renamed it to customer name and it started working
I have a simple contact form, the html is this part:
<!-- CONTACT FORM -->
<form id="contact-form" name="contactform" class="row">
<!-- CONTACT FORM IMPUT -->
<div id="input_name" class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Il tuo nome">
</div>
<div id="input_email" class="col-md-12">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
</div>
<div id="input_subject" class="col-md-12">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Numero di telefono">
</div>
<div id="input_message" class="col-md-12">
<textarea class="form-control" name="message" id="message" rows="6" placeholder="Il tuo messaggio..."></textarea>
</div>
<div class="col-md-12 sinistra"><br>Quale servizio ti interessa?<br><br></div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="standard"> Standard<br>
</div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="avanzato"> Avanzato<br>
</div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="deluxe"> Deluxe<br>
</div>
<div class="col-md-12"><br></div>
<!-- CONTACT FORM SUBMIT BUTTON -->
<div id="form_btn" class="col-md-12">
<input type="submit" value="Invia" id="submit" class="btn btn-small btn-blue">
</div>
<!-- CONTACT FORM MESSAGE -->
<div class="col-md-12 contact-form-msg">
<span class="loading"></span>
</div>
</form>
with this php file
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$subject = $_REQUEST["subject"];
$msg = $_POST["msg"];
$tipologia = $_POST['tipologia'] ;
$to = "info#gmail.com";
if (isset($email) && isset($name) && isset($msg) ) {
$email_subject = "$name ha inviato una richiesta di ordine";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Da: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Da: $name<br/> Email: $email <br/> Telefono: $subject <br/>
Tipologia: $tipologia <br/> Messaggio: $msg";
$mail = mail($to, $email_subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
but I have a problem with radio input, the form works but doesn't pass the value of radio to email, so the email has the Tipologia without the radio selected for the form... What could be the error?
<form> defaults to a GET method if POST isn't implied.
You're using two POST arrays.
So... use a POST method and all POST arrays.
Either way, everything must match.
You should also check if the radio buttons are set or not, or any other you wish to include.
I fill the next form:
<form name="frmContacto" class="form-horizontal" method="GET" action="./mail/contact_me.php">
<fieldset>
<legend class="text-center header">Formulario de contacto</legend>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i
class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="Nombre"
class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i
class="fa fa-envelope-o bigicon"></i></span>
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="Correo electrónico"
class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i
class="fa fa-phone-square bigicon"></i></span>
<div class="col-md-8">
<input id="phone" name="phone" type="text" placeholder="Teléfono"
class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i
class="fa fa-pencil-square-o bigicon"></i></span>
<div class="col-md-8">
<textarea class="form-control" id="message" name="message"
placeholder="Introduce aquí tu mensaje." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<a href="mailto:xxx#gmail.com?Subject=Hello%20again" target="_top">
<button type="submit" class="btn btn-primary btn-lg">Enviar</button>
</a>
</div>
</div>
</fieldset>
</form>
PHP side:
<?php
$name = isset($_GET['name']);
echo $name;
$email_address = isset($_GET['email']);
echo $email_address;
$phone = isset($_GET['phone']);
echo $phone;
$message = isset($_GET['message']);
echo $message;
// Create the email and send the message
$to = 'xxx#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Formulario de contacto web: $name";
$email_body = "Ha recibido un nuevo mensaje desde la web.\n\n" . "Detalles:\n\Nombre: $name\n\nEmail: $email_address\n\Teléfono: $phone\n\Mensaje:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
return true;
?>
I think GET method is getting wrong data, I fill the form with text strings, but the echo methods are printing 1:
If I fill the form with real strings, why is the echo method printing 1 for each field? Also, I think that the warning is because the parameters are all "1", but this make no sense for me.
You have assigned the value of isset() to the variable:
Determine if a variable is set and is not NULL.
This will return 1 when the variable is set.
$name = isset($_GET['name']);
echo $name;
$email_address = isset($_GET['email']);
echo $email_address;
$phone = isset($_GET['phone']);
echo $phone;
$message = isset($_GET['message']);
echo $message;
The following code will check if the $_GET variable is set, then assign it:
if (isset($_GET['name'])) { $name = $_GET['name'];
echo $name;
}
if (isset($_GET['email'])) { $email_address = $_GET['email'];
echo $email_address;
}
if (isset($_GET['phone'])) { $phone = $_GET['phone'];
echo $phone;
}
if (isset($_GET['message'])) { $message = $_GET['message'];
echo $message;
}
You are printing the return value of isset() and not the array index you are testinging to see if it is set.
$name = isset($_GET['name']);
should be something more along the lines of:
$name = "";
if (isset($_GET['name'])) {
$name = $_GET['name'])
};
or you could use a ternary operator:
$name = (isset($_GET['name'])) ? $_GET['name'] : "";
Your problem is there:
$name = isset($_GET['name']);
echo $name;
Because $_GET exists, isset($_GET['name']) returns 1, which you store in $name. $name is equal to 1 now (or true).
do something like
if(isset($_GET['name']))
{
echo $_GET['name'];
}
You need to use like below:-
if(isset($_GET['name']))
{
$name = $_GET['name'];
}
I've been looking to solve this, but i can't find a way to do it. I already tried other methods i found in here, but none of them were working for me.
I'm building a website with Bootstrap and also using Bootstrap Validator to make a Contact Form. I can make it work perfect and also submit the contact, but the "thank you message" is redirected to another page. I want the "Thank you message" to load on the same page, and take the place of the form, since its a one scrolling page website.
Can anyone help me please? This is the code i have so far:
Ps.: All my CSS file are the original from bootstrap, nothing has been changed.
HTML
<div class="container">
<div class="row">
<!-- form: -->
<section>
<div class="col-lg-8 col-lg-offset-2">
<div class="page-header">
<h2>Formulário de contato.</h2>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="contact-2.php">
<div class="form-group">
<label class="col-lg-3 control-label">Nome completo</label>
<div class="col-lg-4">
<input type="text" class="form-control" name="firstName" placeholder="Nome" />
</div>
<div class="col-lg-4">
<input type="text" class="form-control" name="lastName" placeholder="Sobrenome" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Assunto</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="subject" placeholder="WebSite" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">E-mail</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="email" placeholder="email#contato.com" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Telefone</label>
<div class="col-lg-5">
<input type="tel" class="form-control" name="tel" placeholder="48 0000 0000" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Mensagem</label>
<div class="col-lg-5">
<textarea class="form-control" name="message" rows="10" placeholder="Escreva aqui sua mensagem."></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label" id="captchaOperation"></label>
<div class="col-lg-2">
<input type="text" class="form-control" name="captcha" />
</div>
</div>
<div class="form-group">
<div class="col-lg-9 col-lg-offset-3">
<button type="submit" class="btn btn-primary" name="signup" value="Sign up">Enviar</button>
<button type="button" class="btn btn-info" id="resetBtn">Limpar</button>
</div>
</div>
</form>
</div>
</section>
<!-- :form -->
</div>
</div>
PHP Contact
<?php
// getting
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = trim($_POST['email']);
$emailtosend = 'myemail#hotmail.com'; //E-mail to receive message
$tel = $_POST['tel'];
$subject = $_POST['subject'];
$message = $_POST['message'];
/* Message to show on email. */
$mensagemHTML = '<P>Contact form sent by the website.</P>
<p><b>First Name:</b> '.$firstname.'
<p><b>Last Name:</b> '.$lastname.'
<p><b>E-Mail:</b> '.$email.'
<p><b>Telephone:</b> '.$tel.'
<p><b>Subject:</b> '.$subject.'
<p><b>Message:</b> '.$message.'</p>
<hr>';
//
//
$headers = "MIME-Version: 1.1\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: $email\r\n"; // remetente
$headers .= "Return-Path: $emailtosend \r\n"; // return-path
$envio = mail($email, $subject, $mensagemHTML, $headers);
if($envio)
//echo "<script>location.href='sucesso.html'</script>"; // Página que será redirecionada
?>
If there is anyone here able to help me with it, that would be great!
Have a look at the code below. You need AJAX. Place this in your html page.
The whole project of that code can be found at this link (just in case you want to see other files too).
$(function() {
$("#myform button").click(function() {
// Get form values
var name = $("input#name").val();
var email = $("input#email").val();
var comments = $("textarea#comments").val();
// Validate and prepare values for php
var dataString = 'name='+ name + '&email=' + email + '&comments=' + comments;
// Post data to the php file
$.ajax({
type: "POST",
url: "bin/process-form.php",
async:false, // Wait for the result
data: dataString,
success: function(data) {
if (data=="Email Sent Successfully!"){
$('#myform').html("<div id='message'></div>");
$('#message').html("<h2>Enquiry Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<span class='glyphicon glyphicon-ok'></span>");
});
} else {
$("#myform-errors").show();
$('#myform-errors').html(data);
}
}
});
return false;
});