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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Newbie - Set $From to DropDown value?

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
Hello all i'm GREEN when it comes to PHP. I had a buddy dev a quick PHP site that has a few drop downs and input boxes. Then a "Send Email" button that sends an email. That all works.

I added a dropdown list that displays peoples names, and the value of the names are their email address.

example: dropdown displays "Matt Loflin" but the value is "matt.loflin@mail.com" I have 3 (i think easy questions)

1. I would like for the email to appear to be "from" "matt.loflin...." but I can't figure it out.

Here is the code of the page:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]

<head>
    <link href="<?=base_url()?>css/Style.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="<?=base_url()?>javascript/jquery.simplemodal.js"></script>
    <script type="text/javascript" src="<?=base_url()?>javascript/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="<?=base_url()?>javascript/custom.js"></script>
    <title>SWAC</title>
    <style type="text/css">
        @media print
            {
                #wrapper { display: none; }
				#bottom_btns { display: none; }
                #information { display: block; }
            }
    </style>
</head>
<body class="ss-base-body" dir="ltr">
<form method="post" name="SWACCC" action="email" id="SWACCC" class="validate" onsubmit="return validate();">
    <div id="wrapper" class="ss-form-container">
        <h1 class="ss-form-title">SWAC CREDIT CARD CHARGE FORM</h1>
    </div>
    <div id="information" class="information">
        <div class="denote">* Denotes Required Field</div>
        <input value="Name" name="legend0" type="hidden"></input>
        <?php echo form_label('<b>Full Name <span class="req_txt">*</span></b>')?><br />
        <?php echo form_dropdown('name', $name,"class='req' id='name'")?><br /><br />
    
        <input value="Name of Charge Card" name="legend1" type="hidden"></input>
        <?php echo form_label('<b>Name of Charge Card <span class="req_txt">*</span></b>')?><br />
        <?php echo form_dropdown('chargeName',$chargeName, '', "class='req' id='chargeName'");?><br /><br />
        
        <input value="Date of Purchase" name="legend2" type="hidden"></input>
        <?php echo form_label('<b>Date of Purchase <span class="req_txt">*</span></b>')?><br />
        <?php echo form_input('dop', $dop,"class='req' id='dop'")?><br /><br />
    
        <input value="Amount of Purchase" name="legend3" type="hidden"></input>
        <?php echo form_label('<b>Amount of Purchase <span class="req_txt">*</span></b>')?><br />
        <?php echo form_input('amount',$amount,"class='req' id='amount'")?><br /><br />
    
        <input value="Place of Purchase" name="legend4" type="hidden"></input>
        <?php echo form_label('<b>Place of Purchase <span class="req_txt">*</span></b>')?><br />
        <?php echo form_input('pop',$pop,"class='req' id='pop'")?><br /><br />
    
        <input value="Charge to Budget Account" name="legend5" type="hidden"></input>
        <?php echo form_label('<b>Charge to Budget Account <span class="req_txt">*</span></b>')?><br />
        <i>Please select the Budget Account that this expense is to be charged to:</i><br />
        <?php echo form_dropdown('budget', $budget, '', "class='req' id='budget'")?><br /><br />
    
        <input value="Charge to Reserve Account" name="legend6" type="hidden"></input>
        <?php echo form_label('<b>Charge to Reserve Account <span class="req_txt">*</span></b>')?><br />
        <i>Please select the Reserve Account that this expense is to be charged to:</i><br />
        <?php echo form_dropdown('reserve', $reserve, '', "class='req'  id='reserve'")?><br /><br />
    </div>
    <div id="bottom_btns" class="information" align="center">
        <?=form_submit('Submit', 'Send E-mail')?> &nbsp; &nbsp; &nbsp; <input type="button" value="Print Form" onClick="window.print()" />
    </div>
</form>
     <div id="error_alert" style="display:none;">
          <div id="dialog_wrap">
               <div id="popup_content"> Please complete all required fields,
                    hightlighted in red. 
               </div>
               <a href="#" id="modal_close" class="modal_close">OK</a> 
          </div>
     </div>
</body>
</html>

Here is the email function
Code:
	function email() {
			$this->load->helper('url');
			
			if ($_POST) {				
				$message = '';
				$values = array_values($_POST);
				//$count = 0;
				//$prevKey = "";
				foreach($_POST as $key => $value) {
					if($value!='Send'){
						if (strlen(strstr($key,'legend'))>0){
								$message = $message.'<b>'.$value.': </b>';
	                    } else {
								$from = "$value"; //since the first value is the email... i'm grabbing it
								$message = $message.$value.'<br /><br />';
						}     
	                } 
				}
				$to = "matt.loflin@mail.com";

				$subject = "SWAC Credit Card Charge Form";
				$message = $message.'</html>';
				
				// To send HTML mail, the Content-type header must be set
				$headers  = "From: $from\r\n"; 
    			$headers .= "Content-type: text/html\r\n"; 
				$email = '<html><h4>Charge Description</h1>'.$message;
				mail($to, $subject, $email, $headers);

				echo "<html><h4>Thank you for your submission! Your message was successfully sent.</h4>".$message;
		} else {
			echo "<h4 style='margin-left:20px;width:250px'>Sorry, no form data has been received.</h4>";
			}
		}
}

there is an index function that is populating all the dropdowns in a format like:
Code:
'matt.loflin@mail.com'=>'Matt Loflin',

again I don't know much about it.

2.Another thing i'd like it to do is beside the Dropdown for name, when they select a name then have it populate a label with their email.

ex. I select Matt Loflin - then a label right next to it populates my email.

3.In the email it is sending it is swapping the name with their email (i'm assuming because of the "value) could that be in the format of "Matt Loflin - matt.loflin@mail.com" so it shows the text and the value?

Thanks in advance

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
OK #1 I was able to figure out.
Code:
$from = $_POST["name"];
$headers  = "From: $from\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
{/code]

so that leaves 
#2 - having email show up beside the user name
#3 - have name and email show up in email

I'm thinking I might have to change the way i'm doing the drop down list
I have Names with values being emails.

What if I changed it to where names = names and have a function that would populate the email into a label, then when I send an email I would just read the labels value for the from. then for #1 I'd have to just read the labels value instead of the drop downs value

Any ideas?


- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Code:
$from = $_POST["name"];
$headers  = "From: $from\r\n"; 
$headers .= "Content-type: text/html\r\n";


- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
ex. I select Matt Loflin - then a label right next to it populates my email.

that's probably a javascript solution. attach an onchange event to the select box and then populate the innerHTML of an element on the right of the dropdown. assuming you have mark up like this

Code:
<select id="myDropdown"><option value="myemail">My name</option></select><span id="myName"></span>

you could use some simple jQuery like this

Code:
jQuery('document').ready(function(){jQuery('#myDropDown').bind('change', function(){jQuery('#myName').html(jQuery('this').val());});});

3. grab the name value pairs from your lookup function then use this code

Code:
if(isset($myArray[$_POST['from']])):
  $from = $myArray[$_POST['from']] . '<' . $_POST['from'] .'>';
else: 
  $from = $_POST['from'];
endif;
$headers  = "From: $from\r\n";
 
so this is what I have:
Code:
		<select name="name" id="name">
               <?php foreach($name as $key => $value) : ?>
              	<option value="<?php echo $key?>"><?php echo $key?></option>
              	<?php endforeach; ?>
          </select>

and here is my email code:

Code:
function email() {
			$this->load->helper('url');
			
			if ($_POST) {				
				$message = '';
				$values = array_values($_POST);
				//$count = 0;
				//$prevKey = "";
				foreach($_POST as $key => $value) {
					if($value!='Send E-mail'){
						if (strlen(strstr($key,'legend'))>0){
							$message = $message.'<b>'.$value.': </b>';
	                    } else {
							$message = $message.$value.'<br /><br />';
						}  
	                } 
				}
				$to = "ENTER email"
				$from = $_POST["name"];
				$subject = "SWAC Credit Card Charge Form";
				$message = $message.'</html>';
				
				// To send HTML mail, the Content-type header must be set
				$headers  = "From: $from\r\n"; 
    			$headers .= "Content-type: text/html\r\n"; 
				$email = '<html><u><h4>Charge Description</h4></u>'.$message;
				mail($to, $subject, $email, $headers);

				echo "<FORM><html><u><h4>Thank you for your submission, your receipt was successfully sent.</h4></u><br/>".$message."<br/><br/><INPUT TYPE=\"BUTTON\" VALUE=\"Return To Form\" 
				ONCLICK=\"window.location.href='[URL unfurl="true"]http://www.jm.com/SWAC/'\"></html></FORM>";[/URL]
		} else {
			echo "<h4 style='margin-left:20px;width:250px'>Sorry, no form data has been received.</h4>";
			}
		}

What I want is the $From to be the email address in the top Code function. But since i'm populating the Drop down with the $Keys it isn't sending the values. But I still need the $keys because it is the persons name.

$Key => $value and the $key is being displayed in the ddl and being used in the email. What I'd like is that the $value is also used in the email.

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
i gave you the answer above.

the $myArray is the same array as $name in the first code snip you posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top