Do not redirect form - nodejs

Asked

Viewed 109 times

0

I have a form where I send requests POST pro nodejs save in the database but when I send the information it is redirected to the route and I receive the res

{"fieldCount":0,"affectedRows":1,"insertId":7,"serverStatus":2,"warningCount":0,"message":"","protocol41":true,"changedRows":0}

Only that I need it to continue on the form page and just send an alert to the user saying that it was successfully registered, as I do it?

  router.post("/reportar", (req, res) => {
    const latitude = req.body.latitude.substring(0, 10);
    const longitude = req.body.longitude.substring(0, 11);
    const descricao = req.body.longitude.substring(0, 700);
     execSQLQuery(
      `INSERT INTO erros (latitude, longitude, descricao) VALUES ('${latitude}','${longitude}','${descricao}')`,res);
  });

<form id="contacts" autocorrect="off" name="adicionar" autocomplete="off" method="post" action="http://192.168.0.30:3000/reportar">

<input id="valorlat" type="text" name="latitude" value="">
<input id="valorlong" type="text" name="longitude" value="">
<textarea class="input-field" class="textarea1" name="descricao">
<button type="submit"> Enviar </button>

</form>
function mySubmitFunction(e) {
  e.preventDefault();
  $.ajax({
    url:"http://192.168.0.30:3000/reportar",
    success: function(response){
      alert("Banheiro adicionado com sucesso")
    }
});
  return false;
}
  • Sorry, I didn’t get it very well. You have an HTML form and a Submit button?

  • this, only when I click on Ubmit he enters this screen, and I wanted him to be on the form screen

  • Could you please put the HTML part of the form code and the method that is called when submitting the form?

  • Basically, just indicate that the default behavior is not executed, but to explain better I need to know how the structure of your form is.

  • I updated my form. It sends and does everything right, only instead of it redirecting to the page that shows that first line of code, I want it to be on the form page but a warning to the user that it was successfully registered or that there was a problem when registering, but without redirecting to another page

2 answers

0

You can specify a method to be executed when submitting the form

<form
   id="contacts"
   autocorrect="off"
   name="adicionar"
   onsubmit="return mySubmitFunction(event);" >

And in implementing the method you prevent the default redirect behavior and make the request, manipulate the response, etc.

function mySubmitFunction(e) {
  e.preventDefault();

  // aqui você faz a requisição

  // faz o alert

  return false;
}
  • could not, as would be the request?

  • I edited there as I did

-2

I don’t know how the code is but I could try to take the action from the tag and make an ajax request inside your mySubmitFunction. Note: Keep the preventDefault

  • I updated there, but it didn’t work, have to see what I’m doing wrong?

Browser other questions tagged

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