2

How can I write the php locate variable code when redirect to right page! I have pagination and if I'm in second or 3rd page, and then process sending data to database using ajax like this code

<script type="text/javascript">

$.ajax({
type: "GET",
url: "action.php",
data: { status: "<?php echo $_GET['pid'];?>" }
})

</script>

and then redirect back to pagination.... I want to redirect back to page 2nd or 3rd page instead of first page.

I copied second page of pagination url address and I pasted into php location code is works great see the code...

header("Location: http://shopone/admin45/pagination/davetwo.php?cat=&rows=10&page=2");

See the code where it said ?cat=&rows10 mean number of row per page.

See &page=2 mean page 2

In local variable I know this is not right code but need help

header("Location: http://shopone/admin45/pagination/davetwo.php".($_GET['page'] ? '?page='.$_GET['page']:''));

How can i write the variable code to put variable for cat and page! to redirect into right page where left from pagination page!

  • You're sending a php variable to a php script using javascript to get a variable provided by php? – Jonast92 Apr 15 '14 at 18:52
  • yes that is correct, this because i'm using autosave after made change using ajax, that why is sending processing in other page called action.php but after done with page, then i want back to same page were we left in pagination page. and i can't figure out how to write variable code in locate line... – user3527211 Apr 15 '14 at 19:01

1 Answers1

0

I think I know what you're asking. Here is a snippet that will produce the complete URL for the current PHP script, including the query string:

$full_url = ($_SERVER['HTTPS'] == "on" ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $full_url;

And if you just want the query string on the end, i.e. cat=&rows=10&page=2, you can use:

echo $_SERVER['QUERY_STRING'];

So I believe in your case what you're looking for is something like this:

$full_url = ($_SERVER['HTTPS'] == "on" ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header("Location: ".$full_url);
larsAnders
  • 3,813
  • 1
  • 15
  • 19
  • This is new to me, so please correct me if i'm wrong, i wrote like this see if this is correct... '$protocol='http'; if (isset($_SERVER['HTTPS'])) if (strtoupper($_SERVER['HTTPS'])=='ON') $protocol='https'; header("location: $protocol://".$_SERVER['HTTP_HOST']."/?page= ....");' – user3527211 Apr 15 '14 at 20:06
  • That gets you part of the way there. You would be missing the file name - `davetwo.php` which is provided by `$_SERVER['REQUEST_URI']`. In the header call, you would need `location: $protocol://".$_SERVER['HTTP_HOST']."/".$_SERVER['REQUEST_URI']."?page= ....`. – larsAnders Apr 15 '14 at 20:17
  • I'm not clear why you need a redirect at all. That is the whole point of using ajax - that you can bounce information to and from PHP files without needing to refresh. When and why does the refresh happen? – larsAnders Apr 15 '14 at 20:18
  • thanks for asking, is this relate to pagination, and one of column has link relate to activate or deactivate and what it does do is that if you one of row show activate, and want to change to deactivate, all we have to do is just click on that link it will change text link to deactivate after update into database from activate.php using ajax, so let say that if i'm in second page, update that link to activate or deactivate it will redirect back to first home, what i'm trying to do is to stay same page of second page after update. is where i'm having problem with php locate variable. – user3527211 Apr 15 '14 at 20:24
  • So I'm the user, and I'm on page 2 of the file `davetwo.php`. I click a link to deactivate one, and the file sends an ajax request off to `activate.php`, and that file updates the database. Then the page I'm already on, `davetwo.php` is going to redirect me to page 2? Is that what you're saying? – larsAnders Apr 15 '14 at 20:29
  • almost, is going to redirect me to page ONE not page 2, and I'm trying to fix how to redirect to page 2! another word mean how to redirect back to page where I left from that page. is where i need help with header local code... – user3527211 Apr 15 '14 at 20:36
  • Okay, that's the problem. The whole point of ajax is that you don't have to redirect at all. You need to stop the form from redirecting in the first place. So you also don't have to find the right variable. Check out [this question and answer](http://stackoverflow.com/questions/4038567/prevent-redirect-after-form-is-submitted) – larsAnders Apr 15 '14 at 20:41
  • that new to me that i can't hardly figure out how this works, but i was wondering if that possible if to put update code's from action.php into pagination and not use action.php, if so, what do i need to put in ajax url: address! and also if that possible to put refresh code instead of redirect! – user3527211 Apr 15 '14 at 21:26