Message request in file . php

Asked

Viewed 85 times

-2

I created an email form, it’s functional. I just wish I knew how to return the message echo('Email enviado com sucesso'). Inside my html index.

For example:

<form method="POST" action="./email.php"> 
                <label>Digite seu nome:
                   <input id="name" type="text" name="name" value="" placeholder="Nome" required>
                </label>
<input id="btn_enviar_email" class="send" type="submit">Enviar</input>

When submitting logically opens on a new page with the email message sent. I would like to return this value and treat within my index. In this case, without opening a new guide to this.

Mensagem de email não enviado.

  • To not leave the page change the <input type="submit"> by a <input type="button"> with the event onClick pointed to a function that makes a request or JQuery.ajax or XMLHttpRequest directed to email.php with the email data. To handle the messages just seeing the php code to give an opinion.

1 answer

-1

Add an id or class to your form to call it in a JS file, in the example I will pass you the resolution with Jquery, assuming your ID is send-email, you can remove the action as well

$('#enviar-email').submit(function (e) {
 $.ajax({
  url: './email.php',
  type: 'POST',
  data: new FormData(this),
  dataType: 'json',
  processData: false,
  contentType: false,

  error: function (jsonRetError) {
   console.log(jsonRetError);
  },
  success: function (jsonRetSucess) {
   alert(jsonRetSucess);
  }
});
});

In turn your email.php file will do as follows if the email is sent

$retorno['mensagem'] = "E-mail enviado";
echo json_encode($retorno);

Browser other questions tagged

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