0

For some reason this form keeps refreshing the page and I have no idea why as I have another form on the page with a different id but same script and it doesn't refresh. I have tried using preventDefault(); as well but it doesnt work either. Can someone please tell me where I have gone wrong in this form or script?

Thanks

Top of page

<script type="text/javascript" src="../JS/jquery.js"></script>
<script type="text/javascript" src="../JS/jquery-ui.js"></script>
<script type="text/javascript" src="../JS/member_info.js"></script>

The Form

<form id="central">
<table width="40%" class="search">
<tr>
<td width="39%" align="left"><p><b>Inception Date:</b></p><input size="10" maxlength="10" id="creddate" type="text" value="<?php echo $creddate; ?>" /></td>
<td width="41%" align="left"><p><b>Surety:</b></p><input size="15" maxlength="15" id="surety" type="text" value="<?php echo $surety; ?>" /></td>
<td width="20%" align="left"><p><b>Credit Limit:</b></p><input size="15" maxlength="15" id="creditlimit" type="text" value="<?php echo $credlimit; ?>" /></td>
</tr>
</table>
<br />
<table style="margin:0 auto">
<tr>
<td><input type="hidden" id="code" size="12" readonly="true" value="<?php echo $code; ?>" /></input></td>
<td><input type="submit" value=" Save " /></input></td>
<td><p align="center" id="central_status"></p></td>
</tr>
</table>
</form>

The Script - Linked from member_info.js

$(document).ready(function(){
  $("form#central").submit(function() {
    var code = $('#code').val();
    var inception = $('#creddate').val();
    var surety = $('#surety').val();
    var credit = $('#creditlimit').val();
    $.ajax ({
      type: "POST",
      url: '../members/edit_central.php',
      data: {code: code, creddate: creddate, surety: surety, creditlimit: creditlimit},
      success: function(data) {
        $('#central_status').html(data);
      }
    });
    return false;
  });
});
Sina R.
  • 1,781
  • 2
  • 19
  • 37
bass71982
  • 75
  • 1
  • 10

3 Answers3

3

Make sure you preventDefault() immediately after the form is submitted. Don't do anything else before you do that (like calling Ajax).

E.g.:

$("form#central").submit(function(e) {
    e.preventDefault();

    // Your other long running (possibly async)
    // operations here
}

Also, make sure you are not adding the form dynamically to the page. If you do so, you need to attach the submit event live. Like so:

$(document).on('submit', 'form#central', function(e){
    // same as above
});
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
0

use input type button insted of submit

<input type="button" value=" Save " name="saveButton" id="saveButton" />
ratnesh dwivedi
  • 352
  • 2
  • 8
0

in simple return false at the end of the click event handling do the trick for example :

$('form input[type="submit"]').click(function() {
....
return false;
})
Sidux
  • 557
  • 1
  • 5
  • 15