PHP multiple fields contact form issue - php

I've got a problem with a PHP contact form (yes, classic issue I know).
I'm pretty new in PHP and I don't find what is broken in my code.
Here is the PHP:
<?php
// Email address verification
function isEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'contact#must-assurances.com';
$clientEmail = addslashes(trim($_POST['email']));
$name = addslashes(trim($_POST['name']));
$entreprise = addslashes(trim($_POST['entreprise']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$jesuis = addslashes(trim($_POST['jesuis']));
$tel = addslashes(trim($_POST['tel']));
$siren = addslashes(trim($_POST['siren']));
$array = array('emailMessage' => '', 'entrepriseMessage' => '', 'messageMessage' => '', 'antispamMessage' => '', 'jesuisMessage' =>'', 'nameMessage' =>'', 'telMessage' =>'', 'sirenMessage' =>'' );
if($siren == '') {
$array['sirenMessage'] = 'SIREN/SIRET invalide';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Email invalide';
}
if($name == '') {
$array['nameMessage'] = 'Nom manquant';
}
if($entreprise == '') {
$array['entrepriseMessage'] = 'Entreprise manquante';
}
if($tel == '') {
$array['telMessage'] = 'Numéro de téléphone manquant';
}
if($siren == '') {
$array['sirenMessage'] = 'SIREN/SIRET invalide';
}
if($message == '') {
$array['messageMessage'] = 'Message manquant';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Mauvaise réponse';
}
if($jesuis == '') {
$array['jesuisMessage'] = 'Merci de préciser si vous êtes une entreprise, un courtier, un investisseur, un professionnel ou autre';
}
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail .
$headers = array("Content-Type: text/html; charset=UTF-8");
mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers);
}
echo json_encode($array);
}
?>
And here the HTML snippet:
<form role="form" action="contact-mia.php" method="post" accept-charset='UTF-8'>
<div class="form-group col-md-3">
<label for="name">Votre nom :</label>
<input type="text" name="name" placeholder="Votre nom..." class="contact-name form-control" id="name">
</div>
<div class="form-group col-md-3">
<label for="email">Votre adresse e-mail :</label>
<input type="text" name="email" placeholder="Email..." class="contact-email form-control" id="email">
</div>
<div class="form-group col-md-3">
<label for="name">Votre numéro de téléphone :</label>
<input type="text" name="tel" placeholder="Numéro de téléphone..." class="contact-tel form-control" id="tel">
</div>
<div class="form-group col-md-3">
<label for="entreprise">Entreprise :</label>
<input type="text" name="entreprise" placeholder="Entreprise..." class="contact-entreprise form-control" id="entreprise">
</div>
<div class="col-md-6 form-group">
<label for="jesuis">Je suis : </label>
<select class="form-control" name="jesuis" id="jesuis">
<option value="">Selectionnez...</option>
<option value="entreprise">Une entreprise</option>
<option value="courtier">Un courtier</option>
<option value="investisseur">Un investisseur</option>
<option value="professionnel">Un professionnel</option>
<option value="autre">Autre</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="entreprise">Numéro de SIREN/SIRET :</label>
<input type="text" name="siren" placeholder="SIREN/SIRET..." class="contact-siren form-control" id="siren">
</div>
<div class="col-md-12 form-group">
<label for="message">Message :</label>
<textarea name="message" placeholder="Message..." class="contact-message form-control" id="message"></textarea>
</div>
<div class="col-md-12 form-group">
<label for="antispam">Question Antispam : 7 + 5 = ?</label>
<input type="text" name="antispam" placeholder="Votre réponse..." class="contact-antispam form-control" id="antispam">
</div>
<div class="col-md-6 col-md-offset-3 form-group centered">
<button type="submit" class="btn">Envoyer</button>
</div>
</form>
Another issue about my $message syntax, here is the code:
$message = "Je suis un(e) ".$jesuis.'<br />'"Nom : ".$name.'<br />'"Telephone : ".$tel.'<br />'"SIREN : ".$siren.'<br />'"Entreprise : ".$entreprise.'<br />' "Message : ".$message;
Probably something wrong with the ' and " ?
Do you have any ideas?

Your PHP side has an error on the IF statement:
Take a further look at the lines containing: $headers
Your code:
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail .
$headers = array("Content-Type: text/html; charset=UTF-8");
mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers);
}
Corrected Code:
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">\r\n";
$headers.= "Reply-To: " . $clientEmail . "\r\n";
$headers.= "Content-Type: text/html; charset=UTF-8\r\n";
// Wrap mail function in if statement to check if it worked
if (#mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers)) {
echo 'Successfully sent email';
} else {
echo 'Failed to send';
}
}
To help debug your code ensure your PHP errors are enabled, you can do this at the top of your PHP code using the following:
error_reporting(E_ALL);
ini_set('display_errors', 'On');

I've got a new issue about my $message syntax.
Here is the code :
$message = "Je suis un(e) ".$jesuis.'<br />'"Nom : ".$name.'<br />'"Telephone : ".$tel.'<br />'"SIREN : ".$siren.'<br />'"Entreprise : ".$entreprise.'<br />' "Message : ".$message;
Probably something about the ' or " ?

Related

Form redirection on configuration PHP page

I have a completely functional Form that sends mail as it should and all BUT for some reason, when I (try to) submit my message, instead of showing the success or error alert under the form, it opens the sendmail.php page with the messages, without any CSS... It's been 4 days and I'm going crazy. I'm sure it's actually a stupid typo or something but I can't find it, please help.
Screen of the "Success/Alert" display
Here's my sendmail.php:
<?php
$sendto = 'my#email.com';
$subject = 'Message de votre site';
$errormessage = 'Ah, vous avez oublié quelques informations. Réessayez ? ';
$thanks = "Merci pour votre message ! On vous répond aussi vite que possible. ";
$honeypot = "Vous êtes tombé dans le piège ! Si vous êtes humain.e, recommencez ! ";
$emptyname = 'Quel est votre nom ? ';
$emptyemail = 'Quelle est votre adresse mail ? ';
$emptymessage = 'Et votre message ? ';
$alertname = 'Utilisez uniquement un alphabet standard. ';
$alertemail = 'Entrez ce format de mail : <i>name#example.com</i>. ';
$alertmessage = "Vérifiez que vous n 'avez utilisé aucun caractère spécial. ";
$alert = '';
$pass = 0;
function clean_var($variable) {
$variable = strip_tags(stripslashes(trim(rtrim($variable))));
return $variable;
}
if ( empty($_REQUEST['last']) ) {
if ( empty($_REQUEST['name']) ) {
$pass = 1;
$alert .= "<li>" . $emptyname . "</li>";
$alert .= "<script>jQuery(\"#name\").addClass(\"error\");</script>";
} elseif ( preg_match( "/[][{}()*+?.\\^$|]/", $_REQUEST['name'] ) ) {
$pass = 1;
$alert .= "<li>" . $alertname . "</li>";
}
if ( empty($_REQUEST['email']) ) {
$pass = 1;
$alert .= "<li>" . $emptyemail . "</li>";
$alert .= "<script>jQuery(\"#email\").addClass(\"error\");</script>";
} elseif ( !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_REQUEST['email']) ) {
$pass = 1;
$alert .= "<li>" . $alertemail . "</li>";
}
if ( empty($_REQUEST['message']) ) {
$pass = 1;
$alert .= "<li>" . $emptymessage . "</li>";
$alert .= "<script>jQuery(\"#message\").addClass(\"error\");</script>";
} elseif ( preg_match( "/[][{}()*+?\\^$|]/", $_REQUEST['message'] ) ) {
$pass = 1;
$alert .= "<li>" . $alertmessage . "</li>";
}
if ( $pass==1 ) {
echo "<script>$(\".result\").toggle();$(\".result\").toggle().hide(\"fast\").show(\"fast\"); </script>";
echo "<script>$(\".result .alert\").addClass('alert-danger').removeClass('alert-success'); </script>";
echo $errormessage;
echo $alert;
} elseif (isset($_REQUEST['message'])) {
$message = "From: " . clean_var($_REQUEST['name']) . "\n";
$message .= "Email: " . clean_var($_REQUEST['email']) . "\n";
$message .= "Message: \n" . clean_var($_REQUEST['message']);
$header = 'From:'. clean_var($_REQUEST['email']);
mail($sendto, $subject, $message, $header);
echo "<script>$(\".result\").toggle();$(\".result\").toggle().hide(\"fast\").show(\"fast\");$('#contactForm')[0].reset();</script>";
echo "<script>$(\".result .alert\").addClass('alert-success').removeClass('alert-danger'); </script>";
echo $thanks;
echo "<script>jQuery(\"#name\").removeClass(\"error\");jQuery(\"#email\").removeClass(\"error\");jQuery(\"#message\").removeClass(\"error\");</script>";
echo "<script>$(\".result .alert\").delay(4000).hide(\"fast\");</script>";
die();
echo "<br/><br/>" . $message;
}
} else {
echo "<script>$(\".result\").toggle();$(\".result\").toggle().hide(\"fast\").show(\"fast\");</script>";
echo $honeypot;
}
?>
And HTML:
<form class="form-inline flowuplabels" role="form" method="post" autocomplete="off" id="contactForm" action="js/sendmail.php">
<div class="form-group fl_wrap">
<label class="fl_label" for="name">Nom :</label>
<input type="text" name="name" value="" id="name" class="form-control fl_input" required>
</div>
<div class="form-group fl_wrap">
<label class="fl_label" for="email">Email :</label>
<input type="text" name="email" value="" id="email" class="form-control fl_input" required>
</div>
<span class="form-group fl_wrap honeypot">
<label class="fl_label" for="last">Honeypot:</label>
<input type="text" name="last" value="" id="last" class="form-control fl_input">
</span>
<div class="form-group fl_wrap">
<label class="fl_label" for="message">Message :</label>
<textarea type="text" name="message" value="" id="message" class="materialize-textarea" required></textarea>
</div>
<div class="form-group">
<button type="submit" value="Send" id="submit" class="btn btn-block">Envoyer</button>
</div>
<div id="form-alert" class="form-group">
<div class="result">
<div class="alert"></div>
</div>
</div>
</form>
As some asked. The HTML is on the index page and the sendmail.php is an independent page.
And this is where the success/alert message is supposed to appear :
Form in website

post 405 method not allowed php forn

I have a simple form for a website in HTML and PHP query.
HTML form
<form name="contactForm" id="contactForm" method="post" action="" >
<fieldset>
<div class="group">
<input name="contactName" type="text" id="contactName" placeholder="Name" value="" minLength="2" required />
</div>
<div>
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required />
</div>
<div>
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" />
</div>
<div>
<textarea name="contactMessage" id="contactMessage" placeholder="message" rows="10" cols="50" required ></textarea>
</div>
<div>
<button class="submitform contactdetails1">Submit</button>
<div id="submit-loader">
<div class="text-loader contactdetails2">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
After trying to submit the form, I have the error messages that POST 405 NOT ALLOWED. It is unclear from the documentation how to set up a server and fix POST request.
PHP query
<?php
// Replace this with your own email address
$siteOwnersEmail = 'jeremie.xxxxx#xxxxx.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
...................................................................

Contact Form without ajax just php and html page

I have a simple Contact Form
it works very freat but I want the result on the same page in a small box
just under the form
where the fail messages are coming and success too
<form method="post" action="contact.php" name="contactform" id="contactform">
<div class="one_half">
<input name="name" type="text" id="name" size="30" onfocus="if(this.value == 'Name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Name'; }" value="Name">
<input name="alter" type="text" id="alter" size="3" onfocus="if(this.value == 'Alter') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Alter'; }" value="Alter">
<input name="email" type="text" id="email" size="30" onfocus="if(this.value == 'E-Mail') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'E-Mail'; }" value="E-Mail">
<input name="phone" type="text" id="phone" size="30" onfocus="if(this.value == 'Handynummer') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Handynummer'; }" value="Handynummer">
<input name="facebook" type="text" id="facebook" size="200" onfocus="if(this.value == 'Facebook') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Facebook'; }" value="Facebook">
<input name="instagram" type="text" id="instagram" size="200" onfocus="if(this.value == 'Instagram') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Instagram'; }" value="Instagram">
</div>
<div class="one_half last">
<textarea name="comments" cols="40" rows="3" id="comments" onfocus="if(this.value == 'Nachricht') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Nachricht'; }">Nachricht</textarea>
</div>
<input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
and here the PHP file
<?php
if (!$_POST) exit;
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$alter = $_POST['alter'];
$facebook = $_POST['facebook'];
$instagram = $_POST['instagram'];
if (get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx#xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if (mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Bewerbung erfolgreich</h1>";
echo "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'FEHLER!';
}
It works PERFECT
but it's always getting on a new site, no matter if error or success.
I want to add a small block under the form where all results are displayed
I hope u can help me.
I don't want ajax or something else. I just want it added like it is.
A solution for your problem might be to have in the same PHP file the logic with the form rendering. Doing this, the form action will be the same PHP file, so it will load the PHP code before rendering the form view. By that, you'll be able to render bellow the form whatever you want to according with the output from the sending email.
For example, take a closer look at the $mailResult variable:
<?php
$name = $_POST['name'] ?? null;
$email = $_POST['email'] ?? null;
$phone = $_POST['phone'] ?? null;
$comments = $_POST['comments'] ?? null;
$alter = $_POST['alter'] ?? null;
$facebook = $_POST['facebook'] ?? null;
$instagram = $_POST['instagram'] ?? null;
if (get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx#xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
$mailResult = '';
if (isset($name, $email)) {
if (mail($address, $e_subject, $msg, $headers)) {
$mailResult = "<fieldset>";
$mailResult .= "<div id='success_page'>";
$mailResult .= "<h1>Bewerbung erfolgreich</h1>";
$mailResult .= "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
$mailResult .= "</div>";
$mailResult .= "</fieldset>";
} else {
$mailResult .= 'FEHLER!';
}
}
?>
<html lang="en">
<head>
<title>Title page!</title>
</head>
<body>
<form method="post" name="contactform" id="contactform">
<div class="one_half">
<input name="name" type="text" id="name" size="30"
onfocus="if(this.value === 'Name') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Name'; }"
value="Name">
<input name="alter" type="text" id="alter" size="3"
onfocus="if(this.value === 'Alter') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Alter'; }"
value="Alter">
<input name="email" type="text" id="email" size="30"
onfocus="if(this.value === 'E-Mail') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'E-Mail'; }"
value="E-Mail">
<input name="phone" type="text" id="phone" size="30"
onfocus="if(this.value === 'Handynummer') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Handynummer'; }"
value="Handynummer">
<input name="facebook" type="text" id="facebook" size="200"
onfocus="if(this.value === 'Facebook') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Facebook'; }"
value="Facebook">
<input name="instagram" type="text" id="instagram" size="200"
onfocus="if(this.value === 'Instagram') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Instagram'; }"
value="Instagram">
</div>
<div class="one_half last">
<textarea name="comments" cols="40" rows="3" id="comments"
onfocus="if(this.value === 'Nachricht') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Nachricht'; }">
Nachricht
</textarea>
</div>
<input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
<?php echo $mailResult ?>
</body>
</html>
I would anyway avoid this kind of solution. I would approach this problem with AJAX.
In these solutions, we are mixing the logic with the rendering.

Email php script won't send emails [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am having trouble with getting the mail php script to work, I probably dont understand why because I already looked it up on the internet, but I do not find a correct solution ( kinda new to this )
If I run the script it is always entering to the last if then else with something went wrong please try again.
Here are the scripts:
<?php
$siteOwnersEmail = 'test#gmail.be';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (isset($error)) {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
else {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
}
}
?>
And last but not least the form for submitting
<form name="contactForm" id="contactForm" action="inc/sendEmail.php" method="post" >
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Naam" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Onderwerp" value="" class="full-width">
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage" placeholder="Uw vraag" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
</div>
<div class="form-field">
<button class="full-width btn--primary">Submit</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form>
Use this code to send mail it working
<?php
$malil = 'example#example.com';
$to = $malil;
$subject = "Your order has been placed.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
$headers .= "From: example#example.com" . "\r\n" ;
/* $headers .= "Cc:reservations#bookingsmaker.com,bookingsmakerdotcom#gmail.com,bookingsmakerworld#gmail.com "."\r\n";
*/
$headers .="Cc:example#example.com,example#example.in";
////////////////***********Html format*****************//////////////////
$htmlContent = 'Content';
$result=mail($to,$subject,$htmlContent,$headers);
if($result){
echo 'mail Send';
}else{
echo 'Mail not send';
}
?>

PHP to handle mail form from Skeleton CSS

I am a designer trying to get a PHP to email script working.
The HTML form is the standard Skeleton CSS one from their website at
http://getskeleton.com/#forms:
<form>
<div class="row">
<div class="six columns">
<label for="exampleEmailInput">Your email</label>
<input class="u-full-width" placeholder="test#mailbox.com" id="exampleEmailInput" type="email">
</div>
<div class="six columns">
<label for="exampleRecipientInput">Reason for contacting</label>
<select class="u-full-width" id="exampleRecipientInput">
<option value="Option 1">Questions</option>
<option value="Option 2">Admiration</option>
<option value="Option 3">Can I get your number?</option>
</select>
</div>
</div>
<label for="exampleMessage">Message</label>
<textarea class="u-full-width" placeholder="Hi Dave …" id="exampleMessage"></textarea>
<label class="example-send-yourself-copy">
<input type="checkbox">
<span class="label-body">Send a copy to yourself</span>
</label>
<input class="button-primary" value="Submit" type="submit">
</form>
I am trying to amend the existing PHP script to match this and having an awful lot of trouble. This is my base plate for the PHP:
<?php
if($_REQUEST['name'] == '' || $_REQUEST['email'] == '' || $_REQUEST['message'] == ''):
return "error";
endif;
if (filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)):
$to = 'email#myemail.com';
$header = 'From: '. $_REQUEST['name'] . ' <'. $_REQUEST['email'] .'>'. "\r\n";
$header .= 'Reply-To: '. $_REQUEST['name'] . ' <'. $_REQUEST['email'] .'>'. "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion();
$subject = "Hello";
$message .= 'Name: ' . $_REQUEST['name'] . "\n";
$message .= 'Email: ' . $_REQUEST['email'] . "\n";
$message .= 'Message: '. $_REQUEST['message'];
$event = $_GET['exampleRecipientInput']
$mail = mail( $to, $url , $message, $header );
return $mail ? "success" : "error";
else:
return "error";
endif;
?>
I cannot understand how to adapt the PHP which is probably easy for experienced devs but fairly impenetrable for designers. Can anyone help?
As ceejayoz said, you're going to need to add name attributes to the input fields, then you'll be able to access them via $_POST['whatever_name']
Your HTML code could look like the following (added name attributes):
<form>
<div class="row">
<div class="six columns">
<label for="exampleEmailInput">Your email</label>
<input class="u-full-width" placeholder="test#mailbox.com" id="exampleEmailInput" type="email" name="email">
</div>
<div class="six columns">
<label for="exampleRecipientInput">Reason for contacting</label>
<select class="u-full-width" id="exampleRecipientInput" name="reason">
<option value="Option 1">Questions</option>
<option value="Option 2">Admiration</option>
<option value="Option 3">Can I get your number?</option>
</select>
</div>
</div>
<label for="exampleMessage">Message</label>
<textarea class="u-full-width" placeholder="Hi Dave …" id="exampleMessage" name="message"></textarea>
<label class="example-send-yourself-copy">
<input type="checkbox">
<span class="label-body">Send a copy to yourself</span>
</label>
<input class="button-primary" value="Submit" type="submit">
</form>
You need to ensure you're checking if the submit button has been, in fact, submitted:
<?php
if (isset($_POST['submit']) {
if($_POST['name'] == '' || $_POST['email'] == '' || $_POST['message'] == ''):
return "error";
endif;
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)):
$to = 'email#myemail.com';
$header = 'From: '. $_POST['name'] . ' <'. $_POST['email'] .'>'. "\r\n";
$header .= 'Reply-To: '. $_POST['name'] . ' <'. $_POST['email'] .'>'. "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion();
$subject = "Hello";
$message .= 'Name: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'Message: '. $_POST['message'];
$event = $_POST['reason'];
$mail = mail( $to, $url , $message, $header );
return $mail ? "success" : "error";
else:
return "error";
endif;
}
?>
The if (isset($_POST['submit'])) portion will let the script know that the form has been submitted, and will run the code inside of the brackets.
Note: I changed the $_REQUEST[''] to $_POST['name']. These will be associated to the field names I've provided at the top of the HTML edit.

Categories