简体   繁体   中英

if condition wont work in the php script

I'm trying to make a message appear only if I click on the button and it sends the email, but before I click the button the message is already there.

I called the PHP script with a submit button that has an action form on the same page, and this is the PHP script that I called:

 if(isset($_POST['envEmail']))
                    {
                      $nome=  $_POST['name'];
                      $emailcliente=  $_POST['emailCliente'];
                      $assunto=  $_POST['assunto'];
                      $menssagem=  $_POST['message'];
                          $to  = 'jun@paconstrushop.com.br'; //can receive notification

                          $subject = $assunto;
                          $message = 'Email enviado por:'.$nome."\n".$menssagem;
                          $headers = 'From:'.$emailcliente. "\r\n" .
                              'Reply-To: jun@paconstrushop.com.br' . "\r\n" .
                              'X-Mailer: PHP/' . phpversion();

                          mail($to, $subject, $message, $headers);

                          echo " <div id=success_message style=width:100%; height:100%;  > <h3>Sent your message successfully!</h3> </div>";
                    }else {
                        echo "<div id=error_message style=width:100%; height:100%;  > <h3>Error</h3> Sorry there was an error sending your form. </div>";
                    }

                     ?>

the fisrt echo is always dusplayed, it tried adding another if :

 if( mail($to, $subject, $message, $headers)){
   echo"success";
  }else{
   echo"error";
   }

but it didn't change, could someone tell me how to display if email is successful?

this is the divs and form that call the php script:

 <div id="contatoPage">
        <div id="contatoEmail" >
            <div class="row">
                <div class="emailForm">
                    <form role="form" action="Contato.php" method="post"  >
                        <div class="form-group">
                            <label for="name"> Nome:</label>
                            <input type="text" class="form-control" id="name" name="name" required maxlength="50">
                        </div>
                        <div class="form-group">
                            <label for="email"> Email:</label>
                            <input type="email" class="form-control" id="email" name="emailCliente" required maxlength="50">
                        </div>
                        <div class="form-group">
                            <label for="name"> Assunto:</label>
                            <input type="text" class="form-control" id="name" name="assunto" required maxlength="50">
                        </div>
                        <div class="form-group">
                            <label for="name"> Message:</label>
                            <textarea class="form-control" type="textarea" name="message" id="message" placeholder="Escreva sua mensagem aqui" maxlength="6000" rows="7"></textarea>
                        </div>
                        <button type="submit" name="envEmail" class="botaoEnviar pull-right" id="btnContactUs">Enviar! &rarr;</button>
                    </form>
                    <?php

                    if(isset($_POST['envEmail']))
                   {
                     $nome=  $_POST['name'];
                     $emailcliente=  $_POST['emailCliente'];
                     $assunto=  $_POST['assunto'];
                     $menssagem=  $_POST['message'];
                     $to  = 'jun@paconstrushop.com.br'; //can receive notification

                     $subject = $assunto;
                     $message = 'Email enviado por:'.$nome."\n".$menssagem;
                     $headers = 'From:'.$emailcliente. "\r\n" .
                         'Reply-To: jun@paconstrushop.com.br' . "\r\n" .
                         'X-Mailer: PHP/' . phpversion();

                     if(@mail($to, $subject, $message, $headers))
                     {
                         echo " <div id=success_message style=width:100%; height:100%;  > <h3>Sent your message successfully!</h3> </div>";
                     }
                     else
                     {
                       echo "<div id=error_message style=width:100%; height:100%;  > <h3>Error</h3> Sorry there was an error sending your form. </div>";
                     }
                   }
                     ?>
                </div>
            </div>
        </div>

问题出在提交表单的按钮上,而不是按钮标签中,当我进行这些更改时,我应该使用类型为 submit 的输入标签。

Did you try this:

  if(isset($_POST['envEmail']))
  {
    $nome=  $_POST['name'];
    $emailcliente=  $_POST['emailCliente'];
    $assunto=  $_POST['assunto'];
    $menssagem=  $_POST['message'];
    $to  = 'jun@paconstrushop.com.br'; //can receive notification

    $subject = $assunto;
    $message = 'Email enviado por:'.$nome."\n".$menssagem;
    $headers = 'From:'.$emailcliente. "\r\n" .
        'Reply-To: jun@paconstrushop.com.br' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    if(@mail($to, $subject, $message, $headers))
    {
        echo " <div id=success_message style=width:100%; height:100%;  > <h3>Sent your message successfully!</h3> </div>";
    }
    else 
    {
      echo "<div id=error_message style=width:100%; height:100%;  > <h3>Error</h3> Sorry there was an error sending your form. </div>";
    }
  }

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