0

I am trying to submit values of input forms by click a button only. Generally pressing enter also submit the from, bt I need to prevent that. I only need to submit all input data if button is clicked.

So, I use window.location='insert_expences.php', but in insert_expences.php I can't get the values of input field. I use echo "<pre>"; var_dump($_POST);die;

I want to get all input values only when button is clicked.

Plz help Thanks

  • Use `document.getElementById('FORM_ID').submit()` on button click...and `document.addEventListener('keypress', function(e){ if(e.keyCode == 13) { e.preventDefault(); } })` – Rayon Mar 10 '16 at 09:41
  • should I remove `window.location` –  Mar 10 '16 at 09:45
  • 1
    @RayonDabre—why is the first required? The default behaviour of a submit button is to submit the form, it doesn't need a listener to do that. – RobG Mar 10 '16 at 09:45
  • @Mr.Shrestha, You should remove `window.location...` – Rayon Mar 10 '16 at 09:45
  • @RobG, I assumed OP is dealing with `type = "button"` – Rayon Mar 10 '16 at 09:47
  • There are plenty of answers to this by doing a quick search: http://stackoverflow.com/questions/895171/prevent-users-from-submitting-form-by-hitting-enter – Samuel Cloete Mar 10 '16 at 09:50
  • I add form = 'gothere.php' –  Mar 10 '16 at 09:52

3 Answers3

0
<form onsubmit="return false;">
....
</form>
Bishoy Bisahi
  • 377
  • 1
  • 11
0

You can remove submit and use button:

<input type="button" value="" id="button">

And add listener to button:

$('#button').click(function() {
    $(this).parents('form').submit();
});
jekaby
  • 403
  • 3
  • 13
0

Complete working code to prevent form submission on Enter/Return Key. It will submit the form when user click on the Submit button.

1.php file

<?php
/* Add your php code here if needed */
?>
 <!-- form starts -->
<form action="2.php" method="post" id="regForm">
 <input name="a" type="text" />
 <input name="b" type="text" />
 <input name="sub" type="submit" value="Submit" />
</form>
<!-- form ends-->

 <!-- Insert the code below before </body> Tag -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

 <script>
   $('#regForm').on('keyup keypress', function(e) {
   var keyCode = e.keyCode || e.which;
     if (keyCode === 13) {
       e.preventDefault();
       return false;
     }
  });
 </script>

2.php file

 <?php
//Show the form data that was sent here
echo "<pre>"; var_dump($_POST);
die;
?>

To learn more about PHP, please see the following links

PHP with HTML - PHP 5 Tutorial - learn php

Qammar Feroz
  • 708
  • 8
  • 21
  • `
    ` inside form there are many input text and insert button having `type='button'`
    –  Mar 10 '16 at 10:12
  • `
    `
    –  Mar 10 '16 at 10:16
  • Here is working example [form submission with button only](https://jsfiddle.net/qammar/y3tcek4j/11/) – Qammar Feroz Mar 10 '16 at 10:37
  • @Mr. Shrestha, answer if updated. if this solve the problem? If yes, accept the answer by clicking on green tick mark with the answer. – Qammar Feroz Mar 10 '16 at 12:15
  • u are using `type='submit'`, in fiddle pressing enter key doesn't works..... it is good and i want like that but when i tried in real i.e in 1.php and 2.php pressing enter key also works like clicking submit. In short it doesn't works in real page –  Mar 11 '16 at 05:59
  • I thnk u didn't understand my question. –  Mar 11 '16 at 11:12
  • Just try urself. Copy your code in 1.php and display var_dump($_GET) in 2.php, this will submit form while pressing enter key. –  Mar 11 '16 at 11:15
  • If there are other JavaScript errors/bugs in your code, It will NOT work. Test it in a separate HTML/PHP file. thx – Qammar Feroz Mar 11 '16 at 11:26
  • no there are no other codes. I create 1.php and 2.php to check ur code only. there are not a single word then ur code –  Mar 11 '16 at 11:36
  • u may check by ur self and press enter that will submit for –  Mar 11 '16 at 11:36
  • Answer updated with working php code. I guess, you know, how to execute php scripts. – Qammar Feroz Mar 11 '16 at 12:10