I have been trying to add php to a bootstrap form via the following tutorial: https://bootstrapbay.com/blog/working-bootstrap-contact-form/. But nothing happens when I click the submit button - I just go back/stay to the same page with an empty form. I don't receive an email nor any error actions when I don't fill out some fields either. Below my code. The name of the document is werkwijze.php. Does someone have an idea what I'm doing wrong? Thanks a lot in advance!
<?php
if (isset($_POST['submit'])){
$voornaam = $_POST['voornaam'];
$familienaam = $_POST['familienaam'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$vraag = $_POST['vraag'];
$from = 'info#tbep.be';
$to = 'eefje.vanhemelryck#gmail.com';
$subject = 'Vraag van op uw website www.tbep.be';
$body = "From: $voornaam $familienaam\n E-Mail: $email\n Telefoon: $tel\n Vraag: $vraag";
// Check if name has been entered
if (!$_POST['voornaam']) {
$errVoornaam = 'Gelieve uw voornaam op te geven';
}
if (!$_POST['familienaam']) {
$errFamilienaam = 'Gelieve uw familienaam op te geven';
}
// Check if email en phone has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Gelieve uw emailadres op te geven';
}
if (!$_POST['tel']) {
$errTel = 'Gelieve uw telefoonnummer op te geven';
}
//Check if message has been entered
if (!$_POST['vraag']) {
$errVraag = 'Gelieve uw vraag te stellen';
}
// If there are no errors, send the email
if (!$errVoornaam && !$errFamilienaam && !$errEmail && !$errTel && !$errVraag) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Hartelijk dank. Ik neem zo snel mogelijk contact met u op!</div>';
} else {
$result='<div class="alert alert-danger">Sorry, er was een probleem met het versturen van dit formulier. Alternatief kan u ons een email sturen op info#tbep.be</div>';
}
}
}
?>
<form class="form-horizontal" role="form" action="werkwijze.php" method="post" enctype="text/plain">
<div class="form-group">
<label class="control-label col-sm-3" for="voornaam">Voornaam:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="voornaam" name="voornaam" placeholder="Voornaam">
<?php echo "<p class='text-danger'>$errVoornaam</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="familienaam">Familienaam:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="familienaam" name="familienaam" placeholder="Familienaam">
<?php echo "<p class='text-danger'>$errFamilienaam</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email" name="email" placeholder="Email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="telefoon">Telefoonnummer:</label>
<div class="col-sm-9">
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefoonnummer">
<?php echo "<p class='text-danger'>$errTel</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="vraag">Uw vraag:</label>
<div class="col-sm-9">
<textarea class="form-control" rows="5" id="vraag" name="vraag" placeholder="Uw vraag"></textarea>
<?php echo "<p class='text-danger'>$errVraag</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" id="submit" name="submit" value="Send" class="btn btn-default">Versturen</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
<?php echo $result; ?>
</div>
</div>
</form>
Please Remove enctype="text/plain" from your form.
<form class="form-horizontal" role="form" action="werkwijze.php" method="post">
Or use
<form class="form-horizontal" role="form" action="werkwijze.php" method="post" enctype="multipart/form-data">
Related
I'm using a template for a contact form for my website. The problem is, when I test it on my server I cannot send a message. It will only give me my error message and not send. Did something happen when I translated the original send text to German?
I've tried debugging the vars. I've also tried to echo some string inputs.
This is the html code:
<form id="contact-form" action="mail.php" method="post">
<div class="row">
<div class="col-md-6 form-group">
<label class="sr-only">Name</label>
<input type="text" class="form-control input-lg" name="name" placeholder="Name" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-6 form-group">
<label class="sr-only">Email</label>
<input type="email" class="form-control input-lg" name="email" placeholder="Email" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 form-group">
<label class="sr-only">Betreff</label>
<input type="text" class="form-control input-lg" name="subject" placeholder="Betreff" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 form-group">
<textarea class="form-control input-lg" rows="7" name="message" placeholder="Nachricht"></textarea>
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-lg btn-round btn-dark">Senden</button>
</div>
</div>
</form>
PHP:
<?php
// POST GET.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Form Felder GET.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$subject = trim($_POST["betreff"]);
$message = trim($_POST["nachricht"]);
// Check ob Daten an den mailer.
if ( empty($name) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// 400 (bad request) und raus.
http_response_code(400);
echo "Bitte füllen Sie alle Felder aus.";
exit;
}
// Empfaenger.
$recipient = "info#test.de";
// Betreff.
$subject = "Neue Anfrage von $name";
// Inhalt.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Betreff: $subject\n\n";
$email_content .= "Nachricht:\n$message\n";
// Header.
$email_headers = "Von: $name <$email>";
// Senden.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// 200 (okay).
http_response_code(200);
echo "Vielen Dank! Deine Nachricht wurde versendet.";
} else {
// 500 (internal server error).
http_response_code(500);
echo "Oops! Hier ist ein Fehler passiert, deine Nachricht konnte nicht gesendet werden.";
}
} else {
// 403 (forbidden).
http_response_code(403);
echo "Es gibt Probleme mit deiner Anfrage, bitte versuche es ernuet.";
}
?>
Your input name for subject is in English in your form, but in your php file is in German
Choosing the same lang for all your code can to avoid confussions
Change name attributes to german to make it work
<input type="text" class="form-control input-lg" name="betreff" placeholder="Betreff" >
<textarea class="form-control input-lg" rows="7" name="nachricht" placeholder="Nachricht">
</textarea>
I just made my contact form to function, sort of. It has a few issues:
When the form is sent, it kicks you off the page, directs you to a blank page with the message: "Thank you etc". I would love to stay on the page, and just either get a box pop-up with the success message or just have it on the page. Either is fine :)
The error message doesn't show, it just puts you on a blank page.
The errMessages like "please fill in a valid e-mail" don't show. Probably because I removed the labels, but I would like to show that message IN the box rather than under it.
It's overriding my CSS! But that happens sometimes on my computer, one time I see lines other times I see boxes. So that issue might be somewhere else. However with the 'working form' it changes my last box.
This is what I meant with changing CSS on my boxes
PHP code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'denise#hetfulfilmentbedrijf.nl';
$subject = 'Message from Contact Demo';
$body = "From: $name\n Subject: $subject\n Number: $phone\n E-Mail: $email\n Message:\n $message";
//Check if name has been entered
if(!$_POST['name']) {
$errName = 'Vul alsjeblieft je naam in';
}
//Check if subject has been entered
if(!$_POST['subject']) {
$errName = 'Vul alsjeblieft een onderwerp in';
}
//Check if number has been entered
if(!$_POST['phone']) {
$errName = 'Vul alsjeblieft je nummer in';
}
//Check if e-mail is entered and valid
if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vul alsjeblieft je e-mailadres in';
}
//Check if message has been entered
if(!$_POST['email']) {
$errMessage = 'Laat alsjeblieft een bericht achter';
}
// If no errors, send email
if (!$errName && !$errSubject && !$errPhone && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
echo $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
}
else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
HTML:
<form class="form-horizontal" role="form" method="post" action="partials/contactform.php">
<div class="form-group offset-top-45">
<textarea rows="11" cols="100" class="form-control" id="message" name="message" placeholder="Laat hier je bericht voor ons achter:"></textarea><?php echo htmlspecialchars($_POST['message']);?></textarea>
<span class="help-block" style="display: none;">Laat alsjeblieft een bericht achter</span>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="col-md-6">
<div class="row offset-top-10">
<div class="pull-right">
<img height="60" width="100" src="/images/stamp.png" alt="stamp">
</div>
</div>
<div class="row offset-top-10" style="padding-right:20px; padding-left:10px">
<form role="form" id="feedbackForm">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Hoe heet je?" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft je naam in</span>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Wat is het onderwerp?" value="<?php echo htmlspecialchars($_POST['subject']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft een onderwerpin</span>
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Op welk nummer kunnen wij jou bereiken?" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft je nummer in</span>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Wat is je e-mailadres?">
<span class="help-block" style="display: none;">Vul alsjeblieft je e-mail in</span>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<div class="col-md-12 text-right">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-s">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
</form>
Help is much appreciated!
Full script it should work without any problems
<?php
$result = "";
$errors = ""; //use to count errors
//Error message variables
$errName = "";
$errSubject = "";
$errEmail = "";
$errMessage = "";
//message variables
$name = "";
$subject = "";
$phone = "";
$email = "";
$message = "";
$to = "denise#hetfulfilmentbedrijf.nl";
if (isset($_POST['submit'])) {
//check if name has been entered
if (empty($_POST['name'])) {
$errName = "Vul alsjeblieft je naam in";
$errors++;
} else {
$name = UserInput($_POST['name']);
}
////Check if subject has been entered
if (empty($_POST['subject'])) {
$errSubject = "Vul alsjeblieft een onderwerp in";
$errors++;
} else {
$Subject = UserInput($_POST['subject']);
}
//check if email entered
if (empty($_POST['email'])) {
$errEmail = "Laat alsjeblieft een bericht achter";
$errors++;
} else {
$email = UserInput($_POST['email']);
// check if email is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
$errEmail = "Vul alsjeblieft je e-mailadres in";
$errors++;
}
}
if (empty($_POST['phone'])) {
$errPhone = "Vul alsjeblieft je nummer in";
$errors++;
} else {
$phone = UserInput($_POST['phone']);
// check if email is numbers
if (!is_numeric($phone)) {
$errPhone = "enter numbers only";
$errors++;
}
}
//check message
if (empty($_POST['message'])) {
$errMessage = "Laat alsjeblieft een bericht achter";
} else {
$message = UserInput($_POST['message']);
}
if ($errors > 0) {
// we have errors do not send email
$result = "<div class=\"alert alert-danger\">Sorry there was an error sending your message. Please fix " . $errors . " errors on the form </div>";
} else {
//no errors set email headers and send email
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <' . $email . '>' . "\r\n";
$body = "<p> New email from $name";
$body .= "<p> Phone : $phone</p>";
$body .= "<p> Email : $email<p>";
$body .= "<p>Message : $message</p>";
if (mail($email, $subject, $msg, $header)) {
$result = "<div class=\"alert alert-success\">Thank You! I will be in touch</div>";
$_POST = array(); //clear the form aftter sendig
} else {
$result = "<div class=\"alert alert-danger\">Sorry there was an error sending your message. Please try again later</div>";
}
}
}
//sanitise use input
function UserInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form class="form-horizontal" role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<div class="col-md-12 text-right">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-s">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
<div class="form-group offset-top-45">
<textarea rows="11" cols="100" class="form-control" id="message" name="message" placeholder="Laat hier je bericht voor ons achter:"></textarea><?php if(!empty($_POST['message'])){echo $_POST['message'];}?></textarea>
<?php echo "<p class=\"text-danger\">".$errMessage."</p>";?>
</div>
</div>
<div class="col-md-6">
<div class="row offset-top-10">
<div class="pull-right">
<img height="60" width="100" src="/images/stamp.png" alt="stamp">
</div>
</div>
<div class="row offset-top-10" style="padding-right:20px; padding-left:10px">
<form role="form" id="feedbackForm">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Hoe heet je?" <?php if(!empty($_POST['name'])){echo "value=\"".$_POST['name']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errName."</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Wat is het onderwerp?" <?php if(!empty($_POST['Subject'])){echo "value=\"".$_POST['Subject']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errSubject."</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Op welk nummer kunnen wij jou bereiken?" <?php if(!empty($_POST['phone'])){echo "value=\"".$_POST['phone']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errPhone."</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Wat is je e-mailadres?">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</form>
Let me know if you have any question or need any help.
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>
Hey StackOverflow Community, i have a problem with my Contact Form.
I used this PHP Script:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Kontaktformular';
$to = 'email#mail.com';
$subject = 'Buchungsanfrage - $name';
$body ="From: $name\n: $phone\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Bitte geben Sie den Namen ein.';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Bitte geben Sie eine gültige E-Mail Adresse ein.';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Bitte hinterlassen Sie mir eine Nachricht.';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Ihre Anti-Span Antwort war falsch.';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Danke! Ich werde mich schnellstmöglich bei Ihnen melden.</div>';
} else {
$result='<div class="alert alert-danger">Entschuldigung, beim versenden Ihrer Nachricht ist etwas schief gelaufen. Bitte versuchen Sie es nochmal.</div>';
}
}
}
?>
And this HTML for my Form:
<div class="block block-primary-head no-pad">
<h3><i class="fa fa-pencil"></i> Buchungsanfrage</h3>
<div class="block-content">
<form role="form" method="post" action="fotografie.php">
<div class="form-group">
<label>Name*</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Vor- und Nachname" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label>Telefonnummer</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Ihre Telefonnummer" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
</div>
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label>Nachricht</label>
<textarea class="form-control" id="message" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group">
<label>2 + 3 = ?</label>
<input type="text" class="form-control" id="human" name="human" placeholder="Ihre Antwort">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<?php echo $result; ?>
</div>
<button type="submit" id="submit" name="submit" value="send" class="btn btn-primary">Absenden</button>
</form>
</div>
But when i go on my side now, i got the following error:
Notice: Undefined index: submit in ..\htdocs\projects\bootstrap-themes\001\contact\form-fotografie.php on line 2
Line 2 is following code:
if ($_POST["submit"]) {
What could be the problem?
Thanks for your help :-)
It looks like you are trying to check if the form is submitted. You need to use the isset function for this as below
Line 2:
if ($_POST["submit"]) {
Should read:
if (isset($_POST["submit"])) {
EDIT:
You should always check if the variable is set before using it so to correct your html you need to change the following:
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
To:
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php if(isset($_POST['email'])) { echo htmlspecialchars($_POST['email']); } ?>">
<?php if(isset($errEmail) && ($errEmail) != "") { echo "<p class='text-danger'>$errEmail</p>"; } ?>
</div>
You need to make this change for all of the form-groups with the relevant variable names, let me know how you get on
i have a problem with the validation form. It does not work if i put "required", example:
controller:
public function updateBenefit(){
$result = array();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('beneficio', 'Nombre del Beneficio', 'required|alpha');
$this->form_validation->set_rules('info', 'Info', 'alpha');
$this->form_validation->set_rules('descrip', 'Descripción', 'alpha');
$this->form_validation->set_rules('orden', 'Orden', 'integer');
// $this->form_validation->set_rules('fecha', 'Fecha', 'date_valid');
$this->form_validation->set_message('required', 'El campo %s es requerido');
if ($this->form_validation->run() == TRUE){
if (isset($_POST['id'])){
$idb = $_POST['id'];
$benefit = BeneficiosManager::getInstance()->getHome($idb);
$result['message'] = "Se ha modificado el Beneficio con éxito";
} else{
$benefit = BeneficiosManager::getInstance()->create();
$result['message'] = "Se ha cargado el Beneficio con éxito";
}
$benefit->nombre = ucfirst(strtolower($_POST['beneficio']));
$benefit->content = ucfirst(strtolower($_POST['descrip']));
$benefit->intro = ucfirst(strtolower($_POST['info']));
$benefit->active = $_POST['optionsRadios2'];
$benefit->orden = $_POST['orden'];
// $benefit->date = $_POST['fecha'];
BeneficiosManager::getInstance()->save($benefit);
}else{
//no se validaron los datos ingresados
$result['message'] = "Error validación";
}
echo json_encode($result);
}
view:
{extends file='admin/base/base.tpl'}
{block name='content'}
<h3>Cargar Beneficio </h3>
</br>
<form action="{site_url()}admin/updateBenefit" class="form-horizontal" method="post" id="" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label">Beneficio</label>
<div class="controls">
<input type="text" name="beneficio" value="" class="m-wrap medium" />
<span class="help-inline">Nombre del Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Info</label>
<div class="controls">
<textarea name="info" class="medium m-wrap" rows="3"></textarea>
<span class="help-inline">Información Clave</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Descripción</label>
<div class="controls">
<textarea name="descrip" class="large m-wrap" rows="3"></textarea>
<span class="help-inline">Descripción del Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Activo</label>
<div class="controls">
<label class="radio line">
<input type="radio" name="optionsRadios2" value="1"/>Si</input>
</label>
<label class="radio line">
<input type="radio" name="optionsRadios2" value="0"/>No</input>
</label>
<span class="help-inline">Ofrecer Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Orden</label>
<div class="controls">
<input type="text" name="orden" value="" class="m-wrap small" />
<span class="help-inline">Prioridad del Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Fecha</label>
<div class="controls">
<input type="text" name="fecha" value="{$smarty.now|date_format}" class="m-wrap medium" />
<span class="help-inline"></span>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn blue"><i class="icon-ok"></i> Guardar</button>
<button type="button" class="btn">Cancelar</button>
</div>
</form>
{/block}
what might the problem be?
if i remove the "required" field, the form validates... but if i put it, it doesn't... i don't know what else to try, can't understand why this is happening
the code is working now, thanks to #Jonathan that corrected me, and i was also making an imput mistake when i was trying this method. I was trying to put two words in the same field (i.e: bon vivir); so the correct input would be: bonvivir.
I'm not sure you are using "title" as the name of your real title input element. Because I found you use this code to assign your title.
$benefit->title = ucfirst(strtolower(trim($_POST['beneficio'])));
So you may want to try to use
$this->form_validation->set_rules('beneficio', 'Nombre del Beneficio', 'required|alpha');
instead.
I am not sure I had the right guess. Just give it a go. Hope this helps.