Using PHP to send this email form? [duplicate] - php

This question already has an answer here:
Making an Email Form Send Email
(1 answer)
Closed 9 years ago.
I have the following code for an email form from an HTML template. I'd like to make it send email, as the moment I think it doesn't and I need to add some PHP to process the form. Any tips on how to do that for this form?
Thank you!
<!-- Email -->
<article id="email" class="panel">
<header>
<h2>Email Me</h2>
</header>
<form action="#" method="post">
<div class="5grid">
<div class="row">
<div class="6u">
<input type="text" class="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" class="text" name="email" placeholder="Email" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="text" class="text" name="subject" placeholder="Subject" />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message" />
</div>
</div>
</div>
</form>
</article>

You should read http://php.net/manual/es/function.mail.php
so you get your data with $_POST and then treated data, to use something like this:
$message = wordwrap($message, 70, "\r\n");
mail('myMail#example.com', 'subject', $message);

Related

Submitting contact form data

The contact form has several text fields and several dropdowns with the ability to select multiple values. The question is how to send these values by e-mail? Text values of type string (one word at a time) are sent fine. But how to send arrays containing multiple values? How to collect them using the $_POST method and put them in a letter?
Form:
<section class="post-content-area pt-20">
<div class="container">
<div class="row">
<div class="col-lg-8 posts-list">
<div class="card card-signin my-5">
<div class="card-body">
<h5 class="card-title text-center" style="font-size: 26px">Test contact form</h5>
<form method="post" action="email-script.php" enctype="multipart/form-data" id="emailForm">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Name" >
<div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
</div>
<div class="form-group">
<input type="text" name="surname" id="surname" class="form-control" placeholder="Surame" >
<div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
</div>
<div class="form-group">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone" >
<div id="subjectError" style="color: red;font-size: 14px;display: none">subjectError</div>
</div>
<div class="form-group">
<label>First Level Category</label><br />
<select id="first_level" name="first_level[]" multiple class="form-control">
<?php
foreach($result as $row)
{
echo '<option value="'.$row["first_level_category_id"].'">'.$row["first_level_category_name"].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label>Second Level Category</label><br />
<select id="second_level" name="second_level[]" multiple class="form-control">
</select>
</div>
<div class="form-group">
<label>Third Level Category</label><br />
<select id="third_level" name="third_level[]" multiple class="form-control">
</select>
</div>
<div class="form-group">
<input type="file" name="attachment" id="attachment" class="form-control">
<div id="attachmentError" style="color: red;font-size: 14px;display: none">attachmentError</div>
</div>
<div class="submit">
<center><input type="submit" name="submit" onclick="return validateEmailSendForm();" class="btn btn-success" value="SUBMIT"></center>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
Emailing script:
https://pastebin.com/3SR67MUP
There are multiple ways on how to send them via email.
The easiest way is getting them on your form action page and sending them with the php mail() function.
To make the email look better you can create an HTML email template file.
Then get the file data with file_get_contents() and resetting strings with your collected data with the str_replace function and putting the data into your mail funtion.
Hope it helps!

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();?>

Form to Email PHP

Hi I am new to php and am not quite sure how to do this I am attempting to build a contact form that sends an email currently the HTML I Have for the form is:
<section class="mbr-section form1 cid-qL6aon4aVW" id="form1-11">
<div class="container">
<div class="row justify-content-center">
<div class="title col-12 col-lg-8">
<h2 class="mbr-section-title align-center pb-3 mbr-fonts-style display-2">
MAINTENANCE FORM
</h2>
<h3 class="mbr-section-subtitle align-center mbr-light pb-3 mbr-fonts-style display-5">
Request Maintenance for your unit.</h3>
</div>
</div>
</div>
<div class="container">
<div class="row justify-content-center">
<div class="media-container-column col-lg-8" data-form-type="formoid">
<form class="mbr-form" action="feedback.php" method="post">
<div class="row row-sm-offset">
<div class="col-md-4 multi-horizontal" data-for="name">
<div class="form-group">
<label class="form-control-label mbr-fonts-style display-7" for="name-form1-11">Full Name</label>
<input type="text" class="form-control" name="name" data-form-field="Name" required="" placeholder="Full Name" id="name-form1-11">
</div>
</div>
<div class="col-md-4 multi-horizontal" data-for="email">
<div class="form-group">
<label class="form-control-label mbr-fonts-style display-7" for="email-form1-11">Email Address</label>
<input type="email" class="form-control" name="email" data-form-field="Email" required="" placeholder="Email" id="email-form1-11">
</div>
</div>
<div class="col-md-4 multi-horizontal" data-for="phone">
<div class="form-group">
<label class="form-control-label mbr-fonts-style display-7" for="phone-form1-11">Phone Number</label>
<input type="tel" class="form-control" name="phone" data-form-field="Phone" placeholder="Phone #" id="phone-form1-11">
</div>
</div>
</div>
<div class="form-group" data-for="message">
<label class="form-control-label mbr-fonts-style display-7" for="message-form1-11">Request</label>
<textarea type="text" class="form-control" name="message" rows="7" data-form-field="Message" placeholder="Request" id="message-form1-11"></textarea>
</div>
<span class="input-group-btn">
<button href="" type="submit" class="btn btn-primary btn-form display-4">SEND FORM</button>
</span>
</form>
</div>
</div>
</div>
And the PHP I have is :
<?php
if (isset($_POST['submit'])){
$to="email#email.com";
$subject="Shovers.net Maintenance form submission";
$mail_from="from: ".$_POST['email']." \n";
$mail_from .="Content-Type: text/html; charset=utf-8 \n";
$message="<font size=\"2\" face=\"Verdana\"> Here is the message:<br />
".$_POST['comments']."<br />
".$_POST['name']."<br />
".$_POST['email']."</font>";
mail($mail_from,$to,$subject,$comments);
echo "Thanks!";
$TARGET = "/";
header("Location: $TARGET");
exit();
}else{
}
?>
I can't seem to get this to send to my email and I am not sure why on form submit it currently just goes to the feedback.php page which is blank as it is just the PHP script. Does this need to be running on the actual server for it to work or will it work with xampp and apache/mysql running? If not what do I need to do to get this working?
In addition I am also trying to get it to just reload/stay on that same page but pop up a verification message what am I doing wrong for that?
Any help appreciated!
Thanks!

Web page contact form in PHP not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
So I have this HTML code for my contact form:
<section id="contact-form">
<form action="form.php" method="post" enctype="multipart/form-data">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12">
<div class="block">
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Ime" name="name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Email Adresa" name="email">
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Predmet">
</div>
</form>
</div>
</div>
<div class="col-md-6 col-sm-12">
<div class="block">
<form>
<div class="form-group-2">
<textarea class="form-control" rows="3" placeholder="Poruka" name="message"></textarea>
</div>
<button class="btn btn-default" type="submit">Pošalji</button>
</form>
</div>
</div>
</div>
</div>
</form>
</section>
And my PHP script looks like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Kontakt s web-stranice';
$to = 'myEmailAddress';
$subject = 'Kontakt s web-stranice ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thank you for your email!</p>';
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
And for some reason when I click on the submit button the web page refreshes and the message isn't sent to my email.
Does anyone know why?
Your HTML format is using forms within forms, so everything outside that <form> tag is not being collected. All you need to do is have the first <form> tag and wrap all your input boxes and the submit button in it.
Notice in the code below how you only need one <form>.
<section id="contact-form">
<form action="form.php" method="post" enctype="multipart/form-data">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12">
<div class="block">
<div class="form-group">
<input type="text" class="form-control" placeholder="Ime" name="name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Email Adresa" name="email">
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Predmet">
</div>
</div>
</div>
<div class="col-md-6 col-sm-12">
<div class="block">
<div class="form-group-2">
<textarea class="form-control" rows="3" placeholder="Poruka" name="message"></textarea>
</div>
<button class="btn btn-default" type="submit">Pošalji</button>
</div>
</div>
</div>
</div>
</form>
</section>
Your PHP code is correct from the looks of things, but that messed up form tag was the big problem.

can someone help me with this contact validation form?

Ok I need to make this contact form highlight red and say not valid inside the boxes! What ever I do I can't get it to work! I can't change the HTML tags that are already there you can add to the HTML but not delete any of it but I want it to display on the same page with JavaScript and have it look very nice can someone edit what I have and help me make this work please!
Oh and I keep getting 2 emails every time I test it also!
Here is there HTML:
<!-- Contact -->
<div class="wrapper wrapper-style4">
<article id="contact">
<header>
<h2>Want to hire me? Get in touch!</h2>
<span>I do quality work and would love to work for you!.</span>
</header>
<div>
<div class="row">
<div class="12u">
<div id="errors"></div>
<form method='post' action='mailform.php' id='contact-form'>
<div>
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
Send Message
Clear Form
</div>
</div>
</div>
</form>
</div>
</div>
<!-- Contact -->
<div class="wrapper wrapper-style4">
<article id="contact">
<header>
<h2>Want to hire me? Get in touch!</h2>
<span>I do quality work and would love to work for you!.</span>
</header>
<div>
<div class="row">
<div class="12u">
<div id="errors"></div>
<form method='post' action='mailform.php' id='contact-form'>
<div>
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
Send Message
Clear Form
</div>
</div>
</div>
</form>
</div>
</div>
I need the JavaScript here:
<script>
</script>
And the PHP form is here:
<?php
$to = "bcw1995#gmail.com";
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$from = $_REQUEST['email'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail($_REQUEST['email'], $subject,
$message, "From:" . $email);
}
header("Location: http://www.thorbis.com/#contact");
?>
Can someone please fix this you will be a life saver!
You get two E-Mails:
Your mail is send twice, because you are calling two times the mail() function. Delete this line mail($to,$subject,$message,$headers);
// EDIT
change the second E-Mail function to this: mail($to, $subject, $message, "From:" . $email);
Else the sender will get the email that he has send.
Validation:
There are a lot of ways and librarys for a Javascript Form-Validation. I recommand jQuery + jQuery.Validation Plugin (http://jqueryvalidation.org/).
But i would also add a validation in the PHP file, because if somebody has disabled javascript, he can send empty emails.
I hope that i could help you.

Categories