I'm following this extensive article (with sources) to make a contact form on a website. However, I need a slightly more complex form, with more fields and also a radio buttons group.
I simply added to the process.php and scripts.js files the additional fields I needed. Amongst the many errors I could get, the submit button simply doesn't work at all. I click on it and nothing happens. I'm a newbie in PHP and JS, so I don't know a proper way how to debug and understand where the problem is. Could you help me find it?
Here's my code:
EDIT:
This part actually doesn't work, because I get an empty field in the string yielded. Any idea? These are the non-required fields:
// ASCENSORI CONDOMINIO
if (empty($_POST["ascensoriCondominio"])) {
$ascensoriCondominio = "Valore non specificato";
} else {
$ascensoriCondominio = $_POST["ascensoriCondominio"];
}
// SCALE CONDOMINIO
if (empty($_POST["scaleCondominio"])) {
$scaleCondominio = "Valore non specificato";
} else {
$scaleCondominio = $_POST["scaleCondominio"];
}
// RISCALDAMENTO CONDOMINIO
if (empty($_POST["riscaldamento"])) {
$riscaldamento = "Valore non specificato";
} else {
$riscaldamento = $_POST["riscaldamento"];
}
HTML:
<!----------------------------- FORM CONDOMINI ---------------------------------------->
<form action="php/process.php" role="form" id="condominiForm" data-toggle="validator" class="col-xs-12 shake">
<div class="row">
<h3 class="col-xs-12">Referente</h3>
<div class="form-group col-sm-6">
<label for="name" class="h4 obbligatorio">Nome</label>
<input type="text" class="form-control" id="name" placeholder="Inserisci nome" required data-error="Inserire nome">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="cognome" class="h4 obbligatorio">Cognome</label>
<input type="text" class="form-control" id="cognome" placeholder="Inserisci cognome" required data-error="Inserire cognome">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
<label for="email" class="h4 obbligatorio">Email</label>
<input type="email" class="form-control" id="email" placeholder="Inserisci email" required data-error="Inserire email">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="telefono" class="h4 obbligatorio">Telefono/Cellulare</label>
<input type="text" class="form-control" id="telefono" placeholder="Inserisci recapito telefonico" required data-error="Inserire recapito telefonico">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h3>Condominio</h3>
</div>
<div class="form-group col-sm-6">
<label for="nomeCondominio" class="h4 obbligatorio">Nome</label>
<input type="text" class="form-control" id="nomeCondominio" placeholder="Inserisci nome condominio" required data-error="Inserire nome condominio">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="indirizzoCondominio" class="h4 obbligatorio">Indirizzo</label>
<input type="text" class="form-control" id="indirizzoCondominio" placeholder="Inserisci indirizzo" required data-error="Inserire indirizzo condominio">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
<label for="comuneCondominio" class="h4 obbligatorio">Comune</label>
<input type="text" class="form-control" id="comuneCondominio" placeholder="Inserisci comune" required data-error="Inserire comune condominio">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="provinciaCondominio" class="h4 obbligatorio">Provincia</label>
<input type="text" class="form-control" id="provinciaCondominio" placeholder="Inserisci provincia" required data-error="Inserire provincia condominio">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
<label for="unitaCondominio" class="h4 obbligatorio">Numero unità</label>
<input type="text" class="form-control" id="unitaCondominio" placeholder="Inserisci numero unità" required data-error="Inserire numero unità">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="ascensoriCondominio" class="h4">Numero ascensori</label>
<input type="text" class="form-control" id="ascensoriCondominio" placeholder="Inserisci numero ascensori" data-error="Inserire numero ascensori">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
<label for="scaleCondominio" class="h4">Numero scale</label>
<input type="text" class="form-control" id="scaleCondominio" placeholder="Inserisci numero scale" data-error="Inserire numero scale">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<!--<label for="riscaldamentoCondominio" class="h4">Riscaldamento centralizzato</label>-->
<!--<input type="text" class="form-control" id="riscaldamentoCondominio" placeholder="Inserisci numero ascensori" data-error="Inserire numero ascensori">-->
<h4>Riscaldamento centralizzato</h4>
<label class="radio-inline"><input type="radio" name="riscaldamento">Sì</label>
<label class="radio-inline"><input type="radio" name="riscaldamento">No</label>
<!--<div class="help-block with-errors"></div>-->
</div>
</div>
<!--<button type="submit" id="form-submit" class="pulsante">Invia richiesta di preventivo!</button>-->
<!--<div class="col-sm-10 col-sm-offset-1">-->
<!--</div>-->
<input type="submit" id="form-submit" class="btn btn-success btn-lg pull-right">Submit</input>
<div id="msgSubmitCondomini" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</form>
JS:
$("#condominiForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Hai completato correttamente i campi?");
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var cognome = $("#cognome").val();
var email = $("#email").val();
var telefono = $("#telefono").val();
var nomeCondominio = $("#nomeCondominio").val();
var indirizzoCondominio = $("#indirizzoCondominio").val();
var comuneCondominio = $("#comuneCondominio").val();
var provinciaCondominio = $("#provinciaCondominio").val();
var unitaCondominio = $("#unitaCondominio").val();
var ascensoriCondominio = $("#ascensoriCondominio").val();
var scaleCondominio = $("#scaleCondominio").val();
var riscaldamento = $("input[name=riscaldamento]:checked").val();
$.ajax({
type: "POST",
url: "php/process.php",
data: "name=" + name + "&cognome=" + cognome + "&email=" + email + "&telefono=" + telefono + "&nomeCondominio=" + nomeCondominio + "&indirizzoCondominio=" + indirizzoCondominio + "&comuneCondominio=" + comuneCondominio + "&provinciaCondominio=" + provinciaCondominio + "&unitaCondominio=" + unitaCondominio + "&ascensoriCondominio=" + ascensoriCondominio + "&scaleCondominio=" + scaleCondominio + "&riscaldamento=" + riscaldamento,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$("#condominiForm")[0].reset();
submitMSG(true, "Messaggio inviato!")
}
function formError(){
$("#condominiForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = "h3 text-center tada animated text-success";
} else {
var msgClasses = "h3 text-center text-danger";
}
$("#msgSubmitCondomini").removeClass().addClass(msgClasses).text(msg);
}
PHP
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// COGNOME
if (empty($_POST["cognome"])) {
$errorMSG .= "Cognome is required ";
} else {
$cognome = $_POST["cognome"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// TELEFONO
if (empty($_POST["telefono"])) {
$errorMSG .= "Telefono is required ";
} else {
$telefono = $_POST["telefono"];
}
// NOME CONDOMINIO
if (empty($_POST["nomeCondominio"])) {
$errorMSG .= "Message is required ";
} else {
$nomeCondominio = $_POST["nomeCondominio"];
}
// INDIRIZZO CONDOMINIO
if (empty($_POST["indirizzoCondominio"])) {
$errorMSG .= "Message is required ";
} else {
$indirizzoCondominio = $_POST["indirizzoCondominio"];
}
// COMUNE CONDOMINIO
if (empty($_POST["comuneCondominio"])) {
$errorMSG .= "Message is required ";
} else {
$comuneCondominio = $_POST["comuneCondominio"];
}
// PROVINCIA CONDOMINIO
if (empty($_POST["provinciaCondominio"])) {
$errorMSG .= "Message is required ";
} else {
$provinciaCondominio = $_POST["provinciaCondominio"];
}
// UNITA CONDOMINIO
if (empty($_POST["unitaCondominio"])) {
$errorMSG .= "Message is required ";
} else {
$unitaCondominio = $_POST["unitaCondominio"];
}
// ASCENSORI CONDOMINIO
if (!empty($_POST["ascensoriCondominio"])) {
$ascensoriCondominio = $_POST["ascensoriCondominio"];
} else {
$ascensoriCondominio = "Valore non specificato";
}
// SCALE CONDOMINIO
if (!empty($_POST["scaleCondominio"])) {
$scaleCondominio = $_POST["scaleCondominio"];
} else {
$scaleCondominio = "Valore non specificato";
}
// RISCALDAMENTO CONDOMINIO
if (!empty($_POST["riscaldamento"])) {
$riscaldamento = $_POST["riscaldamento"];
} else {
$riscaldamento = "Valore non specificato";
}
$EmailTo = "alessandrocpr#gmail.com";
$Subject = "Nuova contatto da sito Abacond";
// prepare email body text
$Body = "REFERENTE";
$Body .= "\n";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Cognome: ";
$Body .= $cognome;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Telefono: ";
$Body .= $telefono;
$Body .= "\n";
$Body .= "\n";
$Body .= "CONDOMINIO";
$Body .= "\n";
$Body .= "\n";
$Body .= "Nome: ";
$Body .= $nomeCondominio;
$Body .= "\n";
$Body .= "Indirizzo: ";
$Body .= $indirizzoCondominio;
$Body .= "\n";
$Body .= "Comune: ";
$Body .= $comuneCondominio;
$Body .= "\n";
$Body .= "Provincia: ";
$Body .= $provinciaCondominio;
$Body .= "\n";
$Body .= "Numero unità: ";
$Body .= $unitaCondominio;
$Body .= "\n";
$Body .= "Numero ascensori: ";
$Body .= $ascensoriCondominio;
$Body .= "\n";
$Body .= "Numero scale: ";
$Body .= $scaleCondominio;
$Body .= "\n";
$Body .= "Riscaldamento centralizzato: ";
$Body .= $riscaldamento;
$Body .= "\n";
var_dump($Body);
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Qualcosa è andato storto :(";
} else {
echo $errorMSG;
}
}
?>
Your form action is php/process.php and your ajax call is php/process.php. When you click the submit button the JS event for the form is triggered but doesn't render an alternate page and override the form action of the html. I'd suggest simplifying the steps of the interaction of the components to understand them better before trying to master the complexity of what you have here.
I reproduced your code in localhost and all works fine if delete the validator() function:
$("#condominiForm").on("submit", function (event) {
Without that, the jquery still prevents to submit blank inputs and when all filled, the ajax call is executed, all the fields sended and all works fine.
You can see this on: https://www.dropbox.com/s/eqc3vzxpwb0jmgj/test.png?dl=0
Try removing that part. Hope it helps to you!
UPDATE
Also, for sending the mail, add this part for sending better emails:
$reply = "your#mail.com";
$headers = "From: " . strip_tags($Subject) . "\r\n";
$headers .= "Reply-To: ". strip_tags($reply) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Now you can use html tags:
$Body = '<html><body>';
$Body .= '<your actual code with html tags>';
$Body .= '</body></html>';
UPDATE 2
You forgot to set a value for radio inputs:
<input .... value='0'> or <input ... value='Si'>
$("#condominiForm").onclick(function (event) {
you should use on click event to run your all j query code its good of your project because some time on submit is not working
Related
I am currently facing an issue and I am unable to understand the error. I have been trying to resolve it for some time now but I am unable to figure out what is causing the problem. I have been looking online for solutions but I have not been able to find any that have worked for me. I am now reaching out for help online in hopes that someone can assist me in understanding and resolving the error. Any help would be greatly appreciated.
I am trying to send an email through JavaScript and PHP and it gives the following error:
'mobile is required Message is required'
Here is my PHP code:
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
//mobile
if (empty($_POST["mobile"])) {
$errorMSG = "mobile is required ";
} else {
$mobile = $_POST["mobile"];
}
//Design type
if (empty($_POST["Dtype"])) {
$errorMSG = "Design Type is required ";
} else {
$Dtype = $_POST["Dtype"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
// replace email to get from date
$EmailTo = "mail#mail.com";
$EmailTo1 = "mail#mail.com";
$Subject = "New Enquery On _____";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "mobile: ";
$Body .= $mobile;
$Body .= "\n";
$Body .= "Design Type: ";
$Body .= $Dtype;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
$success = mail($EmailTo1, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>`
I also have the script here:
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
formError();
submitMSG(false, "Did you fill in the form properly?");
} else {
event.preventDefault();
submitForm();
}
});
function submitForm() {
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var mobile = $("#mobile").val();
var Dtype = $("#Dtype").val();
$.ajax({
type: "POST",
url: "php/form-process.php",
data: "name=" + name + "&email=" + email + "&message=" + message + "&mobile=" + mobile + "&Dtype=" + Dtype,
success: function (text) {
if (text == "success") {
formSuccess();
} else {
formError();
submitMSG(false, text);
}
}
});
}
function formSuccess() {
$("#contactForm")[0].reset();
submitMSG(true, "Message Submitted!")
}
function formError() {
$("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
$(this).removeClass();
});
}
function submitMSG(valid, msg) {
if (valid) {
var msgClasses = "h3 text-center tada animated text-success";
} else {
var msgClasses = "h3 text-center text-danger";
}
$("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
$('#msgSubmit').delay(2000).hide(0);
}
Also the important part of the form here:
<form id="contactForm" data-toggle="validator" class="shake" action="php/form-process.php" method="post">
<div class="row">
<div class="fieldsets col-sm-12">
<input type="text" id="name" placeholder="Enter name" required data-error="Please fill Out">
<div class="help-block with-errors"></div>
</div>
<div class="fieldsets col-sm-12">
<input type="email" id="email" placeholder="Enter email" required>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="fieldsets col-sm-12">
<input type="text" id="mobile" placeholder="Enter mobile" required data-error="Please fill Out">
<div class="help-block with-errors"></div>
</div>
<div class="fieldsets col-sm-12">
<select name="Dtype" id="Dtype" required>
<option value="">Select Requirement</option>
<option value="2BHK+2T">2BHK +2T</option>
<option value="3BHK+2T">3BHK +2T</option>
<option value="3BHK+3T">3BHK + 3T</option>
<option value="3BHK+3T+POOJA">3BHK + 3T +POOJA</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="fieldsets">
<textarea id="message" rows="4" placeholder="Enter your message" required></textarea>
<div class="help-block with-errors"></div>
</div>
<button type="submit" id="form-submit" class="btn lnk btn-main bg-btn">Submit <span class="circle"></span></button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
<p class="trm"><i class="fas fa-lock"></i>We hate spam, and we respect your privacy.</p>
</form>
I have tried to completely re-write my php script, my form and everything. I just can't seem to figure it out and it got me stressed so badly :(
I got my simple PHP contact form working on my website over at
http://www.colin-grant.ca/speedvale
but when I migrated my contact form to http://www.speedvale.com, it gives me the error 'something went wrong :('.
I have posted my code below for you to see, any help would be great - as I'm not the best at PHP.
HTML
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 mx-auto">
<div class="row">
<div class="col-sm-12 col-sm-offset-3">
<div class="well">
<h1>Contact Us</h1>
<p class="text-center">We are always interested in hearing about how we can improve your visit. If you need more information regarding our packages, or if you wish to set up a detailing appointment, toss us a message below and we will get back to you ASAP.</p>
<hr>
<form role="form" id="contactForm" data-toggle="validator" class="shake">
<div class="row">
<div class="form-group col-sm-6">
<label for="fname" class="h4">First Name</label>
<input type="text" class="form-control" id="fname" placeholder="Please enter your First Name" required data-error="Please Enter First Name">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="lname" class="h4">Last Name</label>
<input type="text" class="form-control" id="lname" placeholder="Please enter your Last Name" required data-error="Please Enter Last Name">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
<label for="email" class="h4">Email</label>
<input type="text" class="form-control" id="email" placeholder="Whats your e-mail?" required data-error="Please enter email">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-sm-6">
<label for="phone" class="p">Phone</label>
<input type="text" class="form-control" id="phone" placeholder="Whats your phone number?" required data-error="Please enter Phone Number">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="form-group col-sm-12">
<label for="email" class="h4">Service</label>
<select class="form-control" id="service" placeholder="Service"name="services">
<option value="" selected disabled>Please choose a service</option>
<option value="Gold Package ($79.99 / $99.99)">Gold Package ($79.99 / $99.99)</option>
<option value="Platinum 1 Package ($144.99 / $174.99)">Platinum 1 Package ($144.99 / $174.99)</option>
<option value="Platinum 2 Package ($199.99 / $229.99)">Platinum 2 Package ($199.99 / $229.99) </option>
<option value="VIP Package ($329.99 / $359.99)">VIP Package ($329.99 / $359.99)</option>
<option value="Express Package ($29.99 / $39.99)">Express Package ($29.99 / $39.99)</option>
<option value="Interior Package ($119.99 / $139.99)">Interior Package ($119.99 / $139.99)</option>
<option value="Exterior Package ($84.99 / $134.99)">Exterior Package ($84.99 / $134.99)</option>
<option value="Additional Services">Additional Services</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<label for="message" class="p ">Message</label>
<textarea id="message" class="form-control" rows="5" placeholder="Please leave details here about the make and model of your car, and any additional information we can use to make your apppointment or request better! Thank you" required></textarea>
<div class="help-block with-errors"></div>
</div>
<button style="width:100%; padding: 10px; margin-bottom: 20px;" type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
<p class="text-center" style="font-size: 14px;">Please do your best to fill out the form below with as much information as possible so we can get started right away on your inquiry!</p>
<p class="text-center" style="font-size:14px;">Thank you from all of us here at Speedvale Auto Detailing.</p>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
PHP
<?php
$errorMSG = "";
// FIRST NAME
if (empty($_POST["fname"])) {
$errorMSG = "First Name is required ";
} else {
$fname = $_POST["fname"];
}
// LAST NAME
if (empty($_POST["lname"])) {
$errorMSG = "Last Name is required ";
} else {
$lname = $_POST["lname"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// PHONE
if (empty($_POST["phone"])) {
$errorMSG .= "Phone Number is required ";
} else {
$phone = $_POST["phone"];
}
// SERVICE
if (empty($_POST["service"])) {
$errorMSG .= "Service Choice is required ";
} else {
$service = $_POST["service"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "myemail#gmail.com";
$Subject = "New Message Received";
// prepare email body text
$Body .= "First Name: ";
$Body .= $fname;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $lname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Service: ";
$Body .= $service;
$Body .= "\n";
$Body .= "Message ";
$Body .= "\n";
$Body .= $message;
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
JAVASCRIPT
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Did you fill in the form properly?");
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var fname = $("#fname").val();
var lname = $("#lname").val();
var email = $("#email").val();
var phone = $("#phone").val();
var service = $("#service").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "php/form-process.php",
data: "fname=" + fname + "&lname=" + lname + "&phone=" + phone + "&email=" + email + "&service=" + service + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$("#contactForm")[0].reset();
submitMSG(true, "Message Submitted!")
}
function formError(){
$("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = "h3 text-center tada animated text-success";
} else {
var msgClasses = "h3 text-center text-danger";
}
$("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}
Anyone who can help with this would be great, its the last piece of the website I need to get done.
Thanks a bunch!
As output is coming as "Something went wrong :(" so as per your code i.e
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
the mail is not been sent and you are getting false in $success i.e while sending mail. Also the else condition can be improve:
if(!empty($errorMSG)){
echo "Something went wrong, error: " . $errorMSG;
} else {
echo "Mail not sent";
}
i am totally new to ajax and just started getting a hang of php. So i got a ajax and php email code online that seemed to work on the first try and next few days suddenly kept returning with the error message which is "something went wrong". Please i'd appreciate if i could be put through on the source of this and how i could fix it. Thanks much
JS
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Did you fill in the form properly?");
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "php/form-process.php",
data: "name=" + name + "&email=" + email + "&msg_subject=" + msg_subject + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$("#contactForm")[0].reset();
submitMSG(true, "Message Submitted!")
}
function formError(){
$("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = "h3 text-center tada animated text-success";
} else {
var msgClasses = "h3 text-center text-danger";
}
$("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="contactForm">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="Full Name" name="name" data-error="Please input your full name" required>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" id="email" placeholder="Your Email" name="name" data-error="Please input your email" required>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea class="form-control" id="message" placeholder="Your Message" rows="8" data-error="Write your message" required></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="submit-button text-center">
<button class="btn btn-common" id="submit" type="submit">Send Message</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</div>
</div>
</div>
</form>
PHP
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
$errorMSG .= "Subject is required ";
} else {
$msg_subject = $_POST["msg_subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "name#email.com";
$Subject = "New Message Received";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :("; //-->this is the error i get
} else {
echo $errorMSG;
}
}
?>
your logic is backwards, in your code an empty $errorMSG is good.
if($errorMSG != ""){ // if NOT empty
echo "Something went wrong: ".$errorMSG;
}else{
//good
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Hello I have made a contact form on my website but when the user type in the details and submit the form, it sends some blank information and cannot find why. Also, a major issue, the form changes the name field for the countries one, so it came in like "Name: France" instead of "Name: John".
I have also added form validator from here https://github.com/1000hz/bootstrap-validator. Anyone know why this is happening?
Below are the js, the php and the html.
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Something went wrong! Did you fill in the form properly?");
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var last = $("#last").val();
var email = $("#email").val();
var company = $("#company").val();
var users = $("#users").val();
var country = $("#country").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "assets/php/form-process.php",
data: "name=" + name + "&last=" + last + "&email=" + email + "&company=" + company + "&users=" + users + "&country=" + country + "&msg_subject=" + msg_subject + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$("#contactForm")[0].reset();
submitMSG(true, "Message Submitted!")
}
function formError(){
$("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = "h3 text-center tada animated text-success";
} else {
var msgClasses = "h3 text-center text-danger";
}
$("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// LAST NAME
if (empty($_POST["last"])) {
$errorMSG = "Last name is required ";
} else {
$name = $_POST["last"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// COMPANY
if (empty($_POST["company"])) {
$errorMSG = "Company name is required ";
} else {
$name = $_POST["company"];
}
// USERS
if (empty($_POST["users"])) {
$errorMSG = "Users number is required ";
} else {
$name = $_POST["users"];
}
// COUNTRY
if (empty($_POST["country"])) {
$errorMSG = "Country is required ";
} else {
$name = $_POST["country"];
}
// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
$errorMSG .= "Subject is required ";
} else {
$msg_subject = $_POST["msg_subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "info#email.com";
$Subject = "New Message Received";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Last: ";
$Body .= $last;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $company;
$Body .= "\n";
$Body .= "Users: ";
$Body .= $users;
$Body .= "\n";
$Body .= "Country: ";
$Body .= $country;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
<html>
<form id="contactForm" class="contact-form">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="sr-only">First name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="First Name" required data-error="Please enter your First name">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="sr-only">Last name</label>
<input type="text" name="last" id="last" class="form-control" placeholder="Last Name" required data-error="Please enter your Last name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<!-- end of /.row -->
<div class="col-md-12">
<div class="form-group">
<label class="sr-only">Email address</label>
<input type="email" name="email" id="email" class="form-control" placeholder="Your Email" required data-error="Please enter your Email address">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="sr-only">Company</label>
<input type="text" name="company" id="company" class="form-control" placeholder="Company" required data-error="Please enter your Company name">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="Users" class="sr-only legacy-label">Users</label>
<select class="form-control" id="users" input name="users" required data-error="Please select the User Number">
<option value="">Users Number</option>
<option value="1 - 3">1 - 3</option>
<option value="4 - 7">4 - 7</option>
<option value="8 - 12">8 - 12</option>
<option value="More than 13">More than 13</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="Country" class="sr-only legacy-label">Country</label>
<select class="form-control" id="country" input name="country" required data-error="Please select your Country">
<option value="">Select your country</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="And so on...">And so on...</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<!-- end of /.row -->
<div class="col-md-12">
<div class="form-group">
<label class="sr-only">Subject</label>
<input type="text" name="msg_subject" id="msg_subject" class="form-control" placeholder="Subject" required data-error="Please enter your Subject">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="sr-only">Your Message</label>
<textarea name="message" id="message" class="form-control" rows="5" placeholder="Your Message" required data-error="Please enter your Message"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" id="submit" class="btn btn-base-alt btn-cmd">Send Message</button>
</form>
</html>
Any help would be much appreciated :)
One of your problem is the variable attribution, each input should have is own variable
You make
$name = $_POST["name"];
...
$name = $_POST["last"];
...
$name = $_POST["company"];
...
$name = $_POST["users"];
...
$name = $_POST["country"];
You should have
$name = $_POST["name"];
...
$last = $_POST["last"];
...
$company = $_POST["company"];
...
$users = $_POST["users"];
...
$country = $_POST["country"];
The last attribution is the country, that why you have
Name: France" instead of "Name: John"
I do not receive any emails when my webform is filled out. It does not give me any errors. Code posted below:
HTML form:
<div class="col-md-6">
<h3>General Questions? <p class="h4">Use our form.</p></h3>
<address>Required fields are marked with an *.</address>
<div class="col-md-12">
<form data-toggle="validator" role="form" class="form-horizontal" id="contactForm">
<!-- Name-->
<div class="form-group">
<label for="inputName" class="control-label"><span class="glyphicon glyphicon-user">
</span> Name * </label><input type="text" class="form-control" id="inputName"
placeholder="What's your name?" data-errors="We need to know what to call you!" required>
<div class="help-block with-errors"></div>
</div>
<!-- Email-->
<div class="form-group">
<label for="inputEmail" class="control-label"><span class="glyphicon glyphicon-
envelope"></span> Email *</label>
<input type="email" class="form-control" id="inputEmail" placeholder="example#example.com"
data-errors="This email address is invalid" required>
<div class="help-block with-errors"></div>
</div>
<!-- Phone Number-->
<div class="form-group">
<label for="inputNumber" class="control-label"><span class="glyphicon glyphicon-phone-
alt"></span> Phone Number</label>
<input type="text" class="form-control" id="inputNnumber" data-minlength="9"
placeholder="(123) 867-5309">
</div>
<!-- Textarea -->
<div class="form-group">
<label for="inputMessage" class="control-label"><span class="glyphicon glyphicon-pencil">
</span> Message *</label>
<textarea rows="8" class="form-control" placeholder="How can we help?" data-errors="We
can't help if you don't tell us how!" required>
</textarea>
<div class="help-block with-errors"></div>
</div>
<!-- Select Basic -->
<div class="control-group text-center">
<span><span class="glyphicon glyphicon-question-sign"></span> <strong>How should we
contact you?</strong></span><br>
<input type="radio" name="contact" value="phone"> by phone
<input type="radio" name="contact" value="email"> by email
</div>
<!-- Button -->
<br>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sm btn-block">Submit</button>
</div>
</form>
</div>
</div>
Javacript: (it's at devnew.company.com/mail.php and will eventually be at company.com/mail.php. Am I correct in leaving it at /mail.php here?)
var mailUrl = "/mail.php";
var formId = "contactForm";
var completeHTML = ' <div class="alert alert-primary"> Sent! Thanks, we will be in touch
soon. </div> ';
function submitForm(){
$.ajax({
url : mailUrl,
type: 'post',
data:{
inputName : $('#inputName').val(),
inputEmail : $('#inputEmail').val(),
inputNumber : $('#inputNumber').val(),
inputMessage : $('#inputMessage').val(),
contact : $('input[name="contact"]:checked').val()
}
});
}
$(function(){
$('#contactForm').submit(function(){
submitForm();
$(this).html( completeHTML );
return false;
})
})
PHP
<?php
if (isset($_POST['Submit'])) {
if ($_POST['inputName'] != "") {
$_POST['inputName'] = filter_var($_POST['inputName'], FILTER_SANITIZE_STRING);
if ($_POST['inputName'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['inputEmail'] != "") {
$email = filter_var($_POST['inputEmail'], FILTER_SANITIZE_EMAIL);
if (!filter_var($inputEmail, FILTER_VALIDATE_EMAIL)) {
$errors .= "$inputNumber is not a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['inputNumber'] != "") {
$homepage = filter_var($_POST['inputNumber'], FILTER_SANITIZE_NUMBER_INT);
if ($_POST['inputNumber'] == "") {
$errors .= "Please enter 9 digits.<br/><br/>";
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['inputMessage'] != "") {
$_POST['inputMessage'] = filter_var($_POST['inputMessage'],
FILTER_SANITIZE_STRING);
if ($_POST['inputMessage'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}
if ($_POST['radio'] != "") {
$_POST['radio'] = filter_var($_POST['radio'], FILTER_SANITIZE_STRING);
if ($_POST['inputName'] == "") {
$errors .= 'Please choose an option.<br/><br/>';
}
} else {
$errors .= 'Please choose one.<br/>';
}
if (!$errors) {
$mail_to = 'rnunley#remedypartners.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['inputName'] . "\n";
$message .= 'Email: ' . $_POST['inputEmail'] . "\n";
$message .= 'Homepage: ' . $_POST['inputNumber'] . "\n";
$message .= "Message:\n" . $_POST['inputMessage'] . "\n\n";
$message .= 'Contact choice: ' . $_POST['radio'] . "\n";
mail($to, $subject, $message);
echo "Thank you for your email, we'll be in touch!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>
From your code, it doesn't look like you are sending a Submit variable in your ajax data. If that is the case, then your PHP will fail your initial if statement. You could try to add a print_r($_POST); at the beginning of your PHP and then watch the browser console to see what PHP is receiving.
It could be that your javascript thinks that the variables you are trying to pass are being read as non existent javascript vars
below is what maybe could work
$.ajax({
url : mailUrl,
type: 'post',
data:{
"inputName" : $('#inputName').val(),
"inputEmail" : $('#inputEmail').val(),
"inputNumber" : $('#inputNumber').val(),
"inputMessage" : $('#inputMessage').val(),
"Submit" : "TRUE",
"contact" : $('input[name="contact"]:checked').val()
},success: function (data) {
alert(data);
}
});
and add this on the beginning of your mail.php
foreach($_POST as $key => $value){
$content .= $key.' : '.$value;
}
echo $content
Too see what you are recieving on your mailurl