简体   繁体   中英

if (isset($_post['submit'])) is not working

I'm learning PHP. A beginner. The code from the tutorial I follow is below.

    <?php
        if (isset($_POST['submit']) && (!empty($_POST['submit']))) {
            $from = 'Alexey Pazukhin (alexey.pazukhin@mail.ru)';
            $subject = $_POST['subject'];
            $text = $_POST['elvismail'];
            $output_form = FALSE;
              if (empty($subject) && empty($text)){
                echo 'Subject and text fields are empty. <br/>';
                $output_form = TRUE;
              }
              if (empty($subject) && (!empty($text))) {
                echo 'Subject field is empty. <br/>';
                $output_form = TRUE;
              }
              if ((!empty($subject)) && empty($text)) {
                echo 'Text field is empty. <br/>';
                $output_form = true;
              }
              if((!empty($subject)) && (!empty($text))){

                 $dbc = mysqli_connect('localhost', 'root', 'root', 'elvis_store') 
        or die ('Connection failed. MySQL');
                 $query = "SELECT * FROM email_list";
                 $result = mysqli_query($dbc, $query) 
        or die('DB query error');
                 while ($row = mysqli_fetch_array($result)) {
                 $first_name = $row['first_name']; 
                 $last_name = $row['last_name'];
                 $msg = "Dear $first_name $last_name, \n $text";
                 $to = $row['email'];
                 mail($to, $subject, $msg, 'From:' . $from);
                 echo 'Message sent:' . $to . '<br/>';
                 }
               mysqli_close($dbc);
              }
          }
        else {
          $output_form = TRUE;
        }

        if ($output_form) {
        ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <label for="subject">Subject of email:</label><br />
        <input id="subject" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
        <label for="elvismail">Body of email:</label><br />
        <textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
        <input type="submit" name="Submit" value="Submit" />
  </form>
<?php
}
?>

The problem is that code doesn't send any mails after clicking Submit button (either I fill the fields or don't) and returns an empty (new) form in browser (Chrome).

Replace

 <input type="submit" name="Submit" value="Submit" />

with

 <input type="submit" name="submit" value="submit" />

you can only check - if(isset($_POST['Submit'])). You dont want to check for !empty.

In your input tag you have given name attribute as name="Submit".

so, use $_POST['Submit']

instead of $_POST['submit']

Because post variables are CASE SENSITIVE.

I'd change if (isset($_POST['Submit')) to if (isset($_POST['sendIt'))

and then i'd change <input type="submit" name="Submit" value="Submit" />

to:

<input type="submit" name="sendIt" value="Submit" />

This way your form has it's own unique send value rather than the bog standard submit which could lead to future issues if and when you decide to add anymore forms into the website.

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