vendredi 6 mars 2015

How can I pass variable from the onclick of a button to the javascript code?

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...


Aucun commentaire:

Enregistrer un commentaire