0

I have a strange behaviour in my form, it sends the data from the textboxes but it doesn't send the button data.

This is my form:

<form name="login_registro" method="POST" id="login_registro">
    <input type="text" id="username" name="username" value="">
    <input type="text" id="password" name="password">

    <input type="submit" name="hacer_login" style="width:auto; height:auto; padding:5px;" class="button" onclick="submitForm('{{matches|last}}/entrar')" value="entrar">

    <input type="submit" name="registro" style="width:auto; height:auto; padding:5px;" class="button" onclick="submitForm('{{matches|last}}/registro')"  value="regístrate gratis!" >
</form>

The submit function:

<script type="text/javascript">
    function submitForm(action) {
    document.getElementById('login_registro').action = action;
    document.getElementById('login_registro').submit();
}
</script>

In the two pages (entrar and registro) I did a print_R($_POST); and it only shows the two inputs, username and password. It doesn't shows the button pressed.

If I remove the onclick function, and add an action="page.php" it sends the two inputs plus the button pressed.

I know I can do a workaround, but I would like to know why this happend. Thanks.

PS: I commented all jquery.

2 Answers 2

1

Your problem here is that using onclick and submitting the form, will submit twice because the submit button already sends the form, and you add form.submit()

Use onsubmit="return submitForm('actionName')". If you return true at the end of the function, the form will get submitted. If you return false, the submission will be cancelled.

You can do this more elegantly by not setting the action dynamically and sending it as a field of the form, or parameter (set through the submitForm function), but this implies changes to your server-side code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, I will upvote you, but can't mark you as answered, I added the answer myself. Thanks!!
0
function submitForm(action) {
    document.getElementById('login_registro').action = action;
}

I didn't understand at all your answer @Vicentiu Bacioiu, but you gave me a cue. How you said the form is submitted twice, so removing the line where it submit the form in the function worked for me.

2 Comments

Basically if you have <input type='submit'> when you press that it's going to submit the form. if you add an additional handler to that (a function that does form.submit) that is another submit.
Exactly, that was the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.