php form with radio buttons - php

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

Related

Passing Session Variables Through PHP Mail

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']

sending html form radio box results by email?

can someone please help i am trying to email the results of 4 radio boxes from my form by email. i receive the email fine but am not getting the results sent through.
can someone show me what im doing wrong?
html form:
<html>
<head>
<title>Site Feedback</title>
</head>
<body>
<form name="myform" action="send_feedback.php" method="POST">
<div class="wrapper_feedback" align="left">
<p>Website Design:</p>
<input type="radio" name="design" value="design1">
1
<input type="radio" name="design" value="design2">
2
<input type="radio" name="design" value="design3" checked>
3
<input type="radio" name="design" value="design4">
4
<input type="radio" name="design" value="design5">
5
</p>
<hr>
<p>Ease of Use:</p>
<input type="radio" name="easeuse" value="ease1">
1
<input type="radio" name="easeuse" value="ease2">
2
<input type="radio" name="easeuse" value="ease3" checked>
3
<input type="radio" name="easeuse" value="ease4">
4
<input type="radio" name="easeuse" value="ease5">
5
</p>
<hr>
<p>Fit for Purpose:</p>
<input type="radio" name="purpose" value="purpose1">
1
<input type="radio" name="purpose" value="purpose2">
2
<input type="radio" name="purpose" value="purpose3" checked>
3
<input type="radio" name="purpose" value="purpose4">
4
<input type="radio" name="purpose" value="purpose5">
5
</p>
<hr>
<p>Layout:</p>
<input type="radio" name="layout" value="layout1">
1
<input type="radio" name="layout" value="layout2">
2
<input type="radio" name="layout" value="layout3" checked>
3
<input type="radio" name="layout" value="layout4">
4
<input type="radio" name="layout" value="layout5">
5
</p>
<input type="submit" action="submit" value="submit" name="submit">
<br>
</div>
</form>
</body>
</html>
php:
<?php ob_start(); ?>
<?php
/* Set e-mail recipient */
$myemail = "info#mydomain.com";
$subject = "site Feedback";
/* Let's prepare the message for the e-mail */
$message = "somesite.com Feedback
$design
$easeuse
$purpose
$layout
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
$_SESSION['feedback']="<div class=\"infobox-index\"><strong>Thank You</strong> - We appreciate you taking the time to tell us what you think.</div>";
header("Location: {$_SERVER['HTTP_REFERER']}");
exit();
?>
<?php ob_end_flush() ?>
Your code is relying on Register Globals which is deprecated and discouraged. You should access the data with:
$_POST['design']
Not:
$design
Instead of these variables:
$design
$easeuse
$purpose
$layout
use this:
$_POST['design']
$_POST['easeuse']
$_POST['purpose']
$_POST['layout']
You need to access the POST variables sent from the form using the following syntax:
$_POST['design']
$_POST['easeuse']
$_POST['purpose']
$_POST['layout']

How to send form check list data with PHP to a specific email

I'm really struggling to find a way to send checklist data to an email, let alone a user input email! I have tried following and applying the answers from other similar questions asked but still to no avail.
This is my PHP so far, although I am quite confused.
<?php
//Check for POST
if (isset($_REQUEST['email'])){
//All your inputs
$expecting = array('checkbox1','checkbox2','checkbox3','checkbox4' ,'checkbox5');
//Start building your email
$email_content = '';
foreach($expecting as $input){
//Is checkbox?
if(substr($input,0,8)=='checkbox'){
$email_content .= ucfirst($input).':'.(isset($_POST[$input]) && $_POST[$input] == 'on' ? 'True' : 'False'.'<br />');
}else{
$email_content .= ucfirst($input).':'.(!empty($_POST[$input]) ? $_POST[$input] : 'Unknown').'<br />';
}
}
print_r($email_content);
if(mail('$email', 'packing list', wordwrap($email_content))){
//mail sent
}else{
//mail failed 2 send
}
}
?>
And this is my HTML
<form name="emailform" method="post" action="send-list.php">
<div data-role="fieldcontain">
<label for='name'><h3>Festival name:</h3> </label>
<input type="text" name="name">
<h3>
Essentials
</h3>
<fieldset data-role="controlgroup" data-type="vertical">
<legend>
</legend>
<input name="checkbox1" id="checkbox1" type="checkbox" />
<label for="checkbox1">
Tickets
</label>
<input name="checkbox2" id="checkbox2" type="checkbox" />
<label for="checkbox2">
Parking pass
</label>
<input name="checkbox3" id="checkbox3" type="checkbox" />
<label for="checkbox3">
Directions
</label>
<input name="checkbox4" id="checkbox4" type="checkbox" />
<label for="checkbox4">
Cash & Cards
</label>
<input name="checkbox5" id="checkbox5" type="checkbox" />
<label for="checkbox5">
Keys
</label>
</fieldset>
Email: <input name='email' type='text'><br>
<input type="submit" value="Send">
I would be happy if I could just get the checked boxes to send to an email and from there, I would hopefully be able to work out how to send the information to a user input email.
You're doing it mostly right, but your code is checking of the checkboxes have the value "on"... but never set that value in your form:
<input name="checkbox5" id="checkbox5" type="checkbox" value="on" />
^^^^^^^^^^
This is also a syntax bug:
if(mail('$email', 'packing list', wordwrap($email_content))){
^-- ^--
single-quoted strings do NOT interpolate variables. so you're trying to send an email to an account named $email, not whatever address is in the $email variable.
Try
if(mail($email, 'packing list', wordwrap($email_content))){
instead (note lack of quotes).
The $email variable is not defined.
Put this:
$email = $_REQUEST['email'];
before the mail function call, and use mail($email, ... for it to work.

Why this submit form "check list" data doesn't send to email with PHP?

I am trying to create a check list that allows users to check off each item they want and then send an email to their chosen email. I am using JQueryMobile and I don't know if it's causing any problems, but the page just keeps on loading continuously when I press submit.
This is my HTML code:
<form name="emailform" enctype="multipart/form-data" action="form-to-email.php" method="post">
<div data-role="fieldcontain">
<label for='name'>Festival name: </label><br>
<input type="text" name="name">
<h3>
Essentials
</h3>
<fieldset data-role="controlgroup" data-type="vertical">
<legend>
</legend>
<input name="checkbox1" id="checkbox1" type="checkbox" />
<label for="checkbox1">
Tickets
</label>
<input name="checkbox2" id="checkbox2" type="checkbox" />
<label for="checkbox2">
Parking pass
</label>
<input name="checkbox3" id="checkbox3" type="checkbox" />
<label for="checkbox3">
Directions
</label>
<input name="checkbox4" id="checkbox4" type="checkbox" />
<label for="checkbox4">
Cash & Cards
</label>
<input name="checkbox5" id="checkbox5" type="checkbox" />
<label for="checkbox5">
Keys
</label>
</fieldset>
<label for='email'>Send to Email:</label><br>
<input type="text" name="email">
<input name="share" type="submit" value="Share">
</form>
And this is my PHP:
<?php
if(!isset($_POST['submit'])){
if (!$_POST['name'] | !$_POST['email'])
{
echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information.</div>";
}
}
$name = $_POST['name'];
$email = $_POST['email'];
$checkbox1 = $_POST['checkbox1'];
$email_from = "Application";
$email_subject = $name;
$email_body = "You have received a new checklist via App.\n".
"Here is the checklist so far:\n $checkbox1".
$headers = "From: $email_from \r\n";
mail($email, $email_subject,$email_body,$headers);
header('Location: index.html');
?>
If you are trying to send email from localhost, then you will definitely face this error as there are no mail settings by default. However you can install a test mail server on your computer to test emails from localhost.
If you are sending the email from live server then you need to follow this tutorial to overcome any issues that might be in your code: http://www.w3schools.com/php/php_mail.asp
I think you should communicate through AJAX with jQuery, In that case you should NOT send headers. Also you should note that checkbox values are TRUE/FALSE, so
"You have received a new checklist via App.".
"Here is the checklist so far: ".

PHP email form is not sending all fields

here is my html form
<form action="formmail1.php" method="post" enctype="multipart/form-data">
<li>
Name<br /><input name="name" type="text" id="name" size="50" maxlength="50" /> <br />
First and Last Name<br /><br />
</li>
<li>
Phone<br /><input name="phone" Type="text" id="email" size="12" maxlength="12"/><br />
___-___-____<br /><br />
</li>
<li>
Email<br />
<input name="email" Type="text" id="email" size="50"/><br />
Valid email address<br /><br />
</li>
<li>
How do you want to be contacted?<br />
<input Type="radio" name="how_to_be_contacted" id="r1" class="radio" value="1" /><label for="r1">Email</label><br />
<input Type="radio" name="how_to_be_contacted" id="r2" class="radio" value="2" /><label for="r1">Phone</label><br /><br />
</li>
<li>
I would like information about...<br />
<input name="aquatic_therapy" type="checkbox" id="aquatic_therapy" />Aquatic Therapy <input name="occupational_therapy" type="checkbox" id="occupational_therapy" />Occupational Therapy<br />
<input name="speech_therapy" type="checkbox" id="speech_therapy" />Speech Therapy <input name="reflex_integration" type="checkbox" id="reflex_integration" />Reflex Integration
<br />
Select all that apply<br />
</li>
<br />
<li>
Message<br />
<textarea name="message" cols="50" rows="10" id="message"></textarea><br />
</li>
</ol>
<input id="submit" type="submit">
Here is my PHP
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
//send email
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$how_do_you_want_to_be_contcted = $_REQUEST['how_do_you_want_to_be_contcted'] ;
$information = $_REQUEST['information'] ;
$message = $_REQUEST['message'] ;
mail("email#nowhere.com", "Subject: Contact Us Form",
$message "From: $email" );
echo "Thank you for using our mail form";
else
//if "email" is not filled out, display the form
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
All that is sending is message text field but none of the other fields....
$_REQUEST['how_do_you_want_to_be_contcted'] won't work because the field name in the HTML code is how_to_be_contacted.
You're defining the phone field as $phone and then referring to $emailphone. That clearly won't work either.
mail("eweb#gmail.com", "Subject: Contact Us Form", $emailphone
$message "From: $email" );
This has syntax errors, so can't work at all as shown.
You haven't escaped any of the input values, so if someone enters something with invalid data it could break the program and/or result in the site getting hacked.
Checkboxes and Radio buttons aren't sent unless checked.
Here is another possibility:
the request name you are asking for is: how_do_you_want_to_be_contcted
in your form you call it: how_to_be_contacted

Categories