Send HTML Form data Via PHP - 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>

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
}
?>

PHP form validation

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
?>

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']);
?>

Can someone find where's wrong in this code PHP email

I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.

php mailer with file upload

I am trying to have a php mailer code attach a file along with the regular information. I don't know what to do. Here is how far I have come.
Form.html
<form method="post" name="contact" action="contactprocessor.php">
<label for="name">Name: </label> <input type="text" id="name">
<label for="email">email: </label> <input type="email" id="email">
<label for="file">Upload file: </label> <input type="file" id="file">
<input type="submit" id="name" value="upload">
contactprocessor.php
<?php
$emailSubject = 'file upload';
$mailto ='me#mymail.com';
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$body = <<<EOD
NAME: $name <br>
Email: $email <br>
EOD;
$headers = "from: $email\r\n";
$success = mail($mailto, $emailSubject, $body, $headers);
?>
Unfortunately, you can't send file with email directly. I recommend to use phpmailer class (http://code.google.com/a/apache-extras.org/p/phpmailer/) but also there is a traditional way: http://webcheatsheet.com/php/send_email_text_html_attachment.php
first off you need to add enctype="multipart/form-data" to your
they're right, there are tutorials to do such a thing. They usually involve multiparts and base64

Categories