Jorge Bellido
Jorge Bellido

Reputation: 79

Form in not sending the button data

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.

Upvotes: 0

Views: 82

Answers (2)

Jorge Bellido
Jorge Bellido

Reputation: 79

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.

Upvotes: 0

Vicentiu Bacioiu
Vicentiu Bacioiu

Reputation: 81

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.

Upvotes: 1

Related Questions