Passing Session Variables Through PHP Mail - php

I'm having an issue passing $_SESSION variables through a multiple page form process using PHP mail. The emails are coming through fine, although they not displaying the variables.
My goal is to have users fill out forms on multiple pages, and have the data emailed back to me.
Page 1
<?php
session_start();
?>
<form method="post" action="submitpage.php">
<label>
<input type="radio" name="vehicle_type" value="car" checked />
<img class="img-responsive" src="img/vehicle2.png">
</label>
<label>
<input type="radio" name="vehicle_type" value="suv" />
<img class="img-responsive" src="img/vehicle2.png">
</label>
<label>
<input type="radio" name="vehicle_type" value="van" />
<img class="img-responsive" src="img/vehicle2.png">
</label>
<label>
<input type="radio" name="vehicle_type" value="truck" />
<img class="img-responsive" src="img/vehicle2.png">
</label>
<label>
<input type="radio" name="vehicle_type" value="none" />
<img class="img-responsive" src="img/vehicle2.png">
</label>
</form>
Page 2
<?php
session_start();
$_SESSION['vehicle_type'] = $_POST['vehicle_type'];
?>
<form method="post" action="emailexample.php" id="submit-form">
<input type="Email" name="email">
<input type="submit" name="submit" value="Submit" id="submitbtn">
</form>
Page 3
<?php
session_start();
$to = 'myemail#gmail.com';
$subject = 'test ';
$message = "Your Vehicle Type is: " . $_POST['vehicle_type'] ."\r\n";
$headers = 'From: email#example.com' . "\r\n" .
mail($to, $subject, $message, $headers);
?>

On page 1 you should put submit button something like this
<input type="submit" name="submit" value="Submit" />
On page 3 you should change
$message = "Your Vehicle Type is: " . $_SESSION['vehicle_type'] ."\r\n";
and you're missing semicolon in $headers variable at the end. It should be
$headers = 'From: email#example.com' . "\r\n";

In page 3
use $_SESSION['vehicle_type'] instead of $_POST['vehicle_type']

Related

Don't know what to write in my message variable?

I am a PHP noob, so i don't know what to write in my message variable. I need my data from the form to be sent to the email. I got a textarea, 2 inputs (name, email). Want the text from input to be sent to my email. Here code i have:
<?
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'rayetzkiillya#gmail.com';
$subject = 'Обратный звонок';
$message;
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Отправитель <from#example.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>
<form action="send.php" class="postcard" method="post">
<span>Your message:</span>
<textarea type="text" value="" required></textarea>
<div id="stripe1"></div>
<div id="stripe2"></div>
<img src="./images/Seal_of_the_United_States_Department_of_the_Post_Office.svg. png" alt="Oops" id="seal" />
<img src="./images/stamp.jpg" alt="Stamp" id="stamp" />
<div class="inputs">
<div class="inputs" id="input1"><label for="to" type="text" id="to">to: </label> <input type="text" value=" Me" readonly><div id="stripe3"></div></div>
<div class="inputs" id="input2"><label for="from" type="text" id="from">from: </label> <input type="text" id="input2"><div id="stripe3"></div></div>
<div class="inputs" id="input3"><label for="email" type="text" id="email">email: </label> <input type="text" id="input3"><div id="stripe3"></div></div>
<button type="button" class="btn btn-primary" id="send_button">Send</button>
</form>
You can make your message an HTML string, or just plain text.
For plain text, you can do something like
$message .= "name: ".$_POST['name']."\r\n";
$message .= "Message: ".$_POST["theMessage"];
However you need to name your inputs in the HTML as well,
so
<input type="text" id="input2" name="name">
<textarea type="text" value="" required name="theMessage"></textarea>
Or similar.
For the from address, you have to use change your header line
change
$headers .= "From: Отправитель <from#example.com>\r\n";
to
$headers .= "From: ".$_POST["name"]." <".$_POST["from"].">\r\n";
And obviously name your input accordingly:
<input type="text" id="input3" name="from">
Also, as suggested by others, you should be sanitizing/validating these values before using them.

Contact form not working while click on submit button

I was working with an HTML contact form, its action is a mail sending php script. But it is not working when i am clicking on the send button. I couldn't find any possible here. Please someone help me to fix this.
HTML form
<div class="contact-form">
<form action="sendmail.php" method="post">
<div class="control-group"><label class="nameLabel" for="name">Name</label> <input id="name" name="name" type="text" /></div>
<div class="control-group"><label class="emailLabel" for="email">Email</label> <input id="email" name="email" type="text" /></div>
<div class="control-group"><label class="messageLabel" for="message">Message</label><textarea id="message" name="message"></textarea></div>
<div class="control-group"><button type="submit">Send</button></div>
</form>
<h1 class="status-message-contact"></h1>
</div>
PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="sarath.sarigama#gmail.com";
$subject="WEB Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../index.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../index.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
you dont have submit button name ,change name of the button
<button name='submit' type="submit">Send</button>
or use input tag
<input name='submit' type='submit'>
Replace
<button type="submit">Send</button>
with
<input type="submit" value="Send" />
And maybe add
<input type="hidden" name="submit" value="1" />
For the isset( $_POST['submit'] )

php form with radio buttons

I'm trying to get the following form to work. I want to be able to send the information from the text fields and radio buttons via email when the user submits the form. I also want the page to redirect to a 'thankyou' page after the form submits. As it currently stands, the page redirects but no email is sent. Here is what I currently have:
HTML:
Name: <input type="text" id="name" name="name" />
Date: <input type="text" id="date" name="date" />
Job description: <input type="text" id="job" name="job" />
<label for="yes">Yes</label>
<input type="radio" id="Yes" name="q1" value="Yes"/>
<label for="no">No</label>
<input type="radio" id="No" name="q1" value="No"/>
<label for="yes">Yes</label>
<input type="radio" id="Yes" name="q2" value="Yes"/>
<label for="no">No</label>
<input type="radio" id="No" name="q2" value="No"/>
<label for="yes">Yes</label>
<input type="radio" id="Yes" name="q3" value="Yes"/>
<label for="no">No</label>
<input type="radio" id="No" name="q3" value="No"/>
<input type="radio" id="Maybe" name="q3" value="Maybe"/>
<label for="why">Why? (please state)</label><input type="text" id="why" name="why"/>
<input type="submit" id="message_submit" class="submit-button"/>
</form>
PHP
<?php
$toaddress = "example#example.com";
$subject = "Subject";
$email = "sender#example.com";
$url = 'thankyou.php';
$msg = "$name\n";
$msg.= "$date\n";
$msg.= "$job\n";
$msg.= "$q1\n";
$msg.= "$q2\n";
$msg.= "$q3\n";
$msg.= "$why\n";
$mailheaders = "From: $email\n";
$mailheaders .= "To: recipient#example.com\n";
$mailheaders .= "Content-Type: multipart/mixed; \n";
mail($toaddress, $subject, $msg, $mailheaders);
if($sent)
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
you have to use $_POST[ ] to retrieve your data from html form... if your using POST method then change your code like this...
$msg = $_POST['name'];
$msg.= $_POST['date'];
PHP doesn't create global variables for every form input you submit. Use the $_POST superglobal.
Hope all values are retrieved and no syntax errors at all.
If you are testing it offline or localhost then it might not work by simply php mail function, so in that case u can use smtp mail server which will work if there is an internet connection.
check this out : PHP Mail server classes

How do I find checkbox value in form sending $_POST (PHP)

I have this form:
<form action="../index_success.php" method="post" id="sendEmail" class="email">
<h3 class="register2">Newsletter Signup:</h3>
<ul class="forms email">
<li class="name">
<label for="yourName">Name: </label>
<input type="text" name="yourName" class="info" id="yourName" value="<?php echo $_POST['yourName']; ?>" /><br />
</li>
<li class="city"><label for="yourCity">City: </label>
<input type="text" name="yourCity" class="info" id="yourCity" value="<?php echo $_POST['yourCity']; ?>" /><br />
</li>
//This is where I need help with the check box code
<li class="classes"><label for="classInterest">Interested in classes?: </label>
<input type="checkbox" name="classInterest" class="info" id="classInterest" value="Yes" /><br />
</li>
<li class="email">
<label for="emailFrom">Email: </label>
<input type="text" name="emailFrom" class="info" id="emailFrom" value="<?php echo $_POST['emailFrom']; ?>" />
<?php if(isset($emailFromError)) echo '<span class="error">'.$emailFromError.'</span>';
?>
</li>
<li class="buttons email">
<button type="submit" id="submit">Send</button>
<input type="hidden" name="submitted" id="submitted" value="true" />
</li>
</ul>
</form>
This is emailed to a user. I don't know how to add the check box above so the user sees a "yes" if the box is checked:
<?php
$mailTo = 'xxx#xxx.com'; // This is the hardcoded Recipient Address
$mailSubject = 'Subject'; // This is the hardcoded Subject
$mailFrom = $_POST['emailFrom'];
$yourName = $_POST['yourName'];
$yourCity = $_POST['yourCity'];
$classInterest = $_POST['classInterest']; //This is the code for the checkbox
$mailHeader = "From: {$mailFrom}";
$mailBody = "Name = {$yourName} City = {$yourCity} Class interest = {$classInterest}";
mail( $mailTo , $mailSubject , $mailBody , $mailHeader );
Basically, what I need is (psudocode):
if ($classInterest == "yes") {
$classInterest = "Interested in Classes in ".$yourCity;
}
else {
...
}
Try this:
$classInterest = (isset($_POST['classInterest']) && $_POST['classInterest'] == 'Yes') ? "Interested in Classes in " . $yourCity : '';
EDIT
http://blog.gerv.net/2006/10/firefox_reload_behaviour/ read the article and first comment by Jason

PHP Sendmail Form Returns no Results

I'm extremely new to php, roughly 2 days experience.
My conundrum is this:
My html is:
<div class="form">
<form action="php/sendmail.php" method="post" id="contactwidget">
<div class="inp_r"><input type="text" name="wname" id="wname" value="Name" size="22" tabindex="11" alt="Name" /></div>
</div>
<div class="inp_r"><input type="text" name="wemail" id="wemail" value="Email" size="22" tabindex="12" alt="Email" /></div>
</div>
<table>
<tr>
<td class="text_m"><textarea name="wmessage" id="wmessage" cols="28" rows="6" tabindex="13" title="Quick Message">Quick Message</textarea></td>
<td class="text_r"></td>
</tr>
</table>
<div class="loading"></div>
<div><input type="hidden" name="wcontactemail" id="wcontactemail" value="info#joshuas.com" /></div>
<div><input type="hidden" name="wcontacturl" id="wcontacturl" value="php/sendmail.php" /></div>
<div><span>Send</span></div>
</form>
</div>
My php is this:
<?php
$email = $_POST['Email'] ;
$name = $_POST['Name'] ;
$message = $_POST['wmessage'] ;
$headers .= 'From: ' . $from . "\r\n";
mail( "info#gmail.com", "Message from Joshua",
$message, "From: $email" );
?>
All that is returning is an email with "Message from Joshua"
Any ideas??
you have to redirect the page in php script.
header( "location=wherever.php" );
IF you want to test the output use this: http://papercut.codeplex.com/

Categories