Asked  7 Months ago    Answers:  5   Viewed   29 times

i have a jquery Ajax request happening on a page. On php side i am checking if the session is active and doing something. If the session is not active i want to redirect the user to another page in php(header redirect). how do i do it.

I know how to achieve it in javascript(i.e if session_fail then change window.location but is there something that i can do in php/cakephp

 Answers

35

If I understand what you want to happen then this is how I'm implementing it. It's in Prototype instead of jQuery but it shouldn't take you long to translate:

new Ajax.Request('process.php', {
    on401: function(response) {
        var redirect = response.getHeader('Location');
        document.location = redirect;
    }
});

In your PHP, output the following if the session is inactive:

header('Location: http://example.com/login.php', true, 401);
exit;
Wednesday, March 31, 2021
 
aurelijusv
answered 7 Months ago
29

You should check link I refered in comment its your complete answer.

here is a Simple example with your code.

include("DbConn.php");
    // Set alerts as array 
$error     = "";

    // I should just trrim and let you check if email is empty .lol 
if (empty($_POST["email_val"])) {
    $error .= "<p class='error'>Fill email value.</p>";

    //Check if this is a real email 
} elseif(!filter_var($_POST["email_val"],FILTER_VALIDATE_EMAIL)){
    $error .= "<p class='error'>Wrong email type.</p>";
}else{
    $email = mysqli_real_escape_string($conn, $_POST["email_val"]);

    //You should use prepare statement $email, Shame on you .lol    
    $query = "SELECT * FROM customer WHERE email = '{$email}'");
    $result = mysqli_query($conn, $query);
    echo mysqli_num_rows($result);
    $error .= "ok";
}
$data = array(
 'error'  => $error
);

This Jquery :

 $(document).ready(function(){
    $('#myform').submit(function(event){
    event.preventDefault();
    var formValues = $(this).serialize();
        $.ajax({
        url:"includesemailAvailability.php",
        method:"POST",
        data:formValues,
        dataType:"JSON",
            success:function(data){
                if(data.error === 'ok'){
                    $('#result').html(data.error);
                } else {
                    $('#result').html(data.error);
                    $('#myform')[0].reset();
                }
            }
        });
    });
});

And Html :

<form id="myform">
  <input class="input--style-4" id="email" type="email" name="email_val">
  <span id="result"></span>
  <button type="button" class="btn btn-primary">Send</button>
</form>
Wednesday, March 31, 2021
 
Besnik
answered 7 Months ago
10

GWT (Google Web Toolkit) is probably the fastest way for a non-UI Java guy to build a complex, ajax-enabled, dynamic UI.

Saturday, May 29, 2021
 
rorymorris
answered 5 Months ago
52

You can't match URL's http:// part in Redirect directive. Use this instead:

Redirect 301 /ch-de/Brautschmuck/Schmuckset-Miranda http://wedding-shop.com/de/brautschmuck/61-schmuckset-miranda-braut.html
Saturday, August 21, 2021
 
G-Cyrillus
answered 2 Months ago
60

there have been 2 problems in your PHP script:
1. the function was not called
2. the return-value of the function was not echoed.

(in order to respond to an AJAX call, the called url always needs to output something - JSON in the most cases. AJAX can't access PHPand thus can't read PHP return values.)

here is the fixed version:

function update_this_func($remove_option, $uservideo_id){

    global $wpdb;

    $wpdb->query("UPDATE " . $wpdb->prefix."uservideo
                  SET is_removed =" . $remove_option . "
                  WHERE uservideo_id =" . $uservideo_id );

    return json_encode(['status' => 'Updated!']); // return status as json
}
echo update_this_func($_POST['remove_option'], $_POST['uservideo_id']);
Wednesday, September 1, 2021
 
MadProgrammer
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :