I am changing the header of my php mail form so that it shows who it was sent from. I have a with contact name options. I want to give those options two values so that I can query on 开发者_JAVA技巧value in one location and the second in another. I feel like the values have to be assigned to a differnt select names to be able to query it, but I have no clue how to go about doing it. Example:
PHP Form
Contact:
<select name="contactname">
<option></option>
<option value1="John Smith" value2="jsmith@email.com">John Smith</option>
</select>
PHP Post
$contactname = $_POST [contactname];
$header = "From: " . $value 1 here . " <" . $value 2 here . ">\r\n";
$message = "
Contact: $value 1 here
";
mail("email.email.com", $subject, $message, $header,);
Anyone have any ideas how to code what I trying to get?
If the list of options is coming out of a database, put in the table's primary key value instead, and then you can look up the appropriate values when the form's submitted. That saves you the hassle of having to validate the email addresses, because nothing says a malicious user can't hack up the script to say "president@whitehouse.gov" and put some threats in the message. They sent the message, but you're the one who'll have the Secret Service kicking down your door.
HTML
<select name="contactname">
<option value="John Smith|jsmith@email.com">John Smith</option>
</select>
PHP
$pieces = explode('|', $_POST['contactname']);
$from = $pieces[0]; // John Smith
$to = $pieces[1]; // jsmith@email.com
Note that this isn't actually the proper way since it can be spoofed.
Contact:
<select name="contactname">
<option></option>
<option value="John Smith,jsmith@email.com">John Smith</option>
</select>
PHP
$contactname = $_POST['contactname'];
$contact_arr = explode(",", $contactname);
$header = "From: ".$contact_arr[0]." <".$contact_arr[1].">";
$message = "
Contact: ".$contact_arr[0]."
";
mail("email@email.com", $subject, $message, $header);
Explode and a delimiter will do it. You should really run some validation on $_POST to make sure its clean first though.
精彩评论