Send form without Ajax and without updating page

Asked

Viewed 769 times

2

When you want to send a form without updating the page, you think of Ajax, which is a practical way to do this. But there would be another way to do it 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 worth the field input with the function:

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

    // validou

}

How to send this form to the PHP page pagina.php without Ajax and without updating the page and still return a confirmation that the form has been received?

  • I decided to delete - temporarily - my answer. " But is there any other way to do this without using Ajax (using Javascript or jQuery)?" I understood that it would be without jQuery’s Ajax and not without the "Asynchronous Javascript And XML".

  • But yes, there is a way to do this. There is even a site that has created a Chat no software for users, no firewall or proxy problems, no dynamic HTML, no Javascript or Java, no Flash and no reload. http://zesty.ca/chat/ Reference: http://www.aaronsw.com/weblog/ajaxhistory

1 answer

0


One way to do this is through a iframe occult:

<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 an attribute target="pagina" in the form (which is the name of iframe) that the form will be sent to him.

Content of pagina.php of iframe:

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

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

Browser other questions tagged

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