Redirect with jQuery and PHP

Asked

Viewed 118 times

1

I have two pages where one is the .js calling via ajax a PHP routine to validate a user: https://pastebin.com/37RQtjUY

Another page in PHP that is the call of this previous routine where I will validate the user by his login and password to refer it to the rest of the site: https://pastebin.com/SExFjj3V

On line 23 and 24 I make a insert on a table of log, however, when I do this, I have the JSON return to the page that called that routine; on the page .js this return of PHP comes right, only on line 23 we have a redirect to the index_adm.php, but jQuery isn’t doing it.

When I comment on line 23 and 24 (i.e., the insert on the table of log in https://pastebin.com/SExFjj3V) Redirecting works well. Only when I insist on doing this insert is that it doesn’t work. I don’t understand why this.

  • You can post the file containing that line

  • in the element inspector, you can see difference in sponses with or without line, suddenly you are returning a true, a special character or even an error, if you manage to inspect element, go to network and perform the action, you will see the call a request and a sponse, try to analyze the differences....

  • Leave the INSERT uncommented and in the file . js comment the redirect, and put console.log(response); in the if and see what appears on the console.

  • I’ll see and return

  • I hadn’t thought about it, when you talk about element inspectors, are you talking about what’s the extent of Chrome? I use Fiddler to see the requests, return, ajax, but I do not use all resources, for lack of knowledge

  • OK I used this and did not point out any error, I do not know how to post an image here, I made one of the Fiddler that I use

  • Could post here the shows the console?

  • See I’m using Fiddler, here is the link of the image https://snag.gy/91wZ8t.jpg, I think it points to an error, but sincerity I’m not able to understand. I used snag, because I don’t know if it’s possible

Show 3 more comments

1 answer

0

Why don’t you try "prepare" and "execute" to see if php is not generating an error qdo vc does the Insert?

        $erros = array();

        $sql_in ="INSERT INTO log_user_on (fk_id_user_sistema,log_user_on_login_data) VALUES ( ?, ? ) ";

        // prepare 
        if ($stmt = $conn->prepare($sql_in)){

            //bind
            $stmt->bind_param("ss", $id_user, $hoje );

            //execute
            if ( $stmt->execute() ) {

                // echo "\n New records created successfully";

            }else{
                $erros["execute_error"] = $conn->error;
            }

            //close
            $stmt->close();

        }else{

            $erros["prepare_error"] = $conn->error;

        }

        $conn = null;

        $response = array("success" => true,"login"=>$varLogin,"id_user"=>$id_user,"token"=>$token, "erros"=>$erros);

        echo json_encode($response);
  • I hadn’t thought about it, I’m a beginner in php and even more with Pdo, I’ll try thank you

  • did not result, pointed no error

  • see I used Fiddler and it seems that it is pointing to some error, https://snag.gy/91wZ8t.jpg

  • If login is returning "1" and errors are "null", there is no problem in your php. If you are not redirecting with jquery $(location).attr('href'...), try with pure javascript: location.href = 'index_adm.php?id='+id_user+'&token='+token

  • Okay, I’ll try to do that, I didn’t think of that, thank you

  • Look I tried this and still continues the mystery of not doing, really I’m already thinking of looking for an alternative to this

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.