How do I get back a Boolean after a form.Ubmit?

Asked

Viewed 584 times

1

I have a customer registration form that runs Submit on the sign up button:

document.getElementById("formularioCadastro").submit();

everything ok, the registration works, but I wanted to return a BOOLEAN true or false, to know if it was registered successfully or gave error.

I tried the following code:

var sucess = document.getElementById("formularioCadastro").submit();

if(sucess == true){
    alert("cadastrado com sucesso!");
else{
    alert("erro ao cadastrar cliente");
    }
}

But it didn’t work, someone can help me?

  • When the form submission happens, it is not directed to the page defined in action of the element form? Or you’re making an asynchronous request?

  • To know if it was successfully registered in fact the answer has to come from the server, only the front is not possible to say that it was "registered" successfully.

  • @Andersoncarloswoss my action calls a Servlet, which registers the customer, and when the method ends I do not know whether it was successful or failed.

  • @Lucascosta, in my Rvlet I have the second stretch at the end of the method: request.setAttribute("sucess", "true");

  • A tip would be to make Submit without updating the screen and call Servlet by ajax, becomes more subtle for the user and make Servlet return an http status: 500 to error or 200 to success. Automatically falls into ajax success or error. Furthermore, it is difficult to play to help you with what @20comer

  • @Lucascosta is just as simple as you see, that’s all. I don’t think I need to use AJAX to accomplish this task, so I just need to get the return of a variable from Let.

Show 1 more comment

1 answer

1


You can solve this using the method ajax of JQUERY.

   $.ajax({

        type: 'POST',
        url : 'ServletName',//aqui você poe o nome do servlet
        async: false,
        data: { param1      :   value1,  //aqui
                param2      :   value2,  //vao
                param3      :   value3,  //os
                param4      :   value4}, //parametros

    success : function(){ //código da função caso sucesso

            alert("Sucesso");

                            },
    error: function(){ //código da função caso de erro

            alert("Falha!"); 

                         }
    );
  • 1

    I’ll test that method, thank you for the comment!

Browser other questions tagged

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