Tutorials for passing value data to email with PHP - php

I have built a html form in which a user has to specify which product he likes to order and depending on his answer other input fields arise. I have used some javascript for that. But now i want to data the user enters to be passed on to email via php. I know very little of php and scripting in general (let's say about nothing) but i did find some tutorials and templates on it. The only thing is that they all seem slightly different and specific to a form (mostly contact forms). And if i follow them closely it works fine, it's just that when i want to customize a little i seem to get terribly lost.
I have posted this before (Can i post data from a select drop down to email using PHP?) and have gotten some answers. It's just that i didn't formulate my initial problem well enough and edited it afterwards, but i don't know if that's the way to go on this forum. So i posted a new question (sorry if that's overkill, i'm pretty new to all of this). This is my html:
<form method="post" name="contact_form" action="contact.php">
<div class="form-div1">
<label for="profile" class="label1">Select your profile</label>
<br>
<select id="user-type" class="selectlist">
<option value="option1">I'm a first-time user</option>
<option value="option2">I would like to renew or upgrade</option>
</select>
</div>
<div class="form-div2">
<label for="SEN" class="label2">SEN</label>
<br>
<input type "text" name="input1" class="input1">
</div>
<div class="form-div3">
<label for="email" class="label3">Email Address</label>
<br>
<input type "text" name="input2" class="input2">
</div>
<div class="form-div4">
<label for="product_choice" class="label4" name="select_menu">Select your product</label>
<br>
<select id="product" class="selectlist">
<option value="option1">JIRA</option>
<option value="option2">Confluence</option>
<option value="option3">JIRA Service Desk</option>
<option value="option4">Stash</option>
<option value="option5">Other</option>
</select>
</div>
<div class="form-div42">
<label for="product" class="label42">Specify your product</label>
<br>
<input type "text" name="input2" class="input2">
</div>
<div class="form-div5">
<label for="license_choice" class="label5">Select your license</label>
<br>
<select id="select" class="selectlist">
<option value="option1">25 users</option>
<option value="option2">50 users</option>
<option value="option3">100 users</option>
</select>
</div>
<div class="input_box_atlassian">
<input type="submit" name="submit" value="Submit" class="submit-button-atl" />
</div>
</form>
Now i want to build working php so i can pass the data to an email. Can somebody point me in the right direction or preferably show me some tutorials that tackle this issue more closely.

I havent dont any validation, try client side & server side validation along with code
Change the HTML as below
<form method="post" name="contact_form" action="contact.php">
<div class="form-div1">
<label for="profile" class="label1">Select your profile</label>
<br>
<select id="user_type" name="user_type" class="selectlist">
<option value="I'm a first-time user">I'm a first-time user</option>
<option value="I would like to renew or upgrade">I would like to renew or upgrade</option>
</select>
</div>
<div class="form-div2">
<label for="SEN" class="label2">SEN</label>
<br>
<input type "text" name="SEN" class="SEN">
</div>
<div class="form-div3">
<label for="email" class="label3">Email Address</label>
<br>
<input type "text" name="email" class="email">
</div>
<div class="form-div4">
<label for="product_choice" class="label4" name="select_menu">Select your product</label>
<br>
<select id="product" name="product" class="selectlist">
<option value="JIRA">JIRA</option>
<option value="Confluence">Confluence</option>
<option value="JIRA Service Desk">JIRA Service Desk</option>
<option value="Stash">Stash</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-div42">
<label for="product" class="label42">Specify your product</label>
<br>
<input type "text" name="product_specify" id="product_specify" class="input2">
</div>
<div class="form-div5">
<label for="license_choice" class="label5">Select your license</label>
<br>
<select id="license_choice" name="license_choice" class="selectlist">
<option value="25">25 users</option>
<option value="50">50 users</option>
<option value="100">100 users</option>
</select>
</div>
<div class="input_box_atlassian">
<input type="submit" name="submit" value="Submit" class="submit-button-atl" />
</div>
</form>
And make another PHP file by the name contact.php & paste the below code.
<?php
if($_REQUEST['submit']=='Submit'){
$user_type=$_REQUEST['user_type'];
$sen=$_REQUEST['SEN'];
$email=$_REQUEST['email'];
$product=$_REQUEST['product'];
$product_specify=$_REQUEST['product_specify'];
$license_choice=$_REQUEST['license_choice'];
$to = 'nobody#example.com'; //to whom the email have to besnd
$message = "User Type: $user_type \n Sen: $sen \n Email: $email \n Product: $product \n Product Specified: $product_specify \n License: $license_choice"; //content of the email
$subject = 'hello'; //shown as subject of the email
$headers = 'From: webmaster#example.com' . "\r\n" . //eamil header information
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
echo "Mail Sucessfully Sent";
} else {
echo "Something went wrong";
}
}
?>
This will get the content from the form HTML & email its contents, however only if the email server is configured correctly

Consider the following URL http://www.freecontactform.com/email_form.php
Cheers!!

Related

PHP Not Displaying HTML Input [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
I'm still stuck on this PHP code. I'm trying to code it where the user inputs some information and the PHP code displays the information the user inputted it. Basically I'm trying to build a confirmation page so the user sees the information inputted before submitted it.
HTML
<DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DormAngel Booking Form</title>
</head>
<body>
<form action="welcome.php" method="POST">
<fieldset>
<legend> Personal Details: </legend>
<label for="fname>"></label>
<input type="text" name="Name" id="fname" required autofocus placeholder="First Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="lname>"></label>
<input type="text" name="Last Name" id="lname" required autofocus placeholder="Last Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="email">Email: </label>
<input type="text" name="email" id="email" required placeholder="Your school email" pattern="[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2} title="Please enter a valid email address>
<label for="phone">Phone: </label>
<input type="tel" name="phone" id="phone" required placeholder="Please enter in your phone number">
<select name="country" required>
<option value=""> </option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="AUS">AUS</option>
</select>
</fieldset>
<br>
<fieldset>
<legend> Booking Details: </legend>
<input type="date" name="date" min="2018-10-07" max="2018-10-31">
<input type=time min=9:00 max=17:00 step=900>
<br>
<br>
<label for="dorm">Dormitory: </label>
<br>
<select name="dorm" required>Dormitory
<option value="Cypress">Cypress Hall</option>
</select>
<br>
<br>
<label for="floor">Floor: </label>
<br>
<select name="floor" required>Floor:
<option value=""></option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
</select>
<br>
<br>
<label for="roomnumber">Room Number: </label>
<br>
<select name="roomnumber">Room Number:
<option value=""></option>
<option value="22">22</option>
</select>
<br>
<br>
<label for="roomletter">Room Letter: </label>
<br>
<select name="roomletter" required="">Room Letter
<option value=""></option>
<option value="A">A</option>
<option value="B"> B</option>
</select>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
PHP
<html>
<head>
<title>Confirmation Page of Web Form</title>
</head>
<h1>Confirmation Page of Customer Info</h1>
<p>Thank you for submitting this form.</p>
<p>We have successfully received it.</p>
<p>Below is a summary of the information you provided.<br><br>
<?php
echo 'First Name: ' . $_POST ["Name"] . '<br>';
echo 'Last Name: ' . $_POST ["Last Name"] . '<br>';
echo 'Email Address: ' . $_POST ["email"] . '<br>';
echo 'Telephone Number: ' . $_POST ["phone"];
?>
</p>
</html>
What is wrong with my code?
You have forgotten the <body> tag in your html.
Replace the code to be this way.
<html>
<head>
<title>Confirmation Page of Web Form</title>
</head>
<h1>Confirmation Page of Customer Info</h1>
<p>Thank you for submitting this form.</p>
<p>We have successfully received it.</p>
<p>Below is a summary of the information you provided.<br><br>
<?php
echo 'First Name: ' . $_POST["Name"] . '<br>';
echo 'Last Name: ' . $_POST["Last Name"] . '<br>';
echo 'Email Address: ' . $_POST["email"] . '<br>';
echo 'Telephone Number: ' . $_POST["phone"];
?>
</p>
</html>

Display html inputted data with PHP

Below is my HTML and PHP code. I'm trying to display my the inputted data, however, I'm getting nothing. What is wrong with my code?
HTML
<form action="welcome.php" method="POST">
<fieldset>
<legend> Personal Details: </legend>
<label for="fname>"></label>
<input type="text" name="Name" id="fname" required autofocus placeholder="First Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="lname>"></label>
<input type="text" name="Last Name" id="lname" required autofocus placeholder="Last Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="email">Email: </label>
<input type="text" name="email" id="email" required placeholder="Your school email" pattern="[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2} title="Please enter a valid email address>
<label for="phone">Phone: </label>
<input type="tel" name="phone" id="phone" required placeholder="Please enter in your phone number" pattern="[0-9]{4} [0-9]{3} [0-9]{3}" title="Please enter in a phone number in this format: #### ### ###">
<select name="country" required>
<option value=""> </option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="AUS">AUS</option>
</select>
</fieldset>
<br>
<fieldset>
<legend> Booking Details: </legend>
<input type="date" name="date" min="2018-10-07" max="2018-10-31">
<input type=time min=9:00 max=17:00 step=900>
<br>
<br>
<label for="dorm">Dormitory: </label>
<br>
<select name="dorm" required>Dormitory
<option value="Cypress">Cypress Hall</option>
</select>
<br>
<br>
<label for="floor">Floor: </label>
<br>
<select name="floor" required>Floor:
<option value=""></option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
</select>
<br>
<br>
<label for="roomnumber">Room Number: </label>
<br>
<select name="roomnumber">Room Number:
<option value=""></option>
<option value="22">22</option>
</select>
<br>
<br>
<label for="roomletter">Room Letter: </label>
<br>
<select name="roomletter" required="">Room Letter
<option value=""></option>
<option value="A">A</option>
<option value="B"> B</option>
</select>
<br>
<br>
<label for="bedroomcleaning">Bedroom: </label><br>
<input type="checkbox" id="box-4" onclick="checkPrice()" value="4">Dust all ceiling fans/light/fixtures within reach.<br>
<input type="checkbox" id="box-5" onclick="checkPrice()" value="5"> Change sheets and/or fold clothes if requested by client.<br>
<input type="checkbox" id="box-6" onclick="checkPrice()" value="6"> Straighten up, put toys away, make beds, fold clothes and put on bed. Straighten papers and put in a pile. DO NOT THROW AWAY ANY PERSONAL ITEMS!<br><br>
<label for="bathroomcleaning">Bathroom: </label><br>
<input type="checkbox" id="box-1" onclick="checkPrice()" value="1"> Clean bowl and wipe down toilet cover, seat, under seat, base and behind the base.<br>
<input type="checkbox" id="box-2" onclick="checkPrice()" value="2"> Clean all mirrors.<br>
<input type="checkbox" id="box-3" onclick="checkPrice()" value="3"> Clean countertops and backsplashes.<br>
<label for="bathroomprice" id="price">Total Price: </label>
<br>
<br>
<input type="submit">
</fieldset>
</form>
PHP
Welcome <?php echo $_POST["Name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
Your phone number is: <?php echo $_POST["phone"]; ?>
Check your pattern for the email input:
[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2}
[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z]{2}
Also:
<input type=time min=9:00 max=17:00 step=900>
<input type="time" min="9:00" max="17:00" step="900">
As said by Jon in the comments, you need a <form> div.
<form action="yourPhpPage.php" method="POST">
<label ...>...</label>
<input ... />
...
<input type="submit" value="Send" />
</form>
The action attribute tells which page to call when submitting form.
The method tells how to send data (GET or POST mainly)
The <input type="submit" /> is the submit button. When you click it the page indicated in action will be called with the values of the different inputs. The value of the submit input is what is shown on it.
Eventually you can use a submit <button> :
<button type="submit">What you want to be displayed on it.</button>
I prefer this one because it's easier to modify its style and behavior respectively with CSS and JS. But it's not the point.
Hope it helps you !Jean-Marc.

Need to email multiple recipients from combobox

I need to email multiple email addresses from each selection of a drop down box, however everything i have tried is not working.. Here is what I have so far
Im using this in the start of the form
$mailto = $_POST['unit'];
This is what I have for the drop down box
<select name="unit" size="1" id="Combobox1" style="position:absolute;left:43px;top:324px;width:61px;height:21px;z- index:18;">
<option value= 'email#email.com'; 'email#email.com'> M17</option>
<option value="email#email.com">M16</option>
</select>
The problems im having
1. on option M17 it only sends to the first email address
2. it sends the email address instead of the unit number (M17)
3. Need to figure out a way to edit the email addresses through a seperate "user friendly" page
</head>
<body>
<div id="space"><br></div>
<div id="container">
<div id="wb_Form1"
<form name="11_Dispatch" method="post" action="<?php echo basename(__FILE__); ?>" enctype="multipart/form-data" id="Form1">
<input type="hidden" name="formid" value="form1">
<div id="wb_Text1" style="position:absolute;left:10px;top:76px;width:111px;height:16px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Address</span> </div>
<input type="text" id="Editbox1" style="position:absolute;left:131px;top:76px;width:198px;height:23px;line-height:23px;z-index:1;" name="LOC" value="">
<div id="wb_Text2" style="position:absolute;left:10px;top:106px;width:111px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Subdivision</span></div>
<input type="text" id="Editbox2" style="position:absolute;left:131px;top:106px;width:198px;height:23px;line-height:23px;z-index:3;" name="SUB" value="">
<div id="wb_Text3" style="position:absolute;left:10px;top:136px;width:111px;height:16px;z-index:4;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Call back # </span></div>
<input type="text" id="Editbox3" style="position:absolute;left:131px;top:136px;width:198px;height:23px;line-height:23px;z-index:5;" name="#" value="">
<div id="wb_Text5" style="position:absolute;left:10px;top:166px;width:111px;height:16px;z-index:6;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Chief Complaint</span></div>
<input type="text" id="Editbox4" style="position:absolute;left:131px;top:166px;width:198px;height:23px;line-height:23px;z-index:7;" name="CC" value="">
<div id="wb_Text6" style="position:absolute;left:10px;top:196px;width:111px;height:16px;z-index:8;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Pt name</span> </div>
<input type="text" id="Editbox5" style="position:absolute;left:131px;top:196px;width:198px;height:23px;line-height:23px;z-index:9;" name="NAME" value="">
<div id="wb_Text7" style="position:absolute;left:10px;top:226px;width:111px;height:16px;z-index:10;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Pt Age</span> </div>
<input type="text" id="Editbox6" style="position:absolute;left:131px;top:226px;width:198px;height:23px;line-height:23px;z-index:11;" name="AGE" value="">
<div id="wb_Text9" style="position:absolute;left:10px;top:256px;width:111px;height:16px;z-index:12;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Callers Name</span></div>
<input type="text" id="Editbox7" style="position:absolute;left:131px;top:256px;width:198px;height:23px;line-height:23px;z-index:13;" name="CALLER" value="">
<div id="wb_Text10" style="position:absolute;left:10px;top:286px;width:111px;height:16px;z-index:14;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Identifying Factors</span></div>
<input type="text" id="Editbox8" style="position:absolute;left:131px;top:286px;width:198px;height:23px;line-height:23px;z-index:15;" name="NOTES" value="">
<input type="submit" id="Button2" name="" value="Dispatch" style="position:absolute;left:169px;top:357px;width:96px;height:25px;z-index:16;">
<div id="wb_Text12" style="position:absolute;left:166px;top:30px;width:133px;height:22px;z-index:17;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:19px;"><strong>911 Dispatch</strong></span></div>
<select name="unit" size="1" id="Combobox1" style="position:absolute;left:43px;top:324px;width:61px;height:21px;z-index:18;">
<option value="M17">M17 </option>
<option value="M16">M16</option>
<option value="M15">M15</option>
<option value="M14">M14</option>
<option value="M11">M11</option>
<option value="M10">M10</option>
<option value="M07">M07</option>
</select>
</form>
As suggested, don't put your to addresses in the form. This is just basic and the mail() part is right from the manual -> http://php.net/manual/en/function.mail.php (Example #4 Sending HTML email shows multiple email recipients using concatenation however, I am using implosion):
PHP:
if(isset($_POST['unit'])) {
// Store emails in the code or database to draw from:
// Personally I like to store this in an array (if I were to hardcode it)
// Reason being, is that if you suddenly one day get access to a database
// and you draw a bunch of email addresses from that database, those emails
// would likely be listed as an array.
// By storing each email in a same-named array, you basically
// create an easy-to-read mailing list
$units['m17'][] = 'email1#tester.com';
$units['m17'][] = 'email2#tester.com';
$units['m16'][] = 'email3#tester.com';
// if the <select> value of $_POST['unit'] equals 'm17'
// $units[$_POST['unit']] then is the same as $units['m17']
$to = implode(",",$units[$_POST['unit']]);
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com'."\r\n";
// As noted in the PHP Manual, the function implode() takes an array
// and combines the values into a string with a "glue", in this case a comma
// $to would then equal: "email1#tester.com,email2#tester.com" if
// $_POST['unit'] equals 'm17'.
mail($to, $subject, $message, $headers);
}
FORM:
<select name="unit" size="1" id="Combobox1" style="position: absolute; left: 43px; top: 324px; width: 61px; height: 21px; z-index: 18;">
<option value="m17">M17</option>
<option value="m16">M16</option>
</select>

Get selected text from dropdown in PHP

I am totally new in PHP, in fact the reason I am doing this is to customize a wordpress plugin so it can fit my need. So far I have a default form, what I am doing now is to add a country dropdown. Here's how I add it
<div class="control-group">
<label class="control-label" for="Country">Country :</label>
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
</div>
So far I only able to retrieve the value of selected item with
$cscf['country'];
How can I get the display text which is the country name in this case ?
You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<input type="hidden" name="country" id="country_hidden">
<script>
$(document).ready(function() {
$("#itemType_id").change(function(){
$("#country_hidden").val(("#itemType_id").find(":selected").text());
});
});
</script>
Then when your page is submitted, you can get the name of the country by using
$_POST["country"]
<select name="text selection" onchange="getText(this)">
<option value="">Select text</option>
<option value="1">my text 1</option>
<option value="2">my text 2</option>
</select>
First put a java script function on the attribute "onchange" of the select.
Then, create your function that will transfer the text in a text box by using getElementById
<script>
function getText(element) {
var textHolder = element.options[element.selectedIndex].text
document.getElementById("txt_holder").value = textHolder;
}
</script>
Then create a temporary holder:
<input type="" name="txt_holder" id="txt_holder"> type should be hidden
Then assign it in a PHP variable:
$variableName=$_POST['txt_holder'];
Try this
<?php
if(isset($_POST['save']))
{
$arrayemail = $_POST['cscf'];
$mail = $arrayemail['country'];
$explode=explode('#',$mail);
// print_r($explode);
if(isset($explode[0]))
echo ucfirst($explode[0]);
}
?>
<form method="post">
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
<div class="controls">
<input type="submit" name='save' value="Save"/>
<span class="help-inline"></span>
</div>
</form>
Try it like,
<?php
if(isset($_POST['save']))
{
//Let $_POST['cscf']['country']= malaysia#email.com
// you can explode by # and get the 0 index name of country
$cnt=explode('#',$_POST['cscf']['country']);
if(isset($cnt[0]))// check if name exists in email
echo ucfirst($cnt[0]);// will echo Malaysia
}
?>
<form method="post">
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
<div class="controls">
<input type="submit" name='save' value="Save"/>
<span class="help-inline"></span>
</div>
</form>

How to populate google maps with form data in jQuery mobile

I was hoping someone can point me in the right direction. I'm looking to have a form in my mobile version of my site that will take either the zip code or city and state plus a radius around that location and display the number of locations of a particular business.
<form id="mobileMapper" action="mapresults.php" method="post">
<div data-role="fieldcontain" class="ui-hide-label formContainer">
<label for="ziCity">City & State, or Zip:</label>
<input type="text" name="zipCity" id="zipCity" value="" placeholder="ex: Chicago, IL or 60602" />
</div>
<div data-role="fieldcontain">
<fieldset class="ui-grid-a">
<div class="ui-block-a">
<label for="radius">Radius:</label>
<select name="radius" id="radius">
<option value="5">5 miles</option>
<option value="10">10 miles</option>
<option value="20">20 miles</option>
</select>
</div>
<div class="ui-block-b"><button type="submit" data-theme="b">Submit</button></div>
</fieldset>
</div>
</form>
Thanks
You can use the Google Places library.

Categories