I’m trying to use the submit-function in javascript to submit a form. The reason I’m not using a submit button is that I want to ask the user if really wants to submit the form. Here is the code:
<?php
if(isset($_POST['submit1']))
die("Submit successfull!");
?>
<!--Javascript submit question.-->
<script type="text/javascript">
function question_submit() {
if(confirm('Are you sure you want to submit?')) {
return true;
}
else {
alert('You have not submitted this form');
return false;
}
}
</script>
<!--The form-->
<form action='test.php' method='POST'>
<input type='button' name='submit1' value='Submit' onClick="if(question_submit()) { this.form.submit(); }" />
</form>
Result: The popups works great, it just never wants to submit the form and so “Submit successfull!” never shows up.
The message is not even shown when I add a real submit button to the page. It’s like the POST-data won’t work.
But when I add a PHP-code and die-function för the submit-button the thing will work and “REal Submit Works” will show up, which is strange because the buttons code is first.
<?php
if(isset($submit1))
die("Submit successfull!");
if(isset($_POST['realsubmitbutton']))
die("REal Submit Works");
?>
I I’ve also tried to change the onClick to just contain the javascript submit function. Like this: onClick=”this.form.submit();”. But that won’t work either. So my question is, what’s wrong with the code and why won’t it work?
Can’t buttons send it’s value with POST?
I’ve read all your answers and have finished the code. It now works with both clicking on the button or pressing Enter key. The javascript runs correctly either way and the form gets submitted.
This is the code:
Thanks for all the answers and hope this is helpfull for someone.