How to submit form and how to leave without refresh

Asked

Viewed 45 times

-1

I’m trying to send my form to my email, but it only returns the message "No Arguments Provided!". I don’t know what I’m missing.

And I would also like to know how to send the form and appear the sent message in index.html itself without refreshing the page or go to another page. And after sent, clear the form. Could someone give me a little help please?

form - index.html

<form action="mail/contact_me.php" method="POST" name="sentMessage" id="contactForm" novalidate>
      <div class="control-group form-group">
        <div class="controls">
          <label>Nome:</label>
          <input type="text" class="form-control" id="nome" required
            data-validation-required-message="Por favor, coloque seu nome.">
          <p class="help-block"></p>
        </div>
      </div>
      <div class="control-group form-group">
        <div class="controls">
          <label>Telefone:</label>
          <input type="tel" class="form-control" id="telefone" required
            data-validation-required-message="Coloque seu telefone.">
        </div>
      </div>
      <div class="control-group form-group">
        <div class="controls">
          <label>Email:</label>
          <input type="email" class="form-control" id="email" required
            data-validation-required-message="Preencha seu email.">
        </div>
      </div>
      <div class="control-group form-group">
        <div class="controls">
          <label>Mensagem:</label>
          <textarea rows="5" cols="100" class="form-control" id="mensagem" required
            data-validation-required-message="Digite sua mensagem" maxlength="999" style="resize:none"></textarea>
        </div>
      </div>
      <div id="success"></div>
      <!-- For success/fail messages -->
      <button type="submit" class="btn btn-primary float-right" id="sendMessageButton">Enviar Mensagem</button>
    </form>

email page - contact_me.php

    <?php
// Check for empty fields
if(empty($_POST['nome'])      ||
   empty($_POST['email'])     ||
   empty($_POST['telefone'])     ||
   empty($_POST['mensagem'])   ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
   echo "No arguments Provided!";
   return false;
   }

$name = strip_tags(htmlspecialchars($_POST['nome']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['telefone']));
$message = strip_tags(htmlspecialchars($_POST['mensagem']));

// Create the email and send the message
$to = '[email protected]'; // Adicione seu endereço de e-mail entre o '' substituindo [email protected] - Este é o lugar para onde o formulário enviará uma mensagem.
$email_subject = "Contato via site | Web Net Informática:  $name";
$email_body = "Você recebeu uma nova mensagem do formulário de contato do seu site.\n\n"."Aqui são os detalhes:\n\nNome: $name\n\nEmail: $email_address\n\nTelefone: $phone\n\nMensagem:\n$message";
$headers = "De: [email protected]\n"; // Este é o endereço de e-mail da mensagem gerada. Recomendamos usar algo como [email protected].
$headers .= "Reply-To: $email_address";   
mail($to,$email_subject,$email_body,$headers);
return true;         
?>

2 answers

1

Hello.

You need to put the parameter name in the attribute name of input. Is giving No arguments Provided! because there are no inputs with the names you expect on the server. For example, in the name input you should put

<input type="text" class="form-control" id="nome" required name="nome"
            data-validation-required-message="Por favor, coloque seu nome.">

To request without giving refresh on the page you have to use ajax. Take a look over ajax on those links W3S MSN

0

The problem is that none of the form fields have a name. In fact you put id’s instead of name. It is the value of the attribute name that is received in the backend. If id’s have no function, exchange them for name, for example:

instead of id="nome", place name="nome", That in every field.

If the id’s have any function, just add the name also, for example:

<input type="text" class="form-control" id="nome" name="nome" required...

Now, to submit the form without refreshing the page, you can use AJAX. If you use jQuery, see documentation, or with pure Javascript you can consult this documentation as a basis. You can also search here in the search field for "AJAX" which has plenty of material and examples available.

Browser other questions tagged

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