I'm trying to create a form without a real submit button, but with a styled a href and javascript, but I'm unable to POST the form itself.
If I do var_dump($_POST); then I get an array of 4 items (3 inputs and 1 text area). The form itself is not there, so if (isset($_POST['submit'])) valuates to false. I also tried if (isset($_POST['ContactForm'])) but its not working either
form code:
<form name='ContactForm' id='ContactForm' action='verzendvraag.php' method='POST'>
<input type="text" class="col-md-6 col-xs-12 name" id="naam" name='naam' placeholder='Naam *' value="<? echo $_SESSION['naam']; ?>" required/>
<input type="email" class="col-md-6 col-xs-12 Email" id="email" name='email' placeholder='Email *' value="<? echo $_SESSION['email']; ?>" required/>
<input type="text" class="col-md-12 col-xs-12 Subject" name='Subject' id='Subject' placeholder='Onderwerp'/>
<textarea type="text" class="col-md-12 col-xs-12 Message" id="vraag" name="vraag" placeholder='Bericht *' style="left: 0px; top: 0px" required><? echo $_SESSION['vraag']; ?></textarea>
<div class="cBtn col-xs-12">
<ul>
<li class="clear"><i class="fa fa-times"></i>ledig vakjes</li>
<li class="send"><i class="fa fa-share"></i>Verstuur bericht</li>
</ul>
</div>
</form>
javascript:
function submitForm(e) {
if (validateForm())
{
document.getElementById("ContactForm").submit();
}
e.preventDefault();
}
php:
session_start();
//Validatie serverside => bescherming inbouwen voor als iemand zijn javascript uitschakelt. Javascript echter wel nodig om de server en netwerktraffic te ontlasten.
if (isset($_POST['submit']))
{
$naam = $_POST['naam'];
//$bedrijf = $_POST['bedrijf'];
$subject = $_POST['Subject'];
$email = $_POST['email'];
$vraag = $_POST['vraag'];
//In session steken zodat we bij een eventuele terugkeer naar de form de reeds ingevulde gegevens kunnen terug zetten.
$_SESSION['naam'] = $_POST['naam'];
//$_SESSION['bedrijf'] = $_POST['bedrijf'];
$_SESSION['Subject'] = $_POST['Subject'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['vraag'] = $_POST['vraag'];
$isOK = true;
$error = '0';
$atpos = strrpos($email,"#");
$dotpos = strrpos($email,".");
if(is_null($naam) || $naam == '')
{
$isOK = false;
echo('NAAM NIET OK ');
$error .= ',1';
}
if(is_null($email) || $email == '')
{
$isOK = false;
echo('EMAIL NIET OK '.$email);
$error .= ',2';
}
else
{
//checken geldig e-mailadres
if ($atpos === false || $dotpos === false || $dotpos < $atpos+2 || $dotpos+2 >= strlen($email)) //=== moet => http://php.net/manual/en/function.strpos.php en http://www.php.net/manual/en/language.operators.comparison.php
{
$isOK = false;
echo("Ongeldig e-mailadres ");
$error .= ',3';
}
}
if(is_null($vraag) || $vraag== '')
{
$isOK = false;
echo('VRAAG NIET OK ');
$error .= ',4';
}
}
if(!($isOK))
{
echo("<script>location.href='index.php?error=$error'</script>");
exit;
}
else
{
.....
}
I checked Javascript Submit is not Sending POST to php file and he says his code works, so I don't understand mine won't.
Since you don't have any button or input named submit so it does not exist in $_POST variable. I think you should check what you have in the form so use .
if (isset($_POST['naam'])){
// your code
}
Instead of
if (isset($_POST['submit'])){
// your code
}
you should also change this if (isset($_POST['submit'])) to if (isset($_POST['naam']))
Use ajax request like this
$.ajax({//create an ajax request to load_page.php
type: "POST",
url: "receive.php",
data: $("#ContactForm").serialize(),
success: function(response) {
if (response) {
alert('Successfully posted.');
}
else {
alert('Successfully not posted.');
}
}
});
Just use ajax for submiting form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<form name='ContactForm' id='ContactForm' action='verzendvraag.php' method='POST'>
<input type="text" class="col-md-6 col-xs-12 name" id="naam" name='naam' placeholder='Naam *' value="<? echo $_SESSION['naam']; ?>" required/>
<input type="email" class="col-md-6 col-xs-12 Email" id="email" name='email' placeholder='Email *' value="<? echo $_SESSION['email']; ?>" required/>
<input type="text" class="col-md-12 col-xs-12 Subject" name='Subject' id='Subject' placeholder='Onderwerp'/>
<textarea type="text" class="col-md-12 col-xs-12 Message" id="vraag" name="vraag" placeholder='Bericht *' style="left: 0px; top: 0px" required><? echo $_SESSION['vraag']; ?></textarea>
<div class="cBtn col-xs-12">
<ul>
<li class="clear"><i class="fa fa-times"></i>ledig vakjes</li>
<li class="send"><i class="fa fa-share"></i>Verstuur bericht</li>
</ul>
</div>
</form>
<script>
function submitForm(e) {
alert('working');
var naam = $("#naam").val();
var email = $("#email").val();
var Subject = $("#Subject").val();
$.ajax({//create an ajax request to load_page.php
type: "POST",
url: "verzendvraag.php",
data: {"naam":naam,"email":email,"Subject":Subject},
success: function(response) {
if (response) {
alert(response);
alert('Successfully posted.');
}
else {
alert('Successfully not posted.');
}
}
});
}
</script>
</body>
</html>
verzendvraag.php
<?php session_start();
//Validatie serverside => bescherming inbouwen voor als iemand zijn javascript uitschakelt. Javascript echter wel nodig om de server en netwerktraffic te ontlasten.
if (isset($_POST))
{
$naam = $_POST['naam'];
//$bedrijf = $_POST['bedrijf'];
$subject = $_POST['Subject'];
$email = $_POST['email'];
//$vraag = $_POST['vraag'];
//In session steken zodat we bij een eventuele terugkeer naar de form de reeds ingevulde gegevens kunnen terug zetten.
$_SESSION['naam'] = $_POST['naam'];
//$_SESSION['bedrijf'] = $_POST['bedrijf'];
$_SESSION['Subject'] = $_POST['Subject'];
$_SESSION['email'] = $_POST['email'];
//$_SESSION['vraag'] = $_POST['vraag'];
$isOK = true;
$error = '0';
$atpos = strrpos($email,"#");
$dotpos = strrpos($email,".");
if(is_null($naam) || $naam == '')
{
$isOK = false;
echo('NAAM NIET OK ');
$error .= ',1';
}
if(is_null($email) || $email == '')
{
$isOK = false;
echo('EMAIL NIET OK '.$email);
$error .= ',2';
}
else
{
//checken geldig e-mailadres
if ($atpos === false || $dotpos === false || $dotpos < $atpos+2 || $dotpos+2 >= strlen($email)) //=== moet => http://php.net/manual/en/function.strpos.php en http://www.php.net/manual/en/language.operators.comparison.php
{
$isOK = false;
echo("Ongeldig e-mailadres ");
$error .= ',3';
}
}
/* if(is_null($vraag) || $vraag== '')
{
$isOK = false;
echo('VRAAG NIET OK ');
$error .= ',4';
}*/
}
if(!($isOK))
{
echo("<script>location.href='index.php?error=$error'</script>");
exit;
}
else
{
echo "SUCCESS";
}
?>
Related
Hello i am trying to do a contact form that gives me an alert box when i press the submit button (e.g. one alert box for "form could not be submitted" and one for "form was successfully submitted")
How can i do this? Does it work with if/else ?
I worked with html/php/js for the form, below is my code (sorry for german)
$anrede = $vorname = $nachname = $anschrift = $postleitzahl = $stadt = $email = $telefon = $nachricht = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["anrede"])) {
$anredeErr = "* Anrede wird benötigt";
} else {
$anrede = test_input($_POST["anrede"]);
}
if (empty($_POST["vorname"])) {
$vornameErr = "* Vorname wird benötigt";
} else {
$vorname = test_input($_POST["vorname"]);
if (!preg_match("/^[a-zA-Z]*$/",$vorname)) {
$vornameErr = "* Nur Buchstaben erlaubt";
}
}
if (empty($_POST["nachname"])) {
$nachnameErr = "* Nachname wird benötigt";
} else {
$nachname = test_input($_POST["nachname"]);
if (!preg_match("/^[a-zA-Z]*$/",$nachname)) {
$nachnameErr = "* Nur Buchstaben erlaubt";
}
}
if (empty($_POST["anschrift"])) {
$anschriftErr = "* Anschrift wird benötigt";
} else {
$anschrift = test_input($_POST["anschrift"]);
}
if (empty($_POST["postleitzahl"])) {
$postleitzahlErr = "* Postleitzahl wird benötigt";
} else {
$postleitzahl = test_input($_POST["postleitzahl"]);
if (!preg_match("/^[0-9]*$/",$postleitzahl)) {
$postleitzahlErr = "* Nur Zahlen erlaubt";
}
}
if (empty($_POST["stadt"])) {
$stadtErr = "* Stadt / Ort wird benötigt";
} else {
$stadt = test_input($_POST["stadt"]);
if (!preg_match("/^[a-zA-Z-' ]*$/",$stadt)) {
$stadtErr = "* Nur Buchstaben und Leerzeichen erlaubt";
}
}
if (empty($_POST["email"])) {
$emailErr = "* E-Mail wird benötigt";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "* Falsches E-Mail Format";
}
}
if (empty($_POST["telefon"])) {
$telefon = "";
} else {
$telefon = test_input($_POST["telefon"]);
if (!preg_match("/^[0-9]*$/",$telefon)) {
$telefonErr = "* Nur Zahlen erlaubt";
}
}
if (empty($_POST["nachricht"])) {
$nachrichtErr = "Nachricht wird benötigt";
} else {
$nachricht = test_input($_POST["nachricht"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="kontaktformular-container">
<h2 class="h2-body3">Schreiben Sie uns eine E-Mail.<br></h2>
<h1 class="h1-body3">Kontaktformular</h1>
<form class="kontaktformular" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="anrede" name="anrede" placeholder="Anrede *" value="<?php echo $anrede ?>">
<span class="error"><?php echo $anredeErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="vorname" name="vorname" placeholder="Vorname *" value="<?php echo $vorname ?>">
<span class="error"><?php echo $vornameErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="nachname" name="nachname" placeholder="Nachname *" value="<?php echo $nachname ?>">
<span class="error"><?php echo $nachnameErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="anschrift" name="anschrift" placeholder="Anschrift *" value="<?php echo $anschrift ?>">
<span class="error"><?php echo $anschriftErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="postleitzahl" name="postleitzahl" placeholder="Postleitzahl *" value="<?php echo $postleitzahl ?>">
<span class="error"><?php echo $postleitzahlErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="stadt" name="stadt" placeholder="Stadt / Ort *" value="<?php echo $stadt ?>">
<span class="error"><?php echo $stadtErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="email" name="email" placeholder="E-Mail-Adresse *" value="<?php echo $email ?>">
<span class="error"><?php echo $emailErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="telefon" name="telefon" placeholder="Telefon" value="<?php echo $telefon ?>">
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<textarea class="form-input" id="nachricht" name="nachricht" rows="10" placeholder="Ihre Nachricht *"><?php echo $nachricht ?></textarea>
<span class="error"><?php echo $nachrichtErr;?></span>
<br>
</div>
<br>
<p class="p-kontakt">Mit einem * markierte Felder sind Pflichtfelder.</p>
<br>
<p class="p-kontakt">Mit dem Absenden des Formulares akzeptieren Sie die <span class="datenschutz"><a class="a-kontakt" href="#">Bestimmungen zum<br>Datenschutz</a></span> und willigen ein von uns per E-Mail/Telefon kontaktiert zu werden.</p>
<br>
<input class="form-submit" onclick="submitted()" type="submit" name="anfrage absenden" value="Anfrage absenden">
<script>
function submitted() {
alert("Nachricht erfolgreich abgeschickt!");
}
</script>```
I can't add a comment because I'm new and have no reputation
If this is the solution or not, please let me know.
At first give form id='contact'
then give submit input id='anfrage'
This code will check the entries if they are empty or not and will give an alert for each entry.. In the end it will give a warning that the command has been completed successfully and will send the forms.
js code:
<script>
if($('#contact').length){
$('#anfrage').click(function(){
var o = new Object();
var form = '#contact';
var anrede = $('#contact #anrede').val();
var vorname = $('#contact #vorname').val();
var nachname = $('#contact #nachname').val();
var anschrift = $('#contact #anschrift').val();
var postleitzahl = $('#contact #postleitzahl').val();
var stadt = $('#contact #stadt').val();
var email = $('#contact #email').val();
var telefon = $('#contact #telefon').val();
var nachricht = $('#contact #nachricht').val();
if(anrede == '')
{
alert("Nachricht erfolgreich abgeschickt1!");
return false;
}
else if(vorname == '')
{
alert("Nachricht erfolgreich abgeschickt2!");
return false;
}
else if(nachname == '')
{
alert("Nachricht erfolgreich abgeschickt3!");
return false;
}
else if(anschrift == '')
{
alert("Nachricht erfolgreich abgeschickt4!");
return false;
}
else if(postleitzahl == '')
{
alert("Nachricht erfolgreich abgeschickt5!");
return false;
}
else if(stadt == '')
{
alert("Nachricht erfolgreich abgeschickt!");
return false;
}
else if(email == '')
{
alert("Nachricht erfolgreich abgeschickt!");
return false;
}
else if(telefon == '')
{
alert("Nachricht erfolgreich abgeschickt!");
return false;
}
else if(nachricht == '')
{
alert("Nachricht erfolgreich abgeschickt!");
return false;
}
else{
alert("checked!");
return true;
}
});
}
Everything works fine with php ,error messages function properly but if i try to do it with ajax it doesnt work. I used diffrent jQuery codes and plain javascript but i was only able to prevent the button's default reaction and stop the page from relaoding but i couldnt get the error-messages to show. i am aware that you have to get the error-messages display with javascript and run the php as backend script but i couldnt get it to work. i would appreciate any help.
here is my html form code:
<?php require('contact-script.php');
?>
<div class="container">
<form id="contact" action="index.php" method="POST">
<h3 id="test" >Contact Form</h3>
<h4>Contact us for custom quote</h4>
<span class="error" ><?php echo $errors['first']; ?></span>
<fieldset>
<input id="first" placeholder="First Name" name="first" type="text" value="<?php echo $first ?>" tabindex="1" >
</fieldset>
<span class="error" ><?php echo $errors['last']; ?></span>
<fieldset>
<input id="last" placeholder="Last Name" name="last" type="text" value="<?php echo $last ?>" tabindex="1" >
</fieldset>
<span class="error" ><?php echo $errors['mail']; ?></span>
<fieldset>
<input id="mail" placeholder="Your Email Address" name="mail" type="text" value="<?php echo $mail ?>" tabindex="2" >
</fieldset>
<span class="error" ><?php echo $errors['subject']; ?></span>
<fieldset>
<input id="subject" placeholder="Subject" name="subject" type="text" value="<?php echo $subject ?>" tabindex="2" >
</fieldset>
<fieldset>
<textarea id="message" placeholder="Type your message here...." name="message" type="text" value="<?php echo $message ?>" tabindex="5" ></textarea>
</fieldset>
<span class="error" ><?php echo $errors['message']; ?></span>
<fieldset>
<button id="formBtn" name="submit" type="submit" >Submit</button>
</fieldset>
</form>
</div>
here is my php code:
//defining Variables
$first = $last = $mail = $message = $subject = "";
$name_error = ""; $mail_error = ""; $message_error = "";
$errors = array( 'mail'=>'' , 'first'=> '' , 'last'=>'', 'subject'=>'', 'message'=>'');
if (isset($_POST['submit'])) {
$mail = htmlspecialchars($_POST['mail']);
$first = htmlspecialchars($_POST['first']);
$last = htmlspecialchars($_POST['last']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);
//check mail
if (empty($_POST['mail'])) {
$errors['mail'] = 'Bitte geben Sie eine E-mail Adresse an <br />';
}
else {
$mail = $_POST['mail'];
if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
$errors['mail'] = 'Bitte geben Sie eine gültige E-mail an';
}
}
//check first name
if(empty($_POST['first'])) {
$errors['first'] = 'Bitte geben Sie einen Namen an <br />';
}
else {
if(!preg_match('/^[a-zA-Z\s]+$/', $first)) {
$errors['first']= 'Name darf nur Buchstaben und Lehrzeichen enthalten ';
}
}
//check last name
if(empty($_POST['last'])) {
$errors['last'] = 'Bitte geben Sie einen Nachnamen an <br />';
}
else {
if(!preg_match('/^[a-zA-Z\s]+$/', $last)) {
$errors['last']= 'Name darf nur Buchstaben und Lehrzeichen enthalten';
}
}
if(empty($_POST['subject'])) {
$errors['subject'] = 'Bitte geben Sie ein Anliegen an <br />';
}
else {
if(!preg_match('/^[a-zA-Z\s]+$/', $subject)) {
$errors['subject']= 'Anliegen darf nur Buchstaben und Lehrzeichen enthalten ';
}
}
//check for message
if(empty($_POST['message'])) {
$errors['message']= 'Bitte schreiben Sie eine Nachricht <br />';
}
if (array_filter($errors)) {
}else {
$mailTo = "info#blanc-code.de";
$headers = "From: ".$mail;
$text = "Sie haben eine e-mail von ".$first." ".$last." erhalten.\n\n".$message;
mail($mailTo, $subject, $text, $headers);
}
i tryed this code for example:
function submitForm() {
getBtn = document.querySelector('#formBtn');
getBtn.disabled = true;
let formdata = new FormData();
formdata.append("mail", document.querySelector('mail').value);
formdata.append("first", document.querySelector('first').value);
formdata.append("last", document.querySelector('last').value);
formdata.append("subject", document.querySelector('subject').value);
let http = new XMLHttpRequest();
http.open("POST", "contact-script.php");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if(http.responseText == "success") {
}else {
getBtn.disabled = false;
}
}
}
http.send(formdata);
or this:
$(document).ready(function(){
$("form").submit(function(event) {
event.preventDefault();
var first = $("#first").val();
var last = $("#last").val();
var mail = $("#mail").val();
var subject = $("#subject").val();
var message = $("#message").val();
$(".error").load("contact-script.php", {
first: first,
last: last,
mail: mail,
subject: subject,
message: message
});
});
});
there was more but this should give you a clue
I'm trying to submit form data to php script with jQuery .post() method,than to receive php script data back and display it on form page,but it doesnt work.Also I'm using jQuery validate plugin for client side validation as well.Here is my code:
Html:
<div class="contact-form">
<button class="contact-close">X</button>
<form method="post" action="formular.php" id="quote-form">
<div id="returned"></div>
<div class="houndred">
<input type="text" name="name" placeholder="Name*" class="input-half" id="name" min-width="2" autofocus required>
<input type="email" name="email" placeholder="Email*" class="input-half" id="email" required>
<input type="text" name="subject" placeholder="Subject" class="input-half" id="subject" required>
<input type="url" name="url" placeholder="Website" class="input-half" id="website">
</div>
<div class="houndred">
<textarea name="message" class="textarea" id="message" placeholder="message*" required></textarea>
<br><p></p>
</div>
<div class="eightytwo">
<button id="quote-button">Send</button>
</div>
<div id="summary"></div>
</form>
</div><!-- .padding-fix -->
<script src="js/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="js/validation.js"></script>
<script src="js/ajaxform.js"></script>
JS for posting:
$(document).ready(function(){
$('form').on('submit', function(event) {
event.preventDefault();
$.post('formular.php', $(this).serialize(), function(data)) {
var dataForm = data;
$("#returned").append(dataForm);
}
});
)};
and PHP:
if(!empty($_POST)) {
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$url = $_POST['url'];
$message = $_POST['message'];
if(empty($name) || empty($email) || empty($subject) || empty($url) || empty($message)) {
$errors[] = 'All fields are required!';
} else {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'Please enter a valid email address';
}
if(ctype_alpha($name === false)) {
$errors[] = 'Name can only contain letters';
}
if (ctype_alpha($subject === false)) {
$errors[] = 'Subject can only contain letters';
}
if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) === false) {
$errors[] = 'Please enter a valid url address with http in front';
}
if(empty($message)) {
$errors[] = 'Please enter a message';
}
}
if(!empty($errors)) {
echo '<ul style=\"list-style: circle; color: #f74d4e\">';
foreach($errors as $error) {
echo '<li style=\"color: #f74d4e;\"' . $error . '</li>';
}
echo '</ul>';
}
$send_message = 'From:' . $url . '<br>' . strip_tags(addslashes($message));
if(empty($errors)) {
mail('something#something.com', $subject, $send_message , $email);
echo '<h4 style=\"color: #f74d4e;\">Thank you for contacting me!</h4>';
exit();
}
}
I'm hoping that this makes sense,to me it had,but I'm strugling with it for last couple of hours,still trying to figure out what went wrong,I also tryed posting with $.ajax() and no response.One more thing,form is geting loaded using jquery .load() method to popup box,that is why it is important to me to do it with ajax
From a cursory inspection of your code, it seems that you have added an extra closing parentheses in this line, like so: function(data))
$.post('formular.php', $(this).serialize(), function(data)) {
try
$.post('formular.php', $(this).serialize(), function(data) {
instead
HTH
The following form works as intended, including the honeypot, but if someone clicks on Refresh in the browser prior to finishing the form, it tries to resubmit. I have tried sessions but it is not working. Does anyone know how I can make the page redirect back to contact.php or end the session so it does not resubmit on refresh in the browser? ( I have spent 2 days searching for answers ). Thanks
Code:
<!--form-->
<?php
//fields
$link_address = 'contact.php'; // page to redirect to home page
$honeypot = '';
$error = '';
$name = 'Name (required)';
$email = 'Email (required)';
$comments = 'Message (required)';
if(isset($_POST['contactus'])) {
$honeypot = $_POST['honeypot'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
// honeypot
if($honeypot)
exit(1);
//error messages
if(trim($name) == 'Name (required)') {
$error = '<div class="error_message">Please enter your Name</div>';
} else if(trim($name) == '') {
$error = '<div class="error_message">Please enter your Name</div>';
} else if(trim($email) == 'Email (required)') {
$error = '<div class="error_message">Please enter an email address</div>';
} else if(trim($email) == '') {
$error = '<div class="error_message">Please enter an email address</div>';
} else if(!isEmail($email)) {
$error = '<div class="error_message">Please enter a valid email address</div>';
} else if(trim($comments) == 'Message (required)') {
$error = '<div class="error_message">Please enter a Message</div>';
} else if(trim($comments) == '') {
$error = '<div class="error_message">Please enter a Message</div>';
}
if($error == '') {
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
//email address
$address = "email#youremail.com";
//email message
$e_subject = 'Web Message from: ' . $name . '.';
$e_body = "From: $name\nEmail: $email \r\n\nMessage:\n$comments\n\n\n";
$msg = $e_body . $e_content . $e_reply;
if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"))
{
//success html page response
echo "<div id='success_page'>";
echo "<div id='thanks'>";
echo "<h1>Message Sent Successfully.</h1>";
echo "<p id='cprint1'>Thank you. Your message was sent to us. </br>We will be in touch shortly.</p>";
echo "<p id='print'><a href='javascript:window.print()'>Print your Message</a></p>";
echo "<p id='cclose'><a href='$link_address'>Close this Message</a></p>";
echo "</div>";
echo "<div id='thanks2'>";
echo "<div id='titlemsg'>Below is your message</div>";
echo "<p><span>Name:</span> $name</p>";
echo "<p><span>Email:</span> $email</p>";
echo "<p><span>Message:</span>$comments</p>";
echo "</div>";
echo "</div>";
} else echo "Error. Mail not sent";
}
}
if(!isset($_POST['contactus']) || $error != '') // Do not edit.
{
?>
<!--form-->
<form method="post" action="" id="myform" autocomplete="off">
<div id="error"><?php echo $error; ?></div>
<input name="name" type="text" id="name" class="form1"
value="<?php echo $name; ?>" onfocus="if(this.value == 'Name (required)') {this.value = ''; }" onblur="if(this.value == '') { this.value = 'Name (required)'; }" value="Name (required)" />
<input name="email" type="text" id="email" class="form1"
value="<?php echo $email; ?>" onfocus="if(this.value == 'Email (required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Email (required)'; }" value="Email (required)" />
<input name="honeypot" type="text" id="honeypot" class="form2" value="<?php echo $honeypot; ?>" />
<textarea name="comments" cols="40" rows="3" id="comments"
value="<?php echo $comments; ?>" onfocus="if(this.value == 'Message (required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Message (required)'; }" value="Message (required)"><?php echo $comments; ?></textarea>
<input name="contactus" type="submit" class="submit" id="contactus" value="Contact us" />
</form>
<?php }
function isEmail($email) { // Email address verification, do not edit.
return(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,12})$/",$email));
}
?>
You can save your message in session on successfull mail sent. and then refresh your page and check if session exist then show message and reset session.
Just redirect it to the same page after making the use of form data.
Example-
header('location:yourpage.php');
and you can also unset $_POST after inserting it to Database.
unset($_POST);
You can use the javascript redirect:
<script>
window.location.href = 'test.php';
</script>
I'm trying to add to a website a contact form with Jquery and Fancybox.
All seems to doing well, the pop-up animation is ok, the mail seems to by sended but I'm not receiving any mail :(
Could someone help me to understand where is the problem ?
Edit:
-I've checked my junk and spam folder
-Here is the website link: lacouleurdurendezvous.fr
-Someone talked about server misconfiguration, what is it ?
EDIT:
-It seems to be a server malfunction... I'm trying to conatct my web hoster...
Here is how I call my scripts:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js" type="text/javascript"></script>
<script src="Ressources/retour.js"></script>
<script src="Ressources/testhead.js"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox.js?v=2.0.6"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript" src="fancybox/jquery.fancybox.js?v=2.0.6"></script>
Here is the html code:
<li id="menu"><a class="modalbox" href="#inline">Contactez Nous</a></li>
<!-- hidden inline form -->
<div id="inline">
<h2>Envoyez nous un email</h2>
<form id="contact" name="contact" action="#" method="post">
<label for="email">E-mail</label>
<input type="email" id="email" name="email" class="txt">
<br>
<label for="msg">Message</label>
<textarea id="msg" name="msg" class="txtarea"></textarea>
<button id="send">Envoyer</button>
</form>
</div>
<!-- basic fancybox setup -->
<script type="text/javascript">
function validateEmail(email) {
var reg = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(email);
}
$(document).ready(function() {
$(".modalbox").fancybox();
$("#contact").submit(function() { return false; });
$("#send").on("click", function(){
var emailval = $("#email").val();
var msgval = $("#msg").val();
var msglen = msgval.length;
var mailvalid = validateEmail(emailval);
if(mailvalid == false) {
$("#email").addClass("error");
}
else if(mailvalid == true){
$("#email").removeClass("error");
}
if(msglen < 4) {
$("#msg").addClass("error");
}
else if(msglen >= 4){
$("#msg").removeClass("error");
}
if(mailvalid == true && msglen >= 4) {
// if both validate we attempt to send the e-mail
// first we hide the submit btn so the user doesnt click twice
$("#send").replaceWith("<em>en cours d'envoi</em>");
$.ajax({
type: 'POST',
url: 'sendmessage.php',
data: $("#contact").serialize(),
success: function(data) {
if(data == "true") {
$("#contact").fadeOut("slow", function(){
$(this).before("<p><strong>Merci de votre interet, bonne journee</strong></p>");
setTimeout("$.fancybox.close()", 4000);
});
}
}
});
}
});
});
</script>
And the php:
<?php
$sendto = "hello#tiphainebuccino.com";
$usermail = $_POST['email'];
$content = nl2br($_POST['msg']);
$subject = "La Couleur Du Rendez Vous";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";
if(#mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}
?>