Form validation output not working - php

I have a form and the form is working: I receive all the details on my e-mail. But when I click submit, it redirects to a blank page with the text: "Request submitted successfully. We will contact with you very soon.".
What I would like to do is to show a green box saying that the form was submitted successfully, on the same page like most websites do, and not a redirect...
I am using Bootstrap :)
Mailer.php code:
<?php
$tipoIn = $_POST['tipoIn'];
$Tipologia_input = $_POST['Tipologia_input'];
$sender_name = $_POST['Nome'];
$sender_email = $_POST['email'];
$phone = $_POST['Telefone'];
$slider_value = $_POST['slider_value'];
$mail_body = $_POST['message'];
$body = $sender_name." sent a new message for you<br><br> Name: ".$sender_name."<br>Email: ".$sender_email."<br>Phone: ".$phone."<br>Tipo: ".$tipoIn."<br>Tipologia: ".$Tipologia_input."<br>Slider Value: ".$slider_value."<br>Message: ".$mail_body;
sendMail($sender_name , $sender_email, $body);
function sendMail($sender, $sender_mail, $body) {
$to = 'geral#porta10.com'; // Set Receiver Email Here
$myemail = 'geral#porta10.comm'; // Set Sender Email Here
$subject = "New EasyLoan Client"; // Set Subject Here
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Lead <geral#porta10.com>\r\n"; // Set Header Here
$message = $body;
$sentmail = mail($to,$subject,$message,$headers);
if($sentmail) { echo "Request submitted successfully. We will contact with you very soon."; }
else { echo "Mail not sent"; }
}
?>
Form code:
<div class="form-container">
<div class="form-mockup">
<h2>Que imóvel procura ?</h2>
<!-- <h4>Easy to apply for a loan with us,Once you have complete this form. </h4>-->
<form action="mailer.php" method="post">
<div class="form-group">
<select class="form-control" id="tipo" name="tipoIn">
<option value="Tipo" disabled selected>Tipo de imóvel</option>
<option value="Moradia">Moradia</option>
<option value="Apartamento">Apartamento</option>
</select>
</div>
<div class="form-group">
<select class="form-control" id="tipologia" name="Tipologia_input">
<option value="Tipologia" disabled selected>Tipologia</option>
<option value="T1">T1</option>
<option value="T2">T2</option>
<option value="T3">T3</option>
<option value="T4">T4</option>
<option value="T5">T5</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Nome Completo" name="Nome">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="E-mail" name="email">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Telefone" name="Telefone">
</div>
<div class="form-group">
<textarea class="form-control" rows="3" id="comentario" placeholder="Diga-nos como imagina a sua casa..." name="message"></textarea>
</div>
<div class="form-group">
<div class="button-slider">
<div class="btn-group btn_group">
<div class="btn btn-default btn_amount">Valor </div>
<div class="btn btn-default btn_slider">
<input id="bootstrap-slider" type="text" data-slider-min="1" data-slider-max="50000" data-slider-step="1" data-slider-value="25000" name="slider_value" />
<div class="valueLabel"><span class="text_span">Valor </span> <span id="sliderValue">25000</span>€</div>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-default quote_btn">Enviar</button>
</form>
</div>
</div>

You can do various things, it depends on the framework or cms or the logic of your code. there are many ways to accomplish this task (showing on (same/existing) page and not redirecting to (blank/defaced)) but its still depends.
2 main ways i currently can imagine are:
Using Ajax and json for messaging between Server and client and
prevent reloading
Using session-keys to show up messages on any page user loads
afterwards.

Related

PHP Contact form error sending email

I currently have a contact form on my index.html & contact.html pages. When testing the PHP contact form they do not send me an email or give any notification that the message has been sent, just appearing to do nothing.
HTML
<div class="form">
<form action="php/contact.php" method="post">
<h5 class="grey-bg text-center iq-font-black iq-tw-6 iq-pall-20">Service Inquiry</h5>
<div class="iq-pall-30">
<div class="row">
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="name" placeholder="Name" id="name" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="email" placeholder="Email" id="email" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<select name="service" id="service">
<option value="Choose Service" >Choose Service</option>
<option value="test1" >test1</option>
<option value="test" >Test</option>
<span class="error">A service is required</span>
</select>
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="phonenumber" placeholder="Phone Number" id="phonenumber" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field ">
<textarea class="form-control" placeholder="Comment" rows="5" id="comment" style="resize: none;"></textarea>
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<button id="submit" name="submit" type="submit" value="Send" class="button pull-right iq-mt-20">Send Message</button>
</div>
</div>
</div>
</form>
</div>
PHP Code
<?php $ToEmail = 'test#test.com';
$EmailSubject = 'User Contact Information';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Service: ".$_POST["service"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Service: ".$_POST["service"]."";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");?>
The email's I do receive are like this:
User Contact Information
Name: Email: Phone: Comment:
Do the fields match the PHP form, and the send message button, how can that have a response for if the email has been sent or not?
A couple errors:
Your <div class="iq-pall-30"> and <div class="row"> elements are unclosed, which make your markup invalid and might affect the output you're getting to your PHP script.
You reference form elements that doesn't actually exist in your form:
$_POST["phone"] is probably supposed to be a reference to your HTML form element <input type="text" name="phonenumber">. You'll have to change your variable reference to $_POST["phonenumber"] to correctly retrieve the data.
$_POST["message"] is probably supposed to be a reference to your textarea form element, but you haven't assigned a name to that element in your markup. You should add name="message" as an attribute of the textarea in order to correctly retrieve that data.

How do I get this php form to work?

Ive got a contact form thats using the same php code on another website and it works great, but on this one it will not work for some reason, after clicking send, it redirects me to a blank page saying "No Arguments Provided!". Below is the html and php for the form.
<!-- Contact form -->
<section id="contact_form">
<div class="container">
<div class="row">
<div class="col-md-6">
<h2> We would love to hear about your upcoming project.</h2>
<h2 class="second_heading">Get In Touch With Us!</h2>
</div>
<form role="form" class="form-inline text-right col-md-6" method="post" action="mail/contact_us.php" name="sentMessage" id="contactForm" novalidate>
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<textarea class="form-control" rows="5" id="msg" placeholder="Message"></textarea>
</div>
<button type="submit" class="btn submit_btn">Submit</button>
</form>
</div>
</div>
</section><!-- Contact form end -->
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['msg']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['msg'];
// create email body and send it
$to = 'myemail#address.com'; // send to: email
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact
form.\n\n"."Here are the details:\n\nName: $name\n\nEmail:
$email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#myemail.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
All your form inputs/textareas are missing the name attribute. For example:
<input type="email" class="form-control" id="email" placeholder="Email">
Needs to be
<input type="email" class="form-control" id="email" name="email" placeholder="Email">
The name attribute is what's used when submitting a form, so as you have it, PHP isn't seeing the form fields.

Input radio not sending data

I have a simple contact form, the html is this part:
<!-- CONTACT FORM -->
<form id="contact-form" name="contactform" class="row">
<!-- CONTACT FORM IMPUT -->
<div id="input_name" class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Il tuo nome">
</div>
<div id="input_email" class="col-md-12">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
</div>
<div id="input_subject" class="col-md-12">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Numero di telefono">
</div>
<div id="input_message" class="col-md-12">
<textarea class="form-control" name="message" id="message" rows="6" placeholder="Il tuo messaggio..."></textarea>
</div>
<div class="col-md-12 sinistra"><br>Quale servizio ti interessa?<br><br></div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="standard"> Standard<br>
</div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="avanzato"> Avanzato<br>
</div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="deluxe"> Deluxe<br>
</div>
<div class="col-md-12"><br></div>
<!-- CONTACT FORM SUBMIT BUTTON -->
<div id="form_btn" class="col-md-12">
<input type="submit" value="Invia" id="submit" class="btn btn-small btn-blue">
</div>
<!-- CONTACT FORM MESSAGE -->
<div class="col-md-12 contact-form-msg">
<span class="loading"></span>
</div>
</form>
with this php file
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$subject = $_REQUEST["subject"];
$msg = $_POST["msg"];
$tipologia = $_POST['tipologia'] ;
$to = "info#gmail.com";
if (isset($email) && isset($name) && isset($msg) ) {
$email_subject = "$name ha inviato una richiesta di ordine";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Da: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Da: $name<br/> Email: $email <br/> Telefono: $subject <br/>
Tipologia: $tipologia <br/> Messaggio: $msg";
$mail = mail($to, $email_subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
but I have a problem with radio input, the form works but doesn't pass the value of radio to email, so the email has the Tipologia without the radio selected for the form... What could be the error?
<form> defaults to a GET method if POST isn't implied.
You're using two POST arrays.
So... use a POST method and all POST arrays.
Either way, everything must match.
You should also check if the radio buttons are set or not, or any other you wish to include.

resetting form after php submit

I am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>
Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!

Cannot POST /.php file when submitting form for email

I'm trying to get a HTML from to work together with PHP in order to make a form for sending a mail, but after submitting the form, PHP file is saying
Cannot POST /quotation.php
Here is my HTML code
<div id="quotation" class="reveal-modal" data-reveal>
<a class="close-reveal-modal">×</a>
<h3>Request Quotation</h3>
<p>Please fill out this information and we will contact you as soon as possilble.</p>
<form method="post" name="" action="quotation.php">
<div class="row">
<div class="large-6 columns">
<label>Name
<input type="text" name="name" placeholder="Name" />
</label>
</div>
<div class="large-6 columns">
<label>Lastname
<input type="text" name="lastname" placeholder="Lastname" />
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Company name
<input type="text" name="company" value="" placeholder="Your company">
</label>
</div>
<div class="large-6 columns">
<label>E-mail
<input type="text" name="email" value="" placeholder="E-mail">
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Additional details
<textarea name="message" placeholder="Additional details here"></textarea>
</label>
</div>
</div>
<input class="button radius" type="submit" name='submit' value="submit">
</form>
</div>
And this is the PHP that runs the form
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = $visitor_email;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name, $lastname.\n".
"Here is the message:\n $message".
$to = "email#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
alert("yo");
//done. redirect to thank-you page.
header('Location: index.html');
?>
I added alert and echo for submit button, but none of them appears so looks like PHP is not even running.
It would be great if someone could guide me trough this problem or point out what am I doing wrong.

Categories