Question

I have two problems with the following code. Firstly, my PHP script is not being called when Submit is clicked. Secondly, how do I pass the selected "Option" from the drop down menu to the mail function as the "from" parameter for the PHPMailer?

Thanks!

   <?php 
if(isset($_POST['submit'])){
    require("class.phpmailer.php");
    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     = "STMP.BLAH.EDU"; // SMTP server

$mail->From     = $email;
$mail->AddAddress("TEST.TEST.EDU");  //HOW TO TAKE OPTION FROM DROP DOWN?
$mail->Subject  = $text;
$mail->Body     = $message;
$mail->WordWrap = 50;

if(!$mail->Send()) {
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
    }
?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Submit a New Request</title>
    <link rel="stylesheet" media="screen" href="styles.css" >
</head>
<body>
<form class="contact_form" action="" method="post" name="contact_form">
    <ul>
        <li>
             <h2>Submit a New Request</h2>
             <span class="required_notification">* Required</span>
        </li>
        <li>
            <label for="email">Email:</label>
            <input type="email" name="email" placeholder="student@iastate.com" required />
            <span class="form_hint">Enter a valid e-mail address.</span>
        </li>
          <li>
            <label for="name">Subject:</label>
            <input type="text" required />
        </li>
        </li>
          <li>
            <label for="name">Contact:</label>
            <select>
            <option value="OPTION1">OP1</option>
            <option value="OPTION1">OP2</option>
            </select>

        </li>
        <li>
            <label for="message">Body:</label>
            <textarea name="message" cols="30" rows="6" required ></textarea>
        </li>
        <li>
        <button class="submit" type="submit">Submit!</button>
        </li>
    </ul>
</form>
</body>
</html>
Was it helpful?

Solution

You're not naming any of your elements to PHP doesn't know what to do with it.

For your submit button:

<button class="submit" name="submit" type="submit">Submit!</button>

For other things you need to go into the tag and add the name to it like I did above there.

If I did this with a name "test1":

<input type="text" name="test1" />

and sent it, like you're doing through POST, it could be read in PHP as:

$_POST['test1']

OTHER TIPS

Change your button to input so your form will be submit, check this documentation

<input type="submit" name="submit" value="Submit!" class="submit">

And your select is also not proper, name is always important,

 <select name="option">
    <option value="OPTION1">OP1</option>
    <option value="OPTION1">OP2</option>
 </select>

take it to $_POST['option']

<?php 
if(isset($_POST['submit'])){
    require("class.phpmailer.php");
    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     = "STMP.BLAH.EDU"; // SMTP server

    $mail->From     = $email; // ??
    $mail->AddAddress($_POST['contact']);  //HOW TO TAKE OPTION FROM DROP DOWN?
    $mail->Subject  = $_POST['subject'];
    $mail->Body     = $_POST['message'];
    $mail->WordWrap = 50;

    if(!$mail->Send()) {
        echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent.';
    }
}
?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Submit a New Request</title>
    <link rel="stylesheet" media="screen" href="styles.css" >
</head>
<body>
<form class="contact_form" action="" method="post" name="contact_form">
    <ul>
        <li>
             <h2>Submit a New Request</h2>
             <span class="required_notification">* Required</span>
        </li>
        <li>
            <label for="email">Email:</label>
            <input type="email" name="email" placeholder="student@iastate.com" required />
            <span class="form_hint">Enter a valid e-mail address.</span>
        </li>
        <li>
            <label for="subject">Subject:</label>
            <input type="text" required name="subject" id="subject" />
        </li>
        </li>
        <li>
            <label for="contact">Contact:</label>
            <select name="contact" id="contact">
                <option value="OPTION1">OP1</option>
                <option value="OPTION1">OP2</option>
            </select>

        </li>
        <li>
            <label for="message">Body:</label>
            <textarea name="message" cols="30" rows="6" required ></textarea>
        </li>
        <li>
            <button class="submit" type="submit" name="submit">Submit!</button>
        </li>
    </ul>
</form>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top