j. harsh j. harsh - 2 months ago 7
jQuery Question

how to stay sticky after form submit with jQuery toggle

I have two forms in the same HTML file .

<form id="#login" class="toggling">
</form>

<form id="#register" class="toggling">
</form>


I switch between my forms with

$('.toggling').toggle();


which works fine but the problem is when user submit the form even while user is on registration form (#register) after submit it will be back to (#login) with error messages related to registration process .

I'm using php for backend so I will send error reports using my error_report() function call but I have one controller (PHP) and just one view (HTML) .

now question is, Is there any way to detect and stay let say on #register after I submit the form obviously while I submit login there is no problem as the main form before toggle is #login so i just need stay in #register if active form is #register and show errors in there.

Answer

You can use hidden input field to identify the form which is sending you the request. e.g.

<form id="#login" class="toggling">
    <input type="hidden" name="form_type" value="login" />
</form>

<form id="#register" class="toggling">
     <input type="hidden" name="form_type" value="register" />
</form>

In PHP side you know the type and you send this information back to the view. So $formType variable will be used to identify the form where the error messages need to be displayed.

which works fine but the problem is when user submit the form even while user is on registration form (#register) after submit it will be back to (#login) with error messages related to registration process .

To answer this we also need to know how you are doing the form submit and how you are handling it in PHP server side.

Let us know if this helps you or not. if you still feel confused then update your question by showing us how you are submitting the form and how you are handling it in PHP.

Comments