Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Ajax Force Page refresh

I have here a form where that accepts user credentials. if the user has invalid user credentials the page will not load however if he or she has valid credentials I want the page to do full page refresh how would I achieve that? I have tried adding.

window.location.href = window.location.href; 

On the response html but it did not work, it seems like jquery is removing the script code?

here's the AJAX for form submission.

$('body').on('submit', '#sign-in', function(e) {
    e.preventDefault();

    var data = $(this).serialize();

    var url = $(this).attr('action');
    $.ajax({
        //this is the php file that processes the data and send mail
        url : url,
        type : "POST",
        data : data,
        //Do not cache the page
        cache : false,
        //success
        success : function(data) {
            alert(data);
            $('#loginModal').html($(data).find('#loginModal').html());
        }
    });
})

if the user has valid credentials the response from the success would be this

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
    window.location.href = window.location.href;

</script>
</head>

but the page is not reloading.

like image 357
user962206 Avatar asked May 22 '26 03:05

user962206


1 Answers

In the success function you can use the JavaScript way to reload :

location.reload();

A bit like this

$('body').on('submit', '#sign-in', function(e) {
    e.preventDefault();

    var data = $(this).serialize();

    var url = $(this).attr('action');
    $.ajax({
        //this is the php file that processes the data and send mail
        url : url,
        type : "POST",
        data : data,
        //Do not cache the page
        cache : false,
        //login success
        success : function(data) {
            //... your other code
            location.reload(); //reload the page on the success
        }
    });
})

That implies that your query thorws an error when the login fails so it doesn't go in your success function.

like image 111
Hugo Dozois Avatar answered May 23 '26 17:05

Hugo Dozois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!