Contact form is not sending the name - javascript

I have built this contact form for my website
but I am not getting the name in the email the contact form sends to me.
Also the contact form is super slow and I get the emails after a long delay of about 5-10 minutes every time I send a lead in the contact form.
Is this something I can fix?
I am using word press. Is it the problem/.
<div class="span4" id="fc">
<?php
$action = ( array_key_exists( 'action', $_REQUEST) ? $_REQUEST['action'] : "" );
if ($action=="") /* display the contact form */
{
?>
<h6 class="form_head">לפרטים / רכישה חייגו : 077-41-800-74 או מלאו את הטופס</h6>
<form class="cf" action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<div class="half right cf">
<input type="text" id="input-name" placeholder="שם*" name="name" required>
<input type="email" id="input-email" placeholder="אימייל" name="email" >
</div>
<div class="half left cf">
<input type="text" id="input-subject" placeholder="פלאפון*" name="message" required>
<input type="submit" value="קבל מידע נוסף" id="input-submit" name="submit">
</div>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($message==""))
{
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("xxx#gmail.com", $subject, $message, $from);
header('Location: http://newoffice.co.il/%D7%AA%D7%95%D7%93%D7%94-%D7%A9%D7%A4%D7%A0%D7%99%D7%AA-%D7%90%D7%9C%D7%99%D7%A0%D7%95/');
exit();
}
}
?>
</div>

Related

Save form details to google sheet

I am creating a business website having a form.. Currently my form details are sent to email.. I want that when form is submitted the details should be added in google sheet directly. because it is hard to check every mail. plz help.
HTML
<form method="post" name="contact_form" name="myForm" id="myForm" action="sendmail.php">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Name*" required />
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" name="phone" class="form-control" id="phone" placeholder="Mobile No.*" required pattern="[0-9]{3}[0-9]{3}[0-9]{4}">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<button type="" name="submit">Submit</button>
</div>
</div>
<div class="clear"></div>
</form>
pHp
<?PHP
$name = $_POST["name"];
//$lname = $_POST["lname"];
$phone = $_POST["phone"];
$to = "yourmail#gmail.com";
$subject = "New Email Address for project";
$headers = "From: $name\n";
$message = "This form has been submitted to project.\n
Name : $name;
Mobile No. : $phone";
mail($to,$subject,$message,$headers);
header('Location: http://websites.com/thank_you.html');
$thank_you = "Thank You For your interest. <br>We will Contact you soon";
echo "$thank_you .";
?>

Properly return value and print in html after a form submit

How do I return the result from a post in the original html?
For example in my form, I submit an email address. After the email address is submited I would like to show that email back in a div and hide the form.
How can I do that using angularjs and php?
simple HTML form
<form name="EmailForm" class="form-horizontal" method="post" action="mail_handler.php">
<div class="form-group form-group-lg">
<input id="email" type="email" name="email" class="form-control size" ng-model="email.text" placeholder="me#example.com" required>
</div>
<div class="form-group">
<label ng-show="EmailForm.email.$valid"><input ng-model="IAgree" name="tos" required="required" type="checkbox" /> I have read and agree to the Terms of Service</label>
</div>
<div class="form-group">
<button type="submit" name="submit" ng-show="IAgree" class="btn btn-lg btn-primary">
<span class="glyphicon glyphicon-play-circle" aria-hidden="true"></span>
Spam me now!
</button>
</div>
</form>
and a simple php
<?php
if(isset($_POST['submit'])){
$email = $_POST['email'];
header('Location: index.html');
}
?>
If you have
$email = $_POST['email'];
header('Location: index.html?email='.urlencode($email));
you can in index.html do
window.onload=function() {
var email = location.search?location.search.split("email=")[1]:"";
document.getElementById("emailId").innerHTML=decodeURIComponent(email);
}
You have two variants:
You should change the action of the form to "index.php" and put all the code for the form in index.php
2.In the other variant you will use everything you have done just change header('Location: index.html'); with header('Location: index.php/?email=' . urlencode($email));
And then in index.php:
$email = $_GET['email'];
echo '<div class="email">' . $email . '</div>';
You can write both html and php in single php file. i.e form_submit.php
After create php file you can write following code in form_submit file.
<form name="EmailForm" class="form-horizontal" method="post" action="mail_handler.php">
<div class="form-group form-group-lg">
<input id="email" type="email" name="email" class="form-control size" ng-model="email.text" placeholder="me#example.com" required>
</div>
<div class="form-group">
<label ng-show="EmailForm.email.$valid"><input ng-model="IAgree" name="tos" required="required" type="checkbox" /> I have read and agree to the Terms of Service</label>
</div>
<div class="form-group">
<button type="submit" name="submit" ng-show="IAgree" class="btn btn-lg btn-primary">
<span class="glyphicon glyphicon-play-circle" aria-hidden="true"></span>
Spam me now!
</button>
</div>
</form>
<?php
if(isset($_POST['submit']))
{
?>
<div>
<?php
$email = $_POST['email'];
?>
</div>
<style>
.form-horizontal{display:none}
<!-- above statement will hide form after submission of form -->
</style>
}
?>
After submit your form you will see email on same page in particular div.

problems resetting a form using php

I have a contact form which sends an email once submitted and displays an alert successfully when sent. My problem was 1) after its sent the email form still has content. 2) If I refresh the page the email is sent again with the previous details. In order to solve this I used the below code in php which is doing the job of refreshing with out sending an email.
header("Location: contact.php");
exit;
Now my problem is The alert is not displayed anymore which is true because the alert is in my HTML and header refreshes the page and it never reaches the alert in HTML(displayed using php echo). Here is the HTML Code :
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3" id="emailForm">
<h1 class="center">Email Form</h1>
<?php echo $result; //header( "Location: contact.php"); //exit; ?>
<p>Please get in touch ! I will get back to you as soon as I can !!</p>
<form method="post" id="contactForm">
<div class="form-group">
<label for="name">Your Name</label>
<input type="text" name="name" class="form-control" placeholder="Your Name" value="<?php echo $_POST['name'] ?>" />
</div>
<div class="form-group">
<label for="email">Your Email</label>
<input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $_POST['email'] ?>" />
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message">
<?php echo $_POST[ 'message'] ?>
</textarea>
</div>
<input type="submit" name="submit" id="send" class="btn btn-success btn-lg center" value="Submit" />
</form>
</div>
</div>
</div>
Here is the PHP code:
<?php
if($_POST['submit']) {
if(!$_POST['name']){
$error.="<br>Please enter your name";
}
if(!$_POST['email']){
$error.="<br>Please enter your email address";
}
if(!$_POST['message']){
$error.="<br>Please enter a message";
}
if ($_POST['email']!= "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error.="<br>Please enter a valid email address";
}
if( $error )
{
$result='<div class="alert alert-danger"><strong>There were error(s):'.$error.'</strong></div>';
}
else {
if(mail("madistackcloud#gmail.com","Message from Deepak", "Name: ".$_POST['name']."Email: ".$_POST['email']."Message: ".$_POST['message'])) {
$result='<div class="alert alert-success alert-dismissible"><strong>Thank you! I\'ll be in touch.</strong></div>';
}
else
{
$result='<div class="alert alert-danger alert-dismissible"><strong>Oops! Something went wrong. Please try again !</strong></div>';
}
}
}
?>
Is there any workaround for this or any other method which can do the job. Kindly help.
Change your form to
<form method="post" id="contactForm">
<div class="form-group">
<label for="name">Your Name</label>
<input type="text" name="name" class="form-control" placeholder="Your Name" value="" />
</div>
<div class="form-group">
<label for="email">Your Email</label>
<input type="email" name="email" class="form-control" placeholder="Your Email" value="" />
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message"></textarea>
</div>
<input type="submit" name="submit" id="send" class="btn btn-success btn-lg center" value="Submit" />
</form>
If you don't want to show the content sent, dont use echo $POST on the value of the inputs.
You're always echoing the post data in the html section. You only want to echo data if there is an error.
value="<?php echo $_POST['name'] ?>"
in the control code above do something like
$name = cleanInput($_POST['name']);
$email = cleanInput($_POST['name']);
// etc...
Then in your
if( $error )
...
else
{
$name = "";
$email = "";
sendMail();
// etc...
and then back in your html:
value="<?php echo $name; ?>"
Why do you do this: value="<?php echo $_POST['email']? This sets the value of the input field to what the user inputted. This means next time you load your page this value is visible and thus will be emailed when transmitted.

PHP Form to write to original HTML page

I have an HTML page with a form that posts to a sendmail.php file but when I click submit I get sent to the blank php page with the words "Email sent!".
I would like to click Submit and then instead of going into a separate page, it stays on the same page and the submit button changes color and reads "Message Sent". How do I do this?
HTML Code:
<form action="sendmail.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<div class="row half">
<div class="6u">
<input type="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="number" placeholder="Number" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>
PHP Code:
<?php
{
$name=$_REQUEST['name'];
$number=$_REQUEST['number'];
$message=$_REQUEST['message'];
$headers = "From: $name<form#site.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (($name=="")||($number=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$subject="Website Contact Form";
mail("me#gmail.com", $subject, "<font size='+1'>Hello, this is a message from your website's contact form.<br><br>This message was sent by <b>".$name.".</b><br>The phone number they provided was <b>".$number.".</b><br><br>Their message is as follows:<br><b>".$message."</b></font>", $headers);
echo "Email sent!";
}
}
?>
Thanks for the help!
get jquery library in your head section
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
then you want to write ajax method
sample:
$(document).ready(function(){
$('form').submit(function(e){
var arr;
e.preventDefault();
$.ajax({
type: 'POST',
url: "sendmail.php",
//this specifies the data to be sent to server through php you should modify accordingly
data:{name:$('input[name="name"]').val()},
async:true,
success: function(result){
//do something with the result if request is successful
}
});
});
});
Change action in form tag.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<div class="row half">
<div class="6u">
<input type="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="number" placeholder="Number" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>
<?php
if(isset($_REQUEST['name']) && ($_REQUEST['name']!="")|| isset($_REQUEST['number']) && ($_REQUEST['number']!="")|| isset($_REQUEST['message']) && ($_REQUEST['message']!=""))
{
$name=$_REQUEST['name'];
$number=$_REQUEST['number'];
$message=$_REQUEST['message'];
$headers = "From: $name<form#site.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (($name=="")||($number=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$subject="Website Contact Form";
mail("me#gmail.com", $subject, "<font size='+1'>Hello, this is a message from your website's contact form.<br><br>This message was sent by <b>".$name.".</b><br>The phone number they provided was <b>".$number.".</b><br><br>Their message is as follows:<br><b>".$message."</b></font>", $headers);
echo "Email sent!";
}
}
else{
print_r(22);exit;
}
?>
so what you need is jquery.form, it can help you submit the form by ajax.
You can use JQuery for this purpose.
<form id="noRedirection" enctype="multipart/form-data" method="POST">
<div class="row half">
<div class="6u">
<input type="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="number" placeholder="Number" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input id="submit" type="submit" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>
<script>
$('#noRedirection').submit(function() {
var post_data = $('#noRedirection').serialize();
$.post('sendMail.php', post_data, function(data) {
$("#submit").text('Message Sent').css("color","red");
});
});
</script>
All you need to learn is how to make ajax calls.
Refer this link from stackoverflow itself . Here you can see how all variables and of data regarding mail is sent through ajax call to another page for mailing.
Click Here
I hope this will help you. you no need to give an action. see my example it will stay same page but request will go another page. just copy and paste this code and don't run on localhost because email is not send on localhost:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'sendmail.php',
data: $('form').serialize(),
success: function (d) {
alert(d);
}
});
});
});
</script>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<div class="row half">
<div class="6u">
<input type="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" placeholder="Email" />
</div>
<div class="6u">
<input type="text" name="number" placeholder="Number" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" name="send" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>
make new page of sendmail.php and paste this code in that file:
echo $name = $_POST['name'];
echo $number = $_POST['number'];
echo $email = $_POST['email'];
echo $message = $_POST['message'];
$headers = "From: $name<form#site.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (($name=="")||($number=="")||($message=="") || ($email=="")) {
echo "All fields are required, please fill again.";
} else {
$subject="Website Contact Form";
mail($email, $subject, "<font size='+1'>Hello, this is a message from your website's contact form.<br><br>This message was sent by <b>".$name.".</b><br>The phone number they provided was <b>".$number.".</b><br><br>Their message is as follows:<br><b>".$message."</b></font>", $headers);
echo "Email sent!";
}
<form action="sendmail.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
You're already using php, so there's no reason at all to use jquery as well, just take the code from your sendmail.php file and put it in the top of this. Using javascript for core page functionality used to be considered bad practice, it's certainly annoying to users with javascript blockers running, makes the pages very hard to use, stackoverflow does that too.
Rather than submit to the other file, just delete the 'action="sendmail.php"' which will then make the page submit to itself, or put in the actual url of the page if it's got a static url that you know. Or use:
action="<?php echo $_SERVER['REQUEST_URI'];?>"
which sets it to whatever the uri of the page is, that's useful for things that generate the pages from query string data.
Then you check to see the following (how did stackoverflow get so popular without having good code blocks?)
if ( isset($_POST['action']) ) {
set color to red or whatever...
and that's all.
Another which is also help you:
if(isset($_POST['name']) && ($_POST['name']!="")|| isset($_POST['number']) && ($_POST['number']!="")|| isset($_POST['message']) && ($_POST['message']!="")){
$name=$_POST['name'];
$number=$_POST['number'];
$message=$_POST['message'];
$headers = "From: $name<form#site.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (($name=="")||($number=="")||($message=="")) {
echo "All fields are required, please fill the form again.";
} else{
$subject="Website Contact Form";
$to = 'admin#gmail.com';
mail($to, $subject, "<font size='+1'>Hello, this is a message from your website's contact form.<br><br>This message was sent by <b>".$name.".</b><br>The phone number they provided was <b>".$number.".</b><br><br>Their message is as follows:<br><b>".$message."</b></font>", $headers);
echo "Email sent!";
}
}
<form action="" method="POST">
<input type="hidden" name="action" value="submit">
<div class="row half">
<div class="6u">
<input type="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="number" placeholder="Number" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>

alert box in PHP and navigation to another page

I am using javascript to check whether username and password are not empty. If one of these is empty, javascript alert is displayed and PHP script should not work, that is username and password validation should not occur and login page should be displayed once again. Is there any simple code to do this?
Nobody need to build whole form. I have already build login form and PHP script for its validation, I just want to know is there any method or function in PHP to stop script on entering empty username/password and submitting
Try This
Php Code :
<p>
<label for="first">User Name:</label>
<input type="text" name="First Name" id="first" />
</p>
<p>
<label for="last">Password:</label>
<input type="text" name="Last Name" id="last" />
</p>
<input type="submit" name="Submit" id="button2" value="Submit" onClick="javascript:verify()"/>
JavaScript Code :
function verify() {
if (document.getElementById('first').value=="") {
alert("Please Enter Your Name");
return false;
}
else if (document.getElementById('last').value=="") {
alert("Please Enter Your Password");
return false;
}
else {
document.form.submit();
}
}
Working Model : http://jsfiddle.net/NWWL4/24/
It took me awhile to get this right. I have my form in my html index
page. On a closed question here, I read this couldn't be done.
This php works VERY well with my form.
It generates a Javascript alert for good and bad results and
links back to the index page in either instance.
If you look at the 2nd echo, you'll see where you can redirect to
another page. I just loaded same page to clear form.
If you want to see the ccs, just ask.
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "admin#blahblah.com";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$subject_field = $_POST['subject'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field:\n Subject: $subject_field\n Message: $message\n";
}
if(empty($name_field) || empty($email_field) || empty($subject_field) || empty($message)) {
echo '<script type="text/javascript">alert("There is a problem, please check the fields");window.history.go(-1);</script>';
}
else {
if(mail($to, $subject_field, $body)) {
echo '<script type="text/javascript">alert("Message successfully sent");window.location = "https://www.blahblah.com/";</script>';
}}
exit;
?>
html:
<div class="clear"></div>
<div class="container">
<div class="row">
<div class="col-md-3">
<form role="form" action="php\mailer.php" method="post" id="form">
<div class="form-group">
<input name="name" type="text" class="form-control" id="name" placeholder="Your Name" maxlength="30">
</div>
<div class="form-group">
<input name="email" type="text" class="form-control" id="email" placeholder="Your Email" maxlength="30">
</div>
<div class="form-group">
<input name="subject" type="text" class="form-control" id="subject" placeholder="Your Subject" maxlength="40">
</div>
<div><input type="submit" name="submit" class="btn btn-primary" value="Send Message"></input></div>
</div>
<div class="col-md-9">
<div class="txtarea">
<textarea name="message" rows="10" class="form-control" id="message"></textarea>
</form>
<div class="clear"></div>
<div class="col-lg-12" style="text-align:center">
<a class="btn btn-large btn-contact-link" href="#blahblah_home">Home</a>

Categories