0

This code works:

<script type="text/javascript">
function submit_form()
{
    if(confirm('My message here.'))
    {
        document.my_form_name.submit();
    }
}
</script>

<form action="index.php" method="post" name="my_form_name">

<input type="button" value="Skip" onclick="submit_form()">
<input name="submit" type="submit" value="Save and continue">

</form>

HIGHLIGHTS:

function submit_form()

document.my_form_name.submit()

form action="index.php" method="post" name="my_form_name"

input type="button" value="Skip" onclick="submit_form()"


This does not work (but I want it to):

<script type="text/javascript">
function submit_form(variable)
{
    if(confirm('My message here.'))
    {
        document.variable.submit();
    }
}
</script>

<form action="index.php" method="post" name="my_form_name">

<input type="button" value="Skip" onclick="submit_form('my_form_name')">
<input name="submit" type="submit" value="Save and continue">

</form>

HIGHLIGHTS:

function submit_form(variable)

document.variable.submit()

form action="index.php" method="post" name="my_form_name"

input type="button" value="Skip" onclick="submit_form('my_form_name')"


I'm quite ok with PHP but lack decent JavaScript knowledge so if someone could point me in the right direction I'd be very happy!

And why do I need 2 buttons? Well, I want to display the skip-button to the left of the continue-button (first in HTML flow) but I do not want it to be default action if form is submitted by pressing enter key, therefore I let skip-button be "just" a button (controlled by JavaScript for submitting) and only the continue-button to be a "real" submit-button...

2 Answers 2

2

When you are using document.variable, it looks for something named variable. If you want it to look for the variable's value ('my_form_name'), use document[variable]:

if(confirm('My message here.'))
{
    document[variable].submit();
}
Sign up to request clarification or add additional context in comments.

Comments

1

@Blex has provided the right answer. Even though I am not a fan of inline JS, the following can save some typing and improve readability of your code:

HTML:

 <input type="button" value="Skip" onclick="submit_form(this)">
 <!-- use this instead of 'my_form_name' -->

JS:

 variable.form.submit(); //instead of document[variable].submit();

Comments

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.