简体   繁体   中英

Adding Select dropdown to PHP POST email form

I'm trying to create a PHP POST email form from scratch and it's going well - I'm just not sure how to implement a select box on the PHP side..

Here's the code for the select box in the html:

<div class="formrow">
    <label class="drops">
        <select name="StartMonth">
            <option value="">---</option>
            <option value="January">January</option>
            <option value="February">February</option>
            <option value="March">March</option>
            <option value="April">April</option>
            <option value="May">May</option>
            <option value="June">June</option>
            <option value="July">July</option>
            <option value="August">August</option>
            <option value="September">September</option>
            <option value="October">October</option>
            <option value="November">November</option>
            <option value="December">December</option>
        </select>
    </label>
</div>

And here's the whole PHP contact form, for posterity:

<?php 
 $to = "me@mycompany.co.uk" ; 
 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data"; 

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Email"} = "Email"; 
 $fields{"Phone"} = "Phone"; 
 $fields{"Message"} = "\n" . "Message"; 

 $selectedProjects  = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
    $selectedProjects = implode(', ', $_POST['projects']);
}
 $selectedSkills  = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
    $selectedSkills = implode(', ', $_POST['skills']);
}

 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n";


 $headers2 = "From: noreply@mycompany.co.uk"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";

 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else { 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {print "Success";} 
 else 
 {print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 
 }
}
 ?> 

Additionally, feel free to note any way that is not best practice with PHP, I'm new to this.

Thanks! MC

I figured it out, eventually.. I added the following under their respective sections in the PHP:

 $startmonth = $_REQUEST['StartMonth'];

and

$body .= 'Start Month: ' . $startmonth . "\n";

Thanks to Winston and Sergio, who pointed me in the right direction!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM