-1

i try to work on something but it's not realy working as i want, so first we have the index page with the ajax code :

function redirect(){
        $(".redirect").load("redirect.php");
    }

setInterval(function(){
    redirect()
}, 3000);
</pre>

Of course in the body we have this :

<div class="redirect"></div>

In the redirect.php code, we have this :

  <?php
session_start();
include('db.php');
  $query = "SELECT * FROM db WHERE client=".$_SESSION['clientvict']."";
  if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $redirect = $row["redirect"];
        if($redirect == 1) {
//header('location: '.$_SESSION['redirect_url']);
        }
      }
    }

Okay so, to start, when my client is on the index.php, the row redirect is on 0. And on my web panel, i can set up the value to 1, if the value is on 1, i want the client to be redirected of the index page to my $_SESSION['redirect_url'].

But the problem is, of course, it redirect only in the class="redirect" div. But i want him to be redirected from the index page, so i tried in the redirect.php code this :

  <?php
session_start();
include('db.php');
  $query = "SELECT * FROM db WHERE client=".$_SESSION['clientvict']."";
  if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $redirect = $row["redirect"];
        if($redirect == 1) {
$ok = 1;
        }
      }
    }

And on the index.php page i added this below the class redirect div :

<?php
if($ok == 1) {
header('location: url');
}
?>

But it doesn't detect the $ok from the redirect.php. Any idea how i could fix this problem ? Thank !

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 1
    _“But it doesn't detect the $ok from the redirect.php”_ - what should it “detect”, you did not produce _any_ output inside of that script. – CBroe Apr 27 '21 at 10:48
  • `if($ok == 1)` as part of your `index.php` does not make sense to begin with here. Your `index.php` did not make the request to `redirect.php` - your browser did, it was an AJAX request. So your `index.php` doesn’t know anything about the result of this request … – CBroe Apr 27 '21 at 10:51
  • @CBroe yes im learning ajax and now i understand why it doesn't work, but how i could do the thing i want ? it have to be refresh every 3 seconds, i looked for refreshing every 3 seconds a include, but everyone say use ajax, but it's not realy working with the thing i want to make – ANhry VOma Apr 27 '21 at 10:52
  • This sounds like you should probably go and have a good read of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429/1427878) first of all, because it doesn’t give the impression that you actually understood those essential basics yet. – CBroe Apr 27 '21 at 10:52
  • You should do the redirect via JavaScript, from within the callback of your AJAX request. https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – CBroe Apr 27 '21 at 10:54
  • @CBroe Thank i took a look, but there is still something i dont realy understand, how the ajax will detect the mysqli request ( to see if the redirect value is 1 or 0), i try to understand but i realy dont know how to make it.. – ANhry VOma Apr 27 '21 at 11:08
  • First of all, you will need to make a _proper_ AJAX request here, with a callback function that gets the output of your PHP script passed into it - _not_ use `$.load()`. And your PHP script needs to create some _output_, based on the result of your database query, so that the JS then can decide what to do, based on that. – CBroe Apr 27 '21 at 11:13
  • @CBroe Great i think i understand on what i have to work, thank for your help ! – ANhry VOma Apr 27 '21 at 11:18
  • Please have a look at prepared statements to avoid SQL querys that are open to injection – Nico Haase Apr 27 '21 at 11:56

1 Answers1

0

Okay i resolved the problem, i don't know if it's the proper way to do it, but it's working as i want ! i did that :

redirect.php :

  <?php
session_start();
include('db.php');
  $query = "SELECT * FROM db WHERE client=".$_SESSION['ccvict']."";
  if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $redirect = $row["redirect"];
        if($redirect == 1) {
echo '<div id="content"><div id="activity">1</div></div>';
        }
else {
echo '<div id="content"><div id="activity">0</div></div>';
}
      }
    }

And the AJAX in index.php :

    <script>
        $(document).ready(function() {
function redirect(){
        $.ajax({
   url:'redirect.php',
   type:'GET',
   success: function(data){
       var active = $(data).find('#activity').html();
if(active == 1) {
<?php echo 'window.location.replace("'.$_SESSION['redirect_payment'].'");'; ?>
}
   }
});

    }
setInterval(function(){
    redirect()
}, 3000);

        });
    </script>

Hope it will help, thank @CBroe