AJAX Transferring page PHP

This is the structure I have:

A router.php, based on $_POST['c'] generates the controller file name, includes the file and calls the $_POST['a'] method of it.

I have a gridview. The only way to open a details page when the user clicks the row is to use AJAX, since only javascript can handle the clicking. I don't want to have a button column - I want a row click.

So I have my javascript doing this:

$( document ).ready(function() {
  // Handler for .ready() called.
$("tr" ).click(function() {

  var id = $('td[name=id]').attr('id');
  var c = $('input[name=c]').attr('value');
  var a = $('input[name=a]').attr('value');

      $.post("Router.php", { id: id, c: c, a: a }, function(data){
        window.location.href = "Router.php";
        });
    });
});

I have my Router.Php:

if (array_key_exists('c', $_POST)) {
        $controller = $_POST['c'];
    };

    if (array_key_exists('a', $_POST)) {
        $action = $_POST['a'];
    };

echo $controller; 
echo $action;

I can never get the post values. I think I'm thinking about AJAX wrong - what AJAX is suppose to do is to just get a response and render it somewhere on the browser. What I'm using it for is to pass POST data and call a method within a PHP file. The reason for this is because I don't want to pass the id in the url.

Any way to fix this?

Answers


The following part of your code issues 2 requests to the server. A POST and then a GET.

$.post("Router.php", { id: id, c: c, a: a }, function(data){
    window.location.href = "Router.php";
});

Most probably you are getting what you want on the first request, but the result that you see on your screen is the outcome of the second request, which is just a GET without params.

The result of the post is being written using echo $controller; and echo $action; and these are returned in (data) but you don't do anything with that. You just redirect the client to Router.php.


Need Your Help

Deleting registry entries - what happens

c# key registry

I have a program, which deletes some registry entries, which I searched before. The program searches for registry keys, which referring to a DLL, but this DLL does not any more exist on the compute...

Laravel Job Queue not processing using Redis driver

php laravel redis publish-subscribe

I'm creating a job, pushing it on to a custom queue, and trying to use the Redis driver to then handle the job when it hits the queue, without success: