Pergunta

I made a contact form a year ago, and have been re-using the code ever since.

It is just 3 text boxes but I need to add a Select option, but I have no idea how to add php to it.

This is one of the sections of the php, it's all the same other than naming so no need to post it all.

<?php 
   error_reporting(E_ALL ^ E_NOTICE);
   if(isset($_POST['submitted'])) {
       if(trim($_POST['contactFirstName']) === '') {
            $nameError =  'Forgot your name!'; 
            $hasError = true;
       } else {
           $name = trim($_POST['contactFirstName']);
       }
       if(!isset($hasError)) {
           $emailTo = 'dezfouli.lila@live.com';
           $subject = 'Submitted message from '.$name;
           $sendCopy = trim($_POST['sendCopy']);
           $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
           $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
           mail($emailTo, $subject, $body, $headers);
           $emailSent = true;
       }
   }
?>

This is one of the text boxes

<form id="contact-us" action="book.php" method="post">
    <div class="formblock">
        <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactFirstName'])) echo $_POST['contactFirstName'];?>" class="txt requiredField" placeholder=" First Name:" />
        <?php if($nameError != '') { ?>
        <br /><span class="error"><?php echo $nameError;?></span> 
        <?php } ?>
    </div>

I need to now make it work with this:

<div class="formblock">
    <select name="month">
        <option value="date">Month
        <option value="1">January
        <option value="2">February
        <option value="3">March
    </select>
    <?php if($emailError != '') { ?>
<br /><span class="error"><?php echo $emailError;?></span>
<?php } ?>
</div>

Nenhuma solução correta

Outras dicas

You can access which value has been submitted by getting the value of $_POST['month']

For Example:

if (isset($_POST['month']) && $_POST['month'] != 'date') {
    // add your code here
}

You also need to close your option tags as so:

<div class="formblock">
    <select name="month">
        <option value="date">Month</option>
        <option value="1">January</option>
        <option value="2">February</option>
        <option value="3">March</option>
    </select>
    <?php if($emailError != '') { ?>
<br /><span class="error"><?php echo $emailError;?></span>
<?php } ?>
</div>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top