Note that there are some explanatory texts on larger screens.

plurals
  1. PO$.post keeps redirecting me
    text
    copied!<p>I have problem with jquery $.post function. I use this function a lot and same function works fine except in one case and I don't understand why it keeps redirecting me after script was executed, here is js code:</p> <pre><code>var record; $('.delbtt').on('click', function(e){ e.preventDefault(); var deleteid = $(this).parent().parent().find('#id').text(); record = $(this).parent().parent(); $('#delrecord').empty().val(deleteid); }); $(document).on('submit', '#delteform', function() { var formData = $(this).serialize(); $.post('includes/delete.php',formData,processData); function processData(data){ record.remove(); }; }); </code></pre> <p>HTML:</p> <pre><code>&lt;!-- Modal --&gt; &lt;div class="modal fade" id="deleteFormModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt; &lt;div class="modal-dialog"&gt; &lt;div class="modal-content"&gt; &lt;form id="deleteform" class="form-horizontal" role="form" action="includes/delete.php" method="POST"&gt; &lt;!-- --&gt; &lt;div class="modal-header"&gt; &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;times;&lt;/button&gt; &lt;h4 class="modal-title"&gt;Delete record&lt;/h4&gt; &lt;/div&gt; &lt;div class="modal-body"&gt; &lt;p&gt;Are you sure you want to delete this person?&lt;/p&gt; &lt;/div&gt; &lt;input type="hidden" id="delrecord" name="delrecord" value="" /&gt; &lt;div class="modal-footer"&gt; &lt;button type="button" class="btn btn-default empty" data-dismiss="modal"&gt;Cancel&lt;/button&gt; &lt;button id="delentry" type="submit" class="btn btn-danger"&gt;Delete Person&lt;/button&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt;&lt;!-- /.modal-content --&gt; &lt;/div&gt;&lt;!-- /.modal-dialog --&gt; &lt;/div&gt;&lt;!-- /.modal --&gt; </code></pre> <p>I know that function is executed successfully, because if I put <code>alert("Why is this not working?");</code> after <code>record.remove();</code>, I can see alert and in background code executed, but instead of staying at the same page it redirects me to 'includes/delete.php'. I tried disabling all other $.post functions that I have in my JS, I tried using <code>$('#deleteform').submit()</code>, I tried putting it outside of my main <code>$(document).ready(function() {});</code> same results... Always same result it redirects me after function completes instead of staying on a page. Does anybody have idea why am I getting this behavior?</p> <p>php code:</p> <pre><code>&lt;?php include('budgetprop/Initialize.php'); Database::GetInstance()-&gt;ConnectToServer($errors); $record = $_POST['delrecord']; # CONNECT TO DB if(Database::GetInstance()-&gt;ConnectToServer($errors)){ $insertSQL1 = "DELETE FROM salaries WHERE record_id = '".$record."' "; $stmt1 = sqlsrv_query(Database::GetInstance()-&gt;databaseConnection, $insertSQL1); # IF THERE IS AN ERROR if(!$stmt1){ echo "Error, cannot delete record"; }else{ Database::GetInstance()-&gt;LastInsertId($stmt1); } # IF ALL QUERIES WERE SUCCSESSFUL, COMMIT THE TRANSACTION, OTHERWISE ROLLBACK if($stmt1 &amp;&amp; !$errors){ sqlsrv_commit(Database::GetInstance()-&gt;databaseConnection); # FREE THE STATEMENT Database::GetInstance()-&gt;FreeDBStatement($stmt1); echo "success"; return true; }else{ sqlsrv_rollback(Database::GetInstance()-&gt;databaseConnection); # FREE THE STATEMENT Database::GetInstance()-&gt;FreeDBStatement($stmt1); return false; } # NO CONNECTION WAS MADE }else{ return false; }; ?&gt; </code></pre> <p><code>preventDefault()</code> doesn't work either</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload