Form php not working - php

I have gone searched the net and tried many ways of creating and validating a form which sends info to an email but I can't get my head around it. This is what I have done and it seems to work (have not been able to test if it sends), but every time I submit it comes up with "all fields required error" even with all boxes filled/empty. I looked on this site through similar questions and tried to fix it by them but no luck.
What am I missing?
Should I use the php on another page?
Once the error is fixed, will it send and keep the information submitted by the user?
In case it helps - I'm using foundation 5 for the site. Beginner at PHP.
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="post">
<div class="row">
<div class="large-12 columns">
<input type="hidden" name="action" value="submit">
<label>Name
<input name"name" type="text" placeholder="Your name"/>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Email
<input name"email" type="text" placeholder="Your email" />
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Your Message
<textarea name="message" type="text" placeholder="Comment here"/></textarea>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<input id="submit" type="submit" value="Send Message">
</div>
</div>
</form>
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields required! Please fill in the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Hire";
mail("myemail", $subject, $message, $from);
// back to homepage - will add send confirm when fixed. header( "Location: ");
}
}
?>
Thanks in advance for any help!

You're missing = to assign value for your name attribute:
<input name="name" type="text" placeholder="Your name"/>
<!------- ^ here
and:
<input name="email" type="text" placeholder="Your email" />
<!-------- ^ and here

You are giving input attribute name for name and email like this :
<input name"name" type="text" placeholder="Your name"/>
<input name"email" type="text" placeholder="Your email" />
Change it to
<input name="name" type="text" placeholder="Your name"/>
<input name="email" type="text" placeholder="Your email" />

check this ..Add= to email and name
<input name="name" type="text" placeholder="Your name"/>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Email
<input name="email" type="text" placeholder="Your email" />
</label>

Related

Run PHP file (PHPMailer to send email through Gmail) in the background and alert on the same page when the email is sent

I am using PHPMailer to send email from my Gmail account to an email from a submitted form on my index.html page. This works perfectly fine. But since I am using the send.php file in the action attribute, the send.php page appears after form submission. I want to stay on the index.html page and let send.php work in the background and alert on the index.html page, when the email is sent.
Note: There isn't any SQL or any database job here. I am using PHPMailer in send.php to send an email from my Gmail account to the user submitted Gmail id in the form.
<form action="send.php" class="form" id="frm-js" method="POST">
<div class="form__group">
<input type="text" class="form__input" id="name" name="name" placeholder="Full Name" required>
<label for="name" class="form__label">Full Name</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="email" name="email" placeholder="Email Address" required>
<label for="email" class="form__label">Email Address</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="phone" name="phone" placeholder="Phone Number">
<label for="phone" class="form__label">Phone Number</label>
</div>
<div class="form__group">
<textarea id="message" rows="4" cols="50" name="message" class="textarea-frm form__input" maxlength="1000" placeholder="Your message"></textarea>
</div>
<div class="form__group">
<button class="btn" type="submit" name="submit">Book</button>
</div>
</form>
You can use jQuery. Try this: remove action from the form tag. I add a p to show the message from send.php:
<form class="form" id="frm-js" method="POST">
<p id="resp_msg"></p>
<div class="form__group">
<input type="text" class="form__input" id="name" name="name" placeholder="Full Name" required>
<label for="name" class="form__label">Full Name</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="email" name="email" placeholder="Email Address" required>
<label for="email" class="form__label">Email Address</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="phone" name="phone" placeholder="Phone Number">
<label for="phone" class="form__label">Phone Number</label>
</div>
<div class="form__group">
<textarea id="message" rows="4" cols="50" name="message" class="textarea-frm form__input" maxlength="1000" placeholder="Your message"></textarea>
</div>
<div class="form__group">
<button class="btn" type="submit" name="submit">Book</button>
</div>
</form>
And add a jQuery function (integrate jQuery if you haven't already library):
$("#frm-js").on("submit", function(event){
event.preventDefault();
var data = $( this ).serialize();
$.post("send.php",{data:data }, function (data) {
// Do other stuff like
// reset form items
// Show message response
$("#resp_msg").text(data).show();
});
});
This is send.php and is how it should be:
if(isset($_POST['data'])) {
$params = array();
parse_str($_POST['data'], $params);
// Send mail
echo "Hi, " . $params['name'] . " your email has been sent";
}

Using php with two different contact forms on a website

I have two contacts forms on my website and have set up some php to send the data to a specified email address. I want to use the same php on both forms which are on different web pages. It is working on one page but not the other. I haven't used php before so am wondering if I am missing something glaring obvious, any help much appreciated. Here is my php:
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['emailaddress'];
$phone=$_POST['phone'];
$msg=$_POST['message'];
$to='example#hotmail.com';
$subject='Inquiry from website';
$message= "Name: ".$name."\n".
"Phone number: ".$phone."\n".
"Email address: ".$email."\n".
"The following inquiry has been made :"."\n\n".$msg;
if(mail($to, $subject, $message)){
echo "<h1>Message sent successfully. Thank you"." ".$name.", we will contact you as soon as possible.</h1>";
}
else{
echo "Something went wrong!";
}
}
?>
I have added my html into the post:
<form action="mail.php" method="POST">
<div class="form-group">
<input type="text" name="name" required class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="emailaddress" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="phone" required class="form-control" placeholder="Phone number">
</div>
<div class="form-group">
<textarea name="message" id="message" required class="form-control" placeholder="Message"
cols="30" rows="5"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary px-5" value="Send Message">
</div>
</form>
where is your html
please send your html form

All fields are not displaying in PHP $_POST as well as $_REQUEST

I have a form
<form
method="post"
class="c-form"
action="mail.php"
id="contactform"
>
<div class="row">
<div class="col-lg-12">
<div class="error-alert"></div>
</div>
<div class="col-lg-12">
<input
type="text"
id="subject" name="subject"value="<?php echo $deviceType['device_type_name']; ?> Repair"
/>
</div>
<div class="col-lg-12">
<input
type="text"
placeholder="NAME"
id="name"
name="name"
/>
</div>
<div class="col-lg-12">
<input
type="text"
placeholder="EMAIL"
id="email"
name="email"
/>
</div>
<div class="col-lg-12">
<input
type="text"
placeholder="PHONE NUMBER"
name="phone"
/>
</div>
<div class="col-lg-12">
<textarea
rows="5"
placeholder="YOUR MESSAGE"
id="message"
name="message"
></textarea>
</div>
<div class="col-lg-12">
<input
type="submit"
class="submit"
value="Contact Us"
id="submit"
/>
<img
src="images/ajax-loader.gif"
class="loader"
alt=""
/>
</div>
</div>
</form>
But when I submit the form two fields (subject and message) isn't showing in the $_POST or $_REQUEST array (other 3 fields name, phone and email is present in the array). (the variable $deviceType['device_type_name']; has value, so there is no issue there)
Can anyone please tell me what am I missing here.
mail.php file (nothing much here just for debugging):
<?php
print_r($_POST);
die();?>

HTML5 form, email not submitting

So i've got this assignment at school to make a web page to a company.
I have been using desingmodo's slides template. There is a guide to make a working form but mine isn't sending the email to specified address, but i do get the "success" message after submitting.
send_mail.php
<?php
$toEmail = "example#gmail.com"; ***i have the correct email here on my .php***
$mailHeaders = "From: ".$_POST["userName"]." <". $_POST["userEmail"] .">\r\n";
$message_content = "Subject: Contact Form Message\r\n
From: ".$_POST["userName"]." ".$_POST["userEmail"]."\r\n
Message: ".$_POST["content"]."";
if(mail($toEmail, 'Contact Form Message', $message_content, $mailHeaders)) {
print "<p class='success'>Message Sent. Thank You!</p>";
} else {
print "<p class='Error'>Problem in Sending Mail.</p>";
}
?>
HTML form
<form class="wide center" action="send_mail.php" id="contact-form" method="post" novalidate="novalidate">
<label class="uppercase ae-4" for="name37">Your name</label>
<input class="stroke round ae-5 wide" id="name" name="name" type="text" placeholder="Name" required/>
<span id="userName-info" class="info"></span>
<label class="uppercase ae-6" for="email37">Email</label>
<input class="stroke round ae-7 wide" id="email" type="email" name="email" placeholder="Email" required/>
<span id="userEmail-info" class="info"></span>
<label class="uppercase ae-8" for="message37">Message</label>
<textarea class="stroke round left ae-9 sourceSans" id="message37" name="message" placeholder="Message" required></textarea>
<span id="content-info" class="info"></span>
<input class="button wide pink round uppercase ae-10 button-55 done" type="submit" name="submit" value="Send message">
im assuming the val_submit.js isnt causing any problems since im getting the "message sent" page.
Any ideas what im missing?
Replace your form with this it will run
Check with name you used in php should be same in form input names
<form class="wide center" action="send_mail.php" id="contact-form" method="post" novalidate="novalidate">
<label class="uppercase ae-4" for="name37">Your name</label>
<input class="stroke round ae-5 wide" id="name" name="userName" type="text" placeholder="Name" required/>
<span id="userName-info" class="info"></span>
<label class="uppercase ae-6" for="email37">Email</label>
<input class="stroke round ae-7 wide" id="email" type="email" name="userEmail" placeholder="Email" required/>
<span id="userEmail-info" class="info"></span>
<label class="uppercase ae-8" for="message37">Message</label>
<textarea class="stroke round left ae-9 sourceSans" id="message37" name="content" placeholder="Message" required></textarea>
<span id="content-info" class="info"></span>
<input class="button wide pink round uppercase ae-10 button-55 done" type="submit" name="submit" value="Send message">

How to send email with contact form on Wordpress page

I am trying to get a simple contact form to work with my wordpress site but cannot get it to work. I dont want to use any plugin, I just want to do it using PHP. So I put this file in root dir of my wordpress installation on my hostgator hosted site
www.example.com/sendmail.php
<?php
if(isset($_POST['submit'])){
$to = "myemail#yahoo.com";
$from= $_POST['email'];
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$message= $_POST['message'];
$subject = "Request email";
$headers = "From:" .$from;
mail($to,$subject,$message,$headers);
}
?>
This is the contact-us form on www.example.com/contact-us
<form action="http://www.example.com/sendmail.php" method="post">
<fieldset>
<div class="form-group">
<div class="col-md-12">
<input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<textarea class="form-control" id="message" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<!-- <button type="submit" class="btn btn-primary btn-lg">Submit</button>-->
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Submit">
</div>
</div>
</fieldset>
</form>
If i try to visit this page www.example.com/sendmail.php, it gives 200 success ok message. However if I try to fill the form and then it sends the email, i am not able to recieve it.
Is there anything I am missing or I need to check ?
You are getting these variables $fname= $_POST['fname']; $lname= $_POST['lname']; on sendmail.php
But your html form input fields does not have name for fname and lname .
So use the below one :
<div class="form-group">
<div class="col-md-12">
<input id="fname" name="fname" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
Instead of :
<div class="form-group">
<div class="col-md-12">
<input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
Hope it helps you.

Categories