PHP form validation - php

So I have created a form inside html the code -
<form method="post" action="hi.php">
<fieldset>
<label class="labelone" type="text" />
<input name="name" placeholder="Your Name"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
and this is the php -
<?php
$emailSubject = 'Test';
$webMaster = 'Test';
$name = $_GET['firstname'];
$email = $_GET['lastname'];
$message = $_GET['comment'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $comment <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<?php
?>
<?php
$emailSubject = 'Kent Pest Control Services';
$webMaster = 'xxxxxxxxxEDITED#gmail.com';
$name = $_GET['firstname'];
$email = $_GET['email'];
$phonenumber = $_GET['phonenumber']
$message = $_GET['comments'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Email: $email <br>
Phone Number: $phonenumber <br>
Message: $comments <br>
EOD;
When I upload it to my hosting and fill in the php form, and click submit, it goes to a blank screen, and I dont get an email, I have used this before like a year ago, I dont know if the code has changed since then, but I cant get it to work, I hope someone can help. :) I changed the email so people cant email me ;)

you are missing a <form> tag in your HTML.
try this:
<form action="yourPHPfile.php" method="post">
<fieldset>
<label class="labelone" type="text" />
<input name="name" placeholder="Your Name"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
I would use POST, and not GET as you are passing potentially a lot of data through and it would create a very ugly url.
your php file then..
<?php
$emailSubject = 'Test';
$webMaster = 'Test';
$name = $_POST['firstname'];
$email = $_POST['lastname'];
$message = $_POST['comments']; // you have the wrong key in your original file (comment)
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $comment <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
?>
other points:
you should be validating the email address server side if youre going to do anything else with it, like send back a confirmation etc

I think this code can resolve you query
<form action="actionname.php" method="post">
<fieldset>
<label class="labelone" type="text" />
<input name="firstname" placeholder="Your firstName"/>
<label class="labeltwo" type="text" />
<input name="lastname" placeholder="Your LastName"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
Now this php code
<?php
$emailSubject = 'Test';
$webMaster = $_POST['email']; /* Pass your form email address here */
$firstname = $_POST['firstname']; /* Firstname from form */
$lastname = $_POST['lastname']; /* Lastname from form */
$message = $_POST['comments']; /* Comment from form */
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $message <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
?>

Related

PHP form redirecting to PHP file itself

I'm trying to get a contact form working, and I'm being redirected to the PHP file itself, and I'm not sure why. I'm fairly new to PHP and I'm not entirely sure what I'm doing wrong. I'd love to be pointed in the right direction. The code is below.
Thanks
HTML:
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
PHP:
<?php
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
?>
This line tells the form what PHP file to send the data to:
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
In particular, it is this part that sends the data to a particular form:
action="form.php"
In fact, this is fine. You can process a form in the same PHP file that contains the form. Just put the PHP form processing at the top:
<?php
if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Just remove enctype="text/plain" otherwise your code is correct in HTML and try to echo feedback variable other way is to write all your code above the HTML like below:
<?php
if (isset($_POST['Email_Address'] && !empty['Email_Address']){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>

HTML contact form, PHP code

I was trying to send multiple vars from my html form to my email by using PHP. The problem is that I do not know anything about PHP. I found a simple form which worked for me, but it contained only 3 vars, subject, email and message. This time however I have 7.
Here's the code.
<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";
$subject = $projecttype;
$to = 'myemail#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $projectemail\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $body, $headers);
header('Location: thanks.html');
?>
And the corresponding HTML
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->
Edited the PHP, left out the body and put message in mail instead, still not working.
<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";
$subject = $projecttype;
$to = 'albermy145#alberttomasiak.be';
$headers = "From: $projectemail\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: thanks.html');
?>
Try this,
<?php
$to = 'myemail#gmail.com';
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$header = "From: $projectemail \r\n";
$subject = $projecttype;
$message = "
<html>
<head>
<title></title>
</head>
<body>
<p>Your HTML</p>
Project Name : $projectname
<br/>
Project Email : $projectemail
<br/>
Project Phone : $projectphone
...
...
...
...
</body>
</html>
";
$message .= "";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if($retval == true)
{
header('Location: thanks.html');
exit();
}
?>
}
HTML
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="projectname">
<label for="email">Email</label>
<input type="text" id="projectemail" name="projectemail">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="projectphone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="projectcompany">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="projecttype">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="projecttimeline">
<label for="message">Message</label>
<textarea name="aboutproject" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->
Add all variables into $body (which is the third param of mail function).
//$body = <<<HTML
//$message <br>
//$projectname <br>
//$projectcompany<br>
//...
//HTML;
Or the second way is to add these vars into the third mail param directly.
mail($to, $subject, $message, $headers);
// this code is updated as I wrote in my comment below.
the form posted the value of input is using 'name' attribute as identifier of value,
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
<!-- end form -->
<?php
$projectname = htmlentities($_POST['name']);
$projectemail = trim(strip_tags($_POST['email']));
$projectphone = htmlentities($_POST['phone']);
$projectcompany = htmlentities($_POST['company']);
$projecttype = trim(strip_tags($_POST['typeofproject']));
$projecttimeline = htmlentities($_POST['timeline']);
$aboutproject = htmlentities($_POST['message']);
?>

PHP Mail function didn't work

I have use php contact form for my web site.but it's doesn't work properly.when i fill all the field correctly and submit. it's display error message "Invalid data" .
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
$subject=$_REQUEST['name'];
$email=$_REQUEST['email'];
$body=$_REQUEST['message'];
}else{
$to = "abc#abc.com";
$subject = $subject;
$from = $email;
$message = $body;
if (($from=="")||($subject=="")||($message==""))
{
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
} else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
?>
Like this it works:
<?php
$action=$_REQUEST['action'];
if ($action=="") {?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
}
else {
$to = "...mail address...";
$subject = $_REQUEST['name'];
$from = $_REQUEST['email'];
$message = $_REQUEST['message'];
if (($from=="")||($subject=="")||($message=="")) {
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
}
else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
}
?>
Noticed a flaw in your logics - you try to set $from as an $email, while you don't get a variable $email
in else statement add this lines:
$from = $_REQUEST['email'];
Plus if mail still is not sending you should considerthat some mail servers, such as qmail, will reject your message if it uses \r\n.
So you should try to use just \n or \n\n as a linebreak in a header.

Send HTML Form data Via PHP

can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>

how to add an attachment to a php contact form

I have made a contact form that allows users to send me messages, but what I couldn't add is the ability for them to send me a file with it. Here is my code:
<form action="mail.php" method="post" enctype='multipart/form-data'>
<input type="text" class="feedback-input" id="firstname" name="firstname" placeholder="First Name" size="30" required="">
<br/>
<input type="text" class="feedback-input" id="lastname" name="lastname" placeholder="Last Name" size="30" required="">
<br/>
<input type="email" class="feedback-input" id="title" name="title" placeholder="E-mail" size="30" required="">
<br/>
<textarea name="message" class="feedback-input" placeholder="What can I help you with?" style="height: 150px;" required=""></textarea><br/>
<br/>
<input type="file" name="file" id="file" placeholder=" " tabindex="1" required/><br/>
<input type="submit" name="submit" id="Submit" value="Send">
and this is the mail.phph file:
<?php
$firstname = $_POST['firstname'];
$title = $_POST['title'];
$lastname = $_POST['lastname'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$formcontent=" From: $firstname $lastname \n Email Address: $title \n IP: $ip \n\n Description : $message";
$recipient = "myemail#yahoo.com";
$subject = "New Message!!";
$mailheader = "From: $title \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: www.mythankyoupage.com");
die();
?>

Categories