Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I send an email using input from form? 1

Status
Not open for further replies.

ERuiz

Technical User
Dec 14, 2005
33
US
I have a question. I would like to have the information entered on this form sent via email once the user submits the information. The catch is that the email to be sent to, has to use a variable taken from the page itself. For example, the email would have to be something like this:

$domicile@flyapr.com

Since we have 2 different domiciles to where a user belongs to, the email would be tjsj@flyapr.com or kmia@flyapr.com

tjsj or kmia would be decided by $domicile@flyapr.com

Here is the code:

Code:
<?php
$ipaddr = getenv(REMOTE_ADDR);
?>
<script language="JavaScript">
<!--
function CalculateHours()
{
var minutes1 = 0;
var minutes2 = 0;
var minutesdelta = 0;
var hours = 0;
var hours2 = '';

minutes1 = document.adminForm.PB_HH.value*60 + document.adminForm.PB_MM.value*1;
minutes2 = document.adminForm.SD_HH.value*60 + document.adminForm.SD_MM.value*1;

if (minutes1 < minutes2) {minutesdelta = minutes2-minutes1}
else {minutesdelta = (minutes2+24*60)-minutes1};

if (document.adminForm.vatsim_flight.value == 'YES') {minutesdelta=minutesdelta*1.5};

hours = (minutesdelta/60)+0.05;
hours2 = ''+hours;

if (hours2.indexOf('.') != -1)
{
        hours2=hours2.substring(0,hours2.indexOf('.')+2);
}

//alert ('Test ' + minutesdelta + ' ' + hours2);
document.adminForm.total_block_time.value=hours2;
}


function callJavaScript(functionName)
{
  return eval(functionName)
}
//-->
</script>

                <script type="text/javascript" src="components/com_philaform/fvalidate/fValidate.config.js"></script>
                <script type="text/javascript" src="components/com_philaform/fvalidate/fValidate.core.js"></script>
                <script type="text/javascript" src="components/com_philaform/fvalidate/fValidate.lang-enUS.js"></script>
                <script type="text/javascript" src="components/com_philaform/fvalidate/fValidate.validators.js"></script>

                    <style>
                      input.errHilite, select.errHilite, textarea.errHilite {
                          border: 1px solid #DDC;
                          background-color: #FFE;
                          }
                      label.errHilite {
                          color: #C00;
                          }

                          ul#errors {
                          display: none;
                          margin-top: 1em;
                          background-color: #FFE;
                          border: 1px solid #CC9;
                          padding: .3em;
                          color: #C00;
                          font-weight: bold;
                          }
                      ul#errors li {
                          margin-top: .5em;
                          margin-bottom: .5em;
                          padding: 0;
                          }
                      ul#errors li.heading {
                          list-style-type: none;
                          color: #000;
                          font-weight: normal;
                          font-style: italic;
                          margin: .5em 0;
                          padding: 0;
                          }
                </style>
            <ul id="errors">

            </ul>

<div class="contentheading"></div>
<form method="post" action="index.php" name="adminForm" id="adminForm" enctype="multipart/form-data" onsubmit="return validateForm( this, false,    true,    false,     true,       28 );">
<div class="componentheading">Submit FREP</div>
<table border="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td><font color="red" size="5">·</font> = Required</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Date Flown</td>
<td>
<input paf="blank" emsg="" type="text" class="inputbox" name="date_flown" id="date_flown" size="9"  value="click here -->">
<script type="text/javascript" src="includes/js/mambojavascript.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="includes/js/calendar/calendar-mos.css" title="green" />
<script type="text/javascript" src="includes/js/calendar/calendar.js"></script>
<script type="text/javascript" src="includes/js/calendar/lang/calendar-en.js"></script>
<input name="reset" type="reset" class="button" onClick="return showCalendar('date_flown');" value="...">
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Flight Number</td>
<td><input type="text" paf="alnum|4|U|true|false|none" emsg="" alt="" class="inputbox" name="flight_number" size="9" maxlength="6"  value="">
 <font size="1">(ex. AP2732)</font>
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>VATSIM Flight</td>
<td><select size="1" paf="select" emsg="" class="inputbox" name="vatsim_flight" onChange="Javascript:CalculateHours();" >
<option value="" SELECTED>&nbsp;&nbsp;&nbsp;&nbsp;</option>
<option value="YES">Yes</option>
<option value="NO">No</option>
</select>
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Aircraft Used</td>
<td><select size="1" paf="select" emsg="" class="inputbox" name="aircraft_used"  >
<option value="" SELECTED>&nbsp;&nbsp;&nbsp;&nbsp;</option>
<option value="Aerospatiale 350">Aerospatiale 350</option>
<option value="Aerospatiale ATR 42-500">Aerospatiale ATR 42-500</option>
<option value="Aerospatiale ATR 72-500">Aerospatiale ATR 72-500</option>
<option value="Airbus 300-600R">Airbus 300-600R</option>
<option value="Airbus 320-211">Airbus 320-211</option>
<option value="Airbus 330-200">Airbus 330-200</option>
<option value="Airbus 340-300">Airbus 340-300</option>
<option value="Beechcraft 1900D">Beechcraft 1900D</option>
<option value="Bell 430">Bell 430</option>
<option value="Boeing 727-200">Boeing 727-200</option>
<option value="Boeing 737-700">Boeing 737-700</option>
<option value="Boeing 737-800">Boeing 737-800</option>
<option value="Boeing 747-200">Boeing 747-200</option>
<option value="Boeing 757-200">Boeing 757-200</option>
<option value="Boeing 767-200">Boeing 767-200</option>
<option value="Boeing 767-300">Boeing 767-300</option>
<option value="Boeing 777-200">Boeing 777-200</option>
<option value="Boeing 787-800">Boeing 787-800</option>
<option value="Bombardier CRJ-200">Bombardier CRJ-200</option>
<option value="Cessna 310">Cessna 310</option>
<option value="Cessna Caranedo Centurion">Cessna Caranedo Centurion</option>
<option value="Cessna Citation X">Cessna Citation X</option>
<option value="Cessna Conquest II">Cessna Conquest II</option>
<option value="Embraer 120">Embraer 120</option>
<option value="Embraer 170">Embraer 170</option>
<option value="Eurocopter EC135">Eurocopter EC135</option>
<option value="McDonnell Douglas DC-3">McDonnell Douglas DC-3</option>
<option value="McDonnell Douglas DC-8-73">McDonnell Douglas DC-8-73</option>
<option value="McDonnell Douglas DC-10">McDonnell Douglas DC-10</option>
<option value="McDonnell Douglas MD-83">McDonnell Douglas MD-83</option>
<option value="Other aircraft">Other aircraft</option>
</select>
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Departure Airport</td>
<td><input type="text" paf="alnum|4|U|false|false|none" emsg="" alt="" class="inputbox" name="departure_airport" size="9" maxlength="4"  value="">
<font size="1">(ICAO Code ex: KJFK)</font>
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Arrival Airport</td>
<td><input type="text" paf="alnum|4|U|false|false|none" emsg="" alt="" class="inputbox" name="arrival_airport" size="9" maxlength="4"  value="">
<font size="1">(ICAO Code ex: TJSJ)</font>
</td>
</tr>
<tr><td><font color="red" size="5">·</font>Pushback Time</td>
<td><select size="1" paf="select" emsg="" class="inputbox" name="PB_HH" onChange="Javascript:CalculateHours();" >
<option value="" SELECTED>&nbsp;&nbsp;&nbsp;&nbsp;</option>
<option value="00">00</option>
<option value="01">01</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>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>

<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
 : <select size="1" paf="select" emsg="" class="inputbox" name="PB_MM" onChange="Javascript:CalculateHours();" >
<option value="" SELECTED>&nbsp;&nbsp;&nbsp;&nbsp;</option>
<option value="00">00</option>
<option value="01">01</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>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>

<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>

<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>

<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>

<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>

<option value="52">52</option>
<option value="53">53</option>
<option value="54">54</option>
<option value="55">55</option>
<option value="56">56</option>
<option value="57">57</option>
<option value="58">58</option>
<option value="59">59</option>
</select>
 <font size="1">Z</font><br />
 </td>
 </tr>
 <tr>
 <td><font color="red" size="5">·</font>Shutdown Time</td>
 <td><select size="1" paf="select" emsg="" class="inputbox" name="SD_HH" onChange="Javascript:CalculateHours();" >
<option value="" SELECTED>&nbsp;&nbsp;&nbsp;&nbsp;</option>
<option value="00">00</option>
<option value="01">01</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>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>

<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>

<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
 : <select size="1" paf="select" emsg="" class="inputbox" name="SD_MM" onChange="Javascript:CalculateHours();" >
<option value="" SELECTED>&nbsp;&nbsp;&nbsp;&nbsp;</option>
<option value="00">00</option>
<option value="01">01</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>
<option value="09">09</option>
<option value="10">10</option>

<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>

<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>

<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>

<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>

<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>
<option value="52">52</option>
<option value="53">53</option>
<option value="54">54</option>
<option value="55">55</option>

<option value="56">56</option>
<option value="57">57</option>
<option value="58">58</option>
<option value="59">59</option>
</select>
<font size="1">Z</font>
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Total Block Time</td>
<td><input type="text" readonly="readonly" paf="" emsg="" alt="" class="inputbox" name="total_block_time" size="9" maxlength="4"  value=""><font size="1"> hrs</font>
</td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Distance Flown</td>
<td><input type="text" paf="numeric|2" emsg="" alt="" class="inputbox" name="distance" size="9" maxlength="8"  value=""><font size="1"> nm (NO DECIMALS and NO COMMAS)</font></td>
</tr>
<tr>
<td><font color="red" size="5">·</font>Fuel Used</td>
<td><input type="text" paf="numeric|3" emsg="" alt="" class="inputbox" name="fuel" size="9" maxlength="6"  value=""><font size="1"> lbs (NO DECIMALS and NO COMMAS)</font></td>
 </tr>
<tr>
<td>Comments</td>
<td><textarea paf="" emsg="" class="inputbox"  name="comments" cols="50" rows="5"  ></textarea>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="hidden" name="adminForm_philaform_sent" value="1">
<input type="submit" name="doit" id="doit" class="button" value="Send FREP" >
<input type="reset" name="reset" class="button" value="Reset">
</td>
</tr>
</tbody>
</table>
<?php
$query = "SELECT name FROM #__users WHERE id = " . $my->id;
$database->setQuery($query);
    echo "<input type=\"hidden\" name=\"username\"     value=\"" .$database->loadResult() . "\">\n";
?>
<?php
$query = "SELECT email FROM #__users WHERE id = " . $my->id;
$database->setQuery($query);
    echo "<input type=\"hidden\" name=\"email\"     value=\"" .$database->loadResult() . "\">\n";
?>
<?php
$query = "SELECT cb_pilotid FROM #__comprofiler WHERE id = " . $my->id;
$database->setQuery($query);
    echo "<input type=\"hidden\" name=\"pilot_id\"     value=\"" .$database->loadResult() . "\">\n";
?>
<?php
$query = "SELECT cb_domicile FROM #__comprofiler WHERE id = " . $my->id;
$database->setQuery($query);
    echo "<input type=\"hidden\" name=\"domicile\"     value=\"" .$database->loadResult() . "\">\n";
?>
<?php
$query = "SELECT cb_division FROM #__comprofiler WHERE id = " . $my->id;
$database->setQuery($query);
    echo "<input type=\"hidden\" name=\"division\"     value=\"" .$database->loadResult() . "\">\n";
?>
<input type="hidden" name="ipaddress"    value="<?php echo $ipaddr; ?>">
<input type="hidden" name="formtitle"     value="">
<input type="hidden" name="form_id"     value="1">
<?php
$timestamp = time();
    echo "<input type=\"hidden\" name=\"TIMESTAMP\"     value=\"" . date("Y-m-d",$timestamp) . "\">\n";
?>
<input type="hidden" name="option"     value="com_philaform">
<input type="hidden" name="Itemid"     value="62">
<input type="hidden" name="formname"     value="adminForm">
</form>
 
You could do it this way:
Code:
$domicile = $_POST['domicile'];


$address = $domicile."@flyapr.com";
$subject = "subject line goes here";
$message = "message here";

mail($address,$subject,$message);
There are additional parameters you can add to the mail function. For a full explanation click below:

 
Ok, if I follow you correctly, by using $_POST['domicile']; I can grab what is being passed as domicile and assigning that to $domicile?

So, in essence, since I will be emailing all the fields filled out by the user, I can assign each field a variable by using this $_POST function?

In other words, I can do this:

$username = $_POST['username'];
$email = $_POST['email'];
$cb_pilotid = $_POST['cb_pilotid'];
etc etc...

If this is correct, how would I go about adding these variables correctly, on the $message = ""; line? It's the coding that goes between the quotes which I don't know. ;-) BTW, THANKS for your help on this!
 
So, in essence, since I will be emailing all the fields filled out by the user, I can assign each field a variable by using this $_POST function?

That's right.


For the message string start with a double quote, then add your variables along with your other text and end with a double quote.

The forward slashes followed by the letter n are newline characters and will force a newline when your email message is rendered.

e.g.
Code:
$firstname = "John";
$lastname = "Smith";

$message = "My first name is $firstname and my last name is $lastname";

Or you could do it this way:
Code:
$message = "My first name is".$firstname."and my last name is".$lastname;

Both will give the same result.
 
You guys are AWESOME! This is the second hurdle I have overcome today with the help of this website! THANKS A MILLION!
 
Ok, I got excited too quick! :-( It's not working. Could it be that it's not working because this code is being used on the form page itself? I mean, when the page is displayed, the forms have not been filled out yet and the script is already being executed once the page is loaded. When the user clicks SEND, this script is not being executed since it was already executed when the page loaded, and there was no data yet to fetch.
 
Here is the code that is being used within the forms page itself:

Code:
$useremail = $_POST['email'];
$username = $_POST['username'];
$pilotid = $_POST['pilot_id'];
$domicile = $_POST['domicile'];
$division = $_POST['division'];
$dateflown = $_POST['date_flown'];
$flightnumber = $_POST['flight_number'];
$vatsimflight = $_POST['vatsim_flight'];
$aircraftused = $_POST['aircraft_used'];
$departureairport = $_POST['departure_airport'];
$arrivalairport = $_POST['arrival_airport'];
$pb_hh = $_POST['PB_HH'];
$pb_mm = $_POST['PB_MM'];
$sd_hh = $_POST['SD_HH'];
$sd_mm = $_POST['SD_MM'];
$totalblocktime = $_POST['total_block_time'];
$distance = $_POST['distance'];
$fuel = $_POST['fuel'];
$comments = $_POST['comments'];
$freptimestamp = $_POST['TIMESTAMP'];
$strdomicile = strtolower($domicile);

$to = "$strdomicile@flyapr.com";
$subject = "AirPUERTORICO Virtual Airlines - FREP - $username - $pilotid";
$extra = "From: $useremail\r\n";
$message = "######AUTOMATICALLY GENERATED EMAIL. DO NOT REPLY TO THIS EMAIL######\n\nHello " . $domicile . " Domicile Manager, below is a copy of a FREP submitted on " . $freptimestamp . " by " . $username . ":\n\n\n\nPilot Name: " . $username . "\nPilot ID: " . $pilotid . "\nDomicile: " . $domicile . "\nDivision: " . $division . "\nDate Flown: " . $dateflown . "\nFlight Number: " . $flightnumber . "\nVATSIM Flight: " . $vatsimflight . "\nAircraft Used: " . $aircraftused . "\nDeparture Airport: " . $departureairport . "\nArrival Airport: " . $arrivalairport . "\nPushback Time: " . $pb_hh . ":" . $pb_mm . " Z\nShutdown Time: " . $sd_hh . ":" . $sd_mm . " Z\nTotal Block Time: " . $totalblocktime . " hrs\nDistance Flown: " . $distance . " nm\nFuel Used: " . $fuel . " lbs\nComments: " . $comments . "\n\nPlease make sure all the information is correct and if so, you are free to update this pilot's Total Flight Time, with the newly acquired time, by editing his profile.";

mail ($to, $subject, $message, $extra);
 
All of the top section above containing $_POST['whatever'] needs to be on the page that you are sending the form to.

You can use echo functions to check that everthing is being passed correctly and to troubleshoot mistakes.
 
Ok, so that means there is no way to get this done from the form page itself? I will paste the code on the other page like you stated. Thanks!
 
I have a question. Can the form be made so that it sends the info to the page as it is right now AND send the info to a SECOND page? I would put the code on this second file and have the form page submit the data to this second file.

In other words, can the form be sent to 2 different files at the same time once the user clicks the send button? If yes, what would be the code to do this? Thanks in advance.
 
NO you are making everything harder than it needs to be.

If the form has not been subitted, the values for the $_POST variables will not be there and you'll most likely get warnings or notices about it.

If you want the script to not execute before the form is submitted add an IF statement that checks the submitions something like:
Code:
if([blue]isset[/blue]($_POST['doit'])){
your mailing code goes here
}
else{
you form code goes here.
}
This will chekc wether your submit button which you names [red]doit[/red] has been sent, if it has it will execute the submission code, if not it will just render the form.

Check all your variables, before sending the email.
try doing [blue]echo[/blue] $to;



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Nop, the above didn't work. The echo returns empty values.
 
That means the values arent getting through.
So all fo these are coming back empty?
Code:
$useremail = $_POST['email'];
$username = $_POST['username'];
$pilotid = $_POST['pilot_id'];
$domicile = $_POST['domicile'];
$division = $_POST['division'];
$dateflown = $_POST['date_flown'];
$flightnumber = $_POST['flight_number'];
$vatsimflight = $_POST['vatsim_flight'];
$aircraftused = $_POST['aircraft_used'];
$departureairport = $_POST['departure_airport'];
$arrivalairport = $_POST['arrival_airport'];
$pb_hh = $_POST['PB_HH'];
$pb_mm = $_POST['PB_MM'];
$sd_hh = $_POST['SD_HH'];
$sd_mm = $_POST['SD_MM'];
$totalblocktime = $_POST['total_block_time'];
$distance = $_POST['distance'];
$fuel = $_POST['fuel'];
$comments = $_POST['comments'];
$freptimestamp = $_POST['TIMESTAMP'];
$strdomicile = strtolower($domicile);
IF they are all blank then then the form is not submitting right.
Remove the onsubmit event from the from declaration and see if it submits the values
Code:
<form method="post" action="index.php" name="adminForm" id="adminForm" enctype="multipart/form-data" onsubmit="[red]return validateForm( this, false,    true,    false,     true,       28 );[/red]">



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top