Send form without Ajax and without refreshing the page

1

When you want to submit a form without updating the page, if you think about Ajax, that is a practical way to do it. But would there be another way to do this without using Ajax?

For example, I have the following form:

<form name="form" action="pagina.php" onsubmit="return valida(this)" method="post">
    <input name="nome" type="text" />
    <br />
    <input type="submit" value="Enviar" />
</form>

And it validates the input field with the function:

function valida(i){
    if(!i.nome.value || i.nome.value.trim() == ''){
       alert("Digite um nome");
       return false;
    }

    // validou

}

How to submit this form to the PHP pagina.php page without Ajax and without refreshing the page and still return a confirmation that the form was received?

    
asked by anonymous 08.02.2018 / 03:11

1 answer

0

One way to do this is through a hidden iframe fault:

<form name="form" action="pagina.php" target="pagina" onsubmit="return valida(this)" method="post">
   <input name="nome" type="text" />
   <br />
   <input type="submit" value="Enviar" />
</form>
<iframe src="pagina.php" name="pagina" style="display: none;"></iframe>

Just put a target="pagina" attribute in form (which is name of iframe ) that the form will be sent to it.

Content of pagina.php of iframe :

<?php
$nome = $_POST['nome'];

if(isset($nome) && !empty($nome)){
?>
<script>
alert("O nome <?=$nome?> foi recebido");
</script>
<?php
}
?>
    
08.02.2018 / 04:09