I have a document with 2 textboxes and 1 submit button. I want to send an email when I press the submit button and in it should be the value of the textboxes. My PHP knowledge is a bit dusty. I don't want to bother the user with the emailproces, so he can't see this and should just be redirected.
<html>
<body>
<div class="section_form" id="usernameSection">
<label for="username">Login:</label>
<input size="20" type="text" name="username" id="username" />
</div>
<div class="section_form" id="emailSection">
<label for="email">Email:</label>
<input size="20" type="text" id="email" name="email" maxlength="20"/>
</div>
<div id="submit_button">
<button type="submit" value="Submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button>
</div>
</body>
</html>
Many thanks!
Use the PHP mail function - listed here.
That should be able to help.
Basically the core of what you're looking for is
<?php
// Check that all fields are present, construct the message body, etc
mail($to, $subject, $body);
header("Location: wherever.php");
exit();
?>
See the mail function in PHP documentation. (Also, thank you Alex for the exit() reminder.)
Use this code for your task.
<?php
if(isset($_POST['submit'])){
extract($_POST);
$message = "username : $username ; email : $email";
$to = "your#email.com";
$subject = "Email Subject";
mail($to, $subject, $message);
}
?>
<html>
<body>
<form method="POST" name="form1">
<div class="section_form" id="usernameSection">
<label for="username">Login:</label>
<input size="20" type="text" name="username" id="username" />
</div>
<div class="section_form" id="emailSection">
<label for="email">Email:</label>
<input size="20" type="text" id="email" name="email" maxlength="20"/>
</div>
<div id="submit_button">
<button type="submit" value="Submit" name="submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button>
</div>
</form>
</body>
</html>
Start with:
- $_POST
- mail()
Also you need to check: Forms in HTML, you might want to use form to send the data of input fields to server.
Related
I'm learning PHP and working on some basic functions. I was wondering why my send message function doesn't work.
I have 2 PHP files:
One to process the login and
One to process the message.
Any advice?
index.php
<form name="admin" method="POST" action="process.php">
<div>
<label for="login">Login: </label>
<input type="text" name="login" placeholder="Login"/>
</div>
<div>
<label for="password">Password: </label>
<input type="text" name="password" placeholder="Password"/>
</div>
<div>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<br>
<form name="sendmail" method="POST" action="sendmail.php">
<div>
<p><b>Send me a message:</b></p>
<textarea name="message"></textarea>
</div>
<div><input type="submit" name="submit" value="Send"></div>
</div>
</form>
sendmail.php
<?php
$message = '';
foreach($_POST as $name=>$value){
if('submit'!=$name){
if(is_array($value)){
/* SYNTAX: implode(separator,array) -
converts an array of values as a string. */
$value=implode(', ', $value);
}
$message .= ucfirst($name) ."is $value.\n\n";
}
}
$to = 'testemail#test.org';
$subject = 'Form submission from webapp';
if (mail($to, $subject, $message)){
echo "Your message has been sent.";
};
?>
From the PHP manua:
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
https://www.php.net/manual/function.mail.php
I've been learning PHP for about two days now. So I'm really new to PHP. I have tried my best to understand but no matter what I write, my code never works. I got it to send an email successfully one time. But I'm not able to get it to send the contents in my form.
Here is my HTML form.
<!-- FORM BEGINS -->
<form action="contact-form-handler.php" method="post">
<!-- Full Name -->
<div class="formWrapper ">
<label name="firstname" for="firstName"><span>*</span>First Name</label>
<input type="text" name="memberFirstName" id="memberFirstName" onkeyup="activateButton()" class="inputText" tabindex="1" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
</div>
<p class="fieldsetDivider"></p>
<div class="formWrapper">
<label for="lastName"><span>*</span>Last Name</label>
<input type="text" name="memberLastName" id="memberLastName" onkeyup="activateButton()" class="inputText" tabindex="2" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
</div>
<p class="fieldsetDivider"></p>
<!-- Date of Birth-->
<p class="fieldsetDivider"></p>
<div class="formWrapper smallInput">
<label for="firstInput"><span>*</span>Date of Birth
<div class="labelClarify">(MM/DD/YYYY)</div>
</label>
<div class="inlineInput">
<input type="text" name="month" id="month" maxlength="2" onkeyup="activateButton();autotab(this, 'day')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="MM" required="required"/>
<span>/</span>
</div>
<div class="inlineInput">
<input type="text" name="day" id="day" maxlength="2" onkeyup="activateButton();autotab(this, 'year')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="DD" required="required"/>
<span>/</span>
</div>
<div class="inlineInput">
<input type="text" name="year" id="year" maxlength="4" onkeyup="activateButton()" onkeypress="return isNumberKey(event)" class="inputText" placeholder="YYYY" required="required"/>
</div>
<a rel="Please enter your date of birth" class="formHelp tooltip-link" href="#">
<img src="../../images/site3/common/form_icon_help.png">
</a>
</div>
<p class="fieldsetDivider"></p>
<!-- Member ID -->
<div class="formWrapper">
<label for="memberID"><span>*</span>Member ID</label>
<div class="inlineInput">
<input type="text" name="memberID" id="memberID" class="inputText" onkeyup="activateButton()" tabindex="3" maxlength="9" onkeypress="return isNumberKey(event)" required="required"/>
</div>
<a rel="Please enter your member ID" class="formHelp tooltip-link" href="#">
<img src="../../images/site3/common/form_icon_help.png">
</a>
</div>
<p class="fieldsetDivider"></p>
<!-- Zip Code -->
<div class="formWrapper">
<label for="zipCode"><span class="requiredStar">*</span>Zip Code</label>
<div class="inlineInput">
<input type="text" name="memberZipCode" id="memberZipCode" onkeyup="activateButton()" tabindex="4" class="inputText" maxlength="5" onkeypress="return isNumberKey(event)"/>
</div>
</div>
<p class="fieldsetDivider"></p>
<!-- Button -->
<div class="button">
<button class="greenBtnLarge" type="submit" style="text-decoration:none;"><span>Next<span></button>
</div>
</form>
<!-- FORM ENDS -->
Can someone please help me with a php code that would work for this form? I have found a couple template forms on the internet and I've spent hours trying to modify them to work with my form. But no luck.
And I can get the PHP and form to work together and make it send me an email from my server, so there's no problem with the server. An email will send. I just can't get the PHP code to send the contents of the form. It sends the email, but with no form details. Understand?
I really, really need help with this. Can you guys help me create a PHP code that will work with my HTML form I posted in this thread? I really need to get past this step in this project.
Thank you!
UPDATE!
Here is PHP template I found on the internet, but I don't know how to really modify it properly.... please help?
<?php
$errors = '';
$myemail = 'example#example.com';//<-----Put Your email address here.
if(empty($_POST['firstname']) ||
empty($_POST['lastname']) ||
empty($_POST['dob']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['firstname'];
$email_address = $_POST['lastname'];
$message = $_POST['dob'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
You have a several code errors...
Remove the <label> tags from your titles for <input type="text">... they are not needed.
Make sure your form field name matches your email code $_POST['something'] for example
You have <input type="text" name="memberFirstName" id="memberFirstName" /> in your form... BUT
you have $_POST['firstname'] in your email code. Change $_POST to memberFirstName OR change your <input name and id to firstname
The same changes need to be made to all elements so they match precisely.
AND remember... PHP is case sensitive.
The HTML form fields are not the same with the fields in php email handler
Ex.
The input name should be same as what the handler should get
Since
<input type="text" name="memberFirstName" required="required"/>
The index of $_POST should be same memberFirstName
$_POST['memberFirstName']
This should be applied to all fields
It seems the HTML form is not related to the php handler at all.
Its better to use this form for testing instead
<form action="contact-form-handler.php" method="POST">
First Name: <input name="firstname" type="text"><br>
Last Name:<input name="lastname" type="text"><br>
DOB: <input name="dob" type="text">
<button type="submit">Send</button>
</form>
Note:
If you are using wamp or xampp local server you need to check if the email service is configured correctly otherwise no email will be sent
Check this for reference:
How to configure XAMPP to send mail from localhost?
When I am filling out the contact form on the website that I am making, the e-mail will be sent, but I am not receiving it in the inbox of my computer.
The code looks like this:
HTML:
<div id="form">
<form action="mailto:psteintj#xs4all.nl" id="contactForm" method="post">
<span></span>
<input type="text" name="name" class="name" placeholder="Enter your name" tabindex=1 />
<span></span>
<input type="text" name="email" class="email" placeholder="Enter your email" tabindex=2 />
<span id="captcha"></span>
<input type="text" name="captcha" class="captcha" maxlength="4" size="4" placeholder="Enter captcha code" tabindex=3 />
<span></span>
<textarea class="message" placeholder="Enter your message" tabindex=4></textarea>
<input type="submit" name="submit" value="Send e-mail" class="submit" tabindex=5>
</form>
</div>
JS:
if ((captchaVal == captchaCode) && (emailFilter.test(emailText)) && (nameFilter.test(nameText)) && (messageText > 50)) {
$.post("mail.php", {
name: $(".name").val(),
email: $(".email").val(),
message:$(".message").val()
});
$("#contactForm").css("display", "none");
$("#form").append("<h2>Message sent!</h2>");
return false;
}
and PHP:
<?php
$name = $POST['name'];
$email = $_POST['email'];
$message = $POST['message'];
Could someone tell me where I am going wrong?
Well, you are not sending any emails (or at least you haven't posted any code about it), so of course you are not receiving any emails. You should configure the mailing, and use the mail function.
The function needs a working SMTP server to actually send out the e-mail.
Your PHP has no mail() call or similar? I'm probably missing something here.
Script1 is based on CSS and html design(with some texture effect) I tried to setup php form action script to a html/css form however it keeps refreshing to the home page.. according to script if successful then it will refresh to thankyou.php, but it is not success.
However script2, I don't use any css, it is very basic html but it is working fine! but since my whole website design have css effect, I want the form also needs to have some css work.
Could someone please help me with script1? if it not possible then could you please suggest me an action script for below form, currently I don't have any action.php script, I tried few in online, unfortunately without success.
CSS form script:
<div class="row add-bottom-main">
<form name="myform" id="contactForm" action="" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
</div>
Script 1
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" enctype="multipart/form-data" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#gmail.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
SCRIPT 2
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" 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"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#capital-placement.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
Your "css form" as posted has no action set on the form and no PHP to process the page in the event that it's submitted. Your two other forms both have PHP that is testing to see if the form has been submitted and process it when it has. The PHP on those two pages is doing the work of processing the form. Without it, they would also not do anything when submit.
It sends the email, but without a body, subject or anything. This is what I get:
HTML:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" class="text" /></label>
<label for="message">Message:<textarea id="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
PHP (mail.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$headers = "From: " . $email;
mail($recipient, $subject, $message, $headers);
echo "Thanks! The message was sent. :)";
?>
Any insight? Thanks so much.
Forms fields are based on their names not their ids.
replace your :
id="(...)"
by
id="(...)" name="(...)"
You should have a look to Swift Mailer which is strongly safer than your current method.
You need to have a name attribute on your HTML input fields. That's what the form uses to create the indexes in the $_POST array. Corrected HTML is below
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
you need to give name attribute for each html element . Use below html code:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
Into my server works, are you sure that in $_POST are set?
If you can't send email assicure you can try to set SMTP to send your email
Try to use something like phpmailer to send email.
In your form for every field you have to put:
- id="example_id"
- name="example_name"