Reputation: 9
Good day!
Trying to refresh the page after submission of values with ajax and php function (to db). Tried doing it via ajax request success method, but it didnt seem to work.
javascript
function saveQuestionnaire() {
/* some code */
$.ajax({
type: "POST",
url: "createquestionnaire.php",
data: {toDbQuestionnaire: toDbJson},
success: function(data){
window.location.reload();
}
});
}
createquestionnaire.php
require("functions.php");
$obj = $_POST['toDbQuestionnaire'];
$obj = json_decode($obj);
if (isset($obj) && isset($obj->name) && $_SESSION['email']) {
$Question->createQuestionnaireWithNameAndEmail($obj->name, $_SESSION['email'], $obj->quiz);
header('Content-Type: application/json');
echo json_encode("{result: true}");
}
The following function after that goes through properly and pushes values to DB. Although, to view the changes on the page, i need the page to refresh. How do i achieve that? As you can see, window.location.reload()
didn't quite work.
---- EDIT ----
Apparently, this works like a charm. Why am i getting an error though?
error: function(data){
window.location.reload();
}
Upvotes: 0
Views: 2953
Reputation: 528
Use location.reload instead of window.location.reload in success
Upvotes: 0
Reputation: 9
really sorry everyone. the reason for error was this part in createquestionnaire.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Upvotes: 0
Reputation: 324
can you change your createquestionnaire.php file like this,
require("functions.php");
$obj = $_POST['toDbQuestionnaire'];
$obj = json_decode($obj);
if (isset($obj) && isset($obj->name) && $_SESSION['email']) {
$Question->createQuestionnaireWithNameAndEmail($obj->name, $_SESSION['email'], $obj->quiz);
header('Content-Type: application/json');
$result = array('result'=>true);
echo json_encode($result );
}else{
$result = array('result'=>false);
echo json_encode($result );
}
Upvotes: 1
Reputation: 27513
success: function(data){
if(data.success == true){ // if true (1)
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 1000);
}
}
use this in the success part
Upvotes: 1