Submit button giving error - PHP

Asked

Viewed 575 times

0

I am putting a form in my index.php, to be sent the completed data to my email.

How do I stop when the person clicks the Submit(submit) button, the page nay upgrade?

The system is working fine, send it to my e-mail correctly, but when I click on the send button, it looks like.

FOTO

Does anyone know why you’re making this mistake?

index php.

<?php
$msg=0;
@$msg= $_REQUEST['msg'];
?>

<!DOCTYPE html>
<html lang="pt-br">
<head> etc...

<body>
<form action="processaForm.php" method="post">
        <label for="nome">Nome:</label><br>
        <input id="nome" name="nome" type="text" required><br>
        <label for="email">E-mail: (obrigatório)</label><br>
        <input id="email" type="email" name="email" required><br>
        <label for="telefone"> Telefone/Whatsapp:</label><br>
        <input type="text" name="telefone" id="telefone" value="27 " required><br>
        <label for="assunto"> Descreva o serviço que deseja:</label><br>
        <textarea name="assunto" id="mensagens assunto"> </textarea><br>
        <input type="submit" id="miniSuccessAnimation" class="btn btn-success">
    </form>
</body>

PHP

<?php 
$para= "[email protected]";
$assunto= "Contato pelo Site";
$nome= $_REQUEST['nome'];
$fone= $_REQUEST['telefone'];
$email= $_REQUEST['email'];
$msg= $_REQUEST['assunto'];

    $corpo = "<strong> Mensagem de Contato</strong><br><br>";
    $corpo .= "<strong> Nome: </strong> $nome";
    $corpo .= "<br><strong> Telefone: </strong> $fone";
    $corpo .= "<br><strong> Email: </strong> $email";
    $corpo .= "<br><strong> Mensagem: </strong> $msg";

    $header = "Content-Type: text/html; charset= utf-8\n";
    $header .="From: $email Reply-to: $email\n";
mail($para,$assunto,$corpo,$header);
header("location:index.php?msg=enviado");
?>
  • Post your code, help us help you.

  • Okay, I’ve entered the code.

  • That mistake is because the path of <form> or of hader("location:") you’re probably wrong

1 answer

2


Solution Do Not Reload Page (You Need jQuery.js Instantiated):

HTML

<form>
    <label for="nome">Nome:</label><br>
    <input id="nome" name="nome" type="text" required><br>
    <label for="email">E-mail: (obrigatório)</label><br>
    <input id="email" type="email" name="email" required><br>
    <label for="telefone"> Telefone/Whatsapp:</label><br>
    <input type="text" name="telefone" id="telefone" value="27 " required><br>
    <label for="assunto"> Descreva o serviço que deseja:</label><br>
    <textarea name="assunto" id="assunto"> </textarea><br>
    <input type="button" id="miniSuccessAnimation" class="btn btn-success">
</form>

JS

$(document).ready(function () {
    $("#miniSuccessAnimation").click(function () {
        var request = $.ajax({
            url: "processaForm.php",
            method: "POST",
            data: {nome: $("#nome").val(), email: $("#email").val(), telefone: $("#telefone").val(), assunto: $("#assunto").val()}
        });

        request.done(function (data) {
            alert(data);
        });

        request.fail(function () {
            alert("OPS ocorreu um erro na requisição.");
        });
    });
});

PHP

<?php 
$para= "[email protected]";
$assunto= "Contato pelo Site";
$nome= $_REQUEST['nome'];
$fone= $_REQUEST['telefone'];
$email= $_REQUEST['email'];
$msg= $_REQUEST['assunto'];

    $corpo = "<strong> Mensagem de Contato</strong><br><br>";
    $corpo .= "<strong> Nome: </strong> $nome";
    $corpo .= "<br><strong> Telefone: </strong> $fone";
    $corpo .= "<br><strong> Email: </strong> $email";
    $corpo .= "<br><strong> Mensagem: </strong> $msg";

    $header = "Content-Type: text/html; charset= utf-8\n";
    $header .="From: $email Reply-to: $email\n";
    if(mail($para,$assunto,$corpo,$header)){
        echo "E-Mail enviado";
    }else{
        echo "E-Mail não enviado";
    }

?>
  • Hey, Uilherme. Because I put header("Location:index.php?msg=sent"); in my php, the page is being updated in the same way. and if I take it off, when I click to send, the page is blank.

  • Did you replace your code exactly as I put it above? Did you include jQuery in the header? When you click on the button appears some message Alert() on the screen?

  • I already had the jquery attached to the page, because I use other JS. There appeared an Alert, opened an all white page written "sent email". Remember that, I already put an "animation" when you click the send button. Open a pop-up on the side, I will update the post for you to see the photo

  • Since I already put this, I don’t need anything to appear on the screen, I just need it not to update, for the pop-up to appear and be able to be viewed

  • Then there is something wrong, because the script I put took the action form because the page cannot be updated... I think you forgot to copy something friend... When you clicked the button in case the email was sent an Alert with the success message would appear, otherwise error...

  • yes, I put it exactly as it is and it didn’t work, it goes to a new page all blank, with the message "E-mail sent"

  • Can you edit the code by placing not to appear any Alert? just do not refresh the page when clicking the button, being sent or not.

  • So, if you don’t want Alert just delete it, but from what you told me the page still reloads... I’m confused because I tested it here and is not loading the page...

  • this if Else that you put in php, would be for what? this is what you are doing open a new page with the text "email sent"

  • If the MAIL function can send the email it shows "Email sent" otherwise it shows the other message.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.