artaxerxe
artaxerxe

Reputation: 6421

AJAX reload page with POST

Can anybody tell me how to refresh the current page with JavaScript, having a POST variable modified or added?

To be clear, I want to set some POST variables prior to reloading the page.

Upvotes: 19

Views: 153344

Answers (5)

Takuchi
Takuchi

Reputation: 1

Got trouble making it too but it will be better if you don`t use ajax.

<script>
$(document).ready(function(){
SelectOnChange = document.querySelector("#payment"); //for <select> didn`t include it...
SelectOnChange.onchange = function(){
    SelectedValue = SelectOnChange.value;
    document.querySelector("#payment_schedule_date").value = SelectedValue;
    document.getElementById('paymentform').submit(); //<form>
}
});
</script>

In HTML just put input with hidden and use .submit() to submit using form.

<form name="paymentform" id="paymentform" action="#" method="post">
<input hidden name="payment_schedule_date" id="payment_schedule_date" value="">

Upvotes: 0

Danil
Danil

Reputation: 5191

There's another way with post instead of ajax

var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

Upvotes: 1

jiten
jiten

Reputation: 5264

Reload the current document:

 <script type="text/javascript">
 function reloadPage()
 {
   window.location.reload()
 }
 </script>

Upvotes: 5

mapet
mapet

Reputation: 878

By using jquery ajax you can reload your page

$.ajax({
    type: "POST",
    url: "packtypeAdd.php",
    data: infoPO,
    success: function() {   
        location.reload();  
    }
});

Upvotes: 42

pho
pho

Reputation: 25490

If you want to refresh the entire page, it makes no sense to use AJAX. Use normal Javascript to post the form element in that page. Make sure the form submits to the same page, or that the form submits to a page which then redirects back to that page

Javascript to be used (always in myForm.php):

function submitform()
{
  document.getElementById('myForm').submit();
}

Suppose your form is on myForm.php: Method 1:

<form action="./myForm.php" method="post" id="myForm">
    ...
</form>

Method 2:

myForm.php:

<form action="./myFormActor.php" method="post" id="myForm">
    ...
</form>

myFormActor.php:

<?php
    //all code here, no output
    header("Location: ./myForm.php");
?>

Upvotes: 7

Related Questions