Problem to submit form from Javascript

Asked

Viewed 46 times

-1

I have a form and would like to submit it without reloading the page I am trying it with javascript, when I try to click the button it does not submit the form as I asked

<form  id="myForm" method='GET'>

"; ?>

<script>

Function Submitformdata() { var codefolder = $("#codefolder"). val(); $. post("listxth.php", { codigofolder: codigofolder}, Function(date) { //In case the page saves.php data send some response after receiving the data }); }

1 answer

0


When you do Submit() by javascript, it will reload the page.

An easy way to do this would be using Jquery. Here is an example you can use to change your form:

function SubmitFormData() {
    var nome = $("#nome").val();
    var email = $("#email").val();
    var telefone = $("#telefone").val();
    var sexo = $("input[type=radio]:checked").val();
    $.post("gravar_dados.php", { nome: nome, email: email, telefone: telefone, sexo: sexo },
    function(data) {
	//Para o caso da página gravar_dados.php enviar alguma resposta depois de receber os dados
    });
}
<html>
<title>Enviar sem recarregar a página</title>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/submit.js"></script>
</head>
<body>
  <form id="myForm" method="post">
     Nome:    <input name="nome" id="nome" type="text" /><br />
     Email:   <input name="email" id="email" type="text" /><br />
     Telefone:<input name="telefone" id="telefone" type="text" /><br />
     Sexo:  <input name="sexo" type="radio" value="masculino">Masculino
	      <input name="sexo" type="radio" value="feminino">Feminino<br />

    <input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
   </form>
   <br/>
</body>
</html>

Note that in the example the form does not have a button of type Submit

  • edited the question to try to do with what you propose but could not note in question pf

  • You need to post the HTML of your form, it does not appear in your question.

Browser other questions tagged

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