4


I have a table (I'm using DataTables plug-in for jQuery) and when I click on a row I want to be redirected to another page where will be displayed customer details.

Here is my code in customers.php file:

<script>
$(document).ready(function () {
    $('#customer').DataTable({
        serverSide: true,
        ajax: "customer-data.php",
        scrollY: 350,
        scrollX: true,
        deferRender: true,
        select:true,
        scroller: {
            loadingIndicator: true,
            displayBuffer: 4,
            serverWait: 100
        }
    });
    var table = $('#customer').DataTable();
    $('body').on('click', '#customer tbody tr', function () {
        var name = table.cell('.selected', 1).data();
        $.ajax({
            type: "POST",
            url: "customer_details.php",
            data: {name1: name},
            success: function() {
                window.location = "customer_details.php";
            }
        });
    });
});
</script>

This is in customer_details.php file

<div>Company name</div>
<div>
    <?php
    $name = $_POST['name1'];
    echo "$name";
    ?>
</div>

When I click on a row I am redirected to customer details page but I can't see the data (i.e. name). When I look at response tab in console in Firebug I can see this data in div.
Could anyone help me what am I doing wrong?

SMW
  • 125
  • 1
  • 12
  • 3
    Well, you post via ajax, then redirect them to the same page (for some reason) which will be a simple GET. It won't have any POST data. – Jonnix Nov 18 '15 at 09:35
  • console `var name = table.cell('.selected', 1).data();` and check whether name is giving any data or not – Apb Nov 18 '15 at 09:38
  • `echo "$name"; `will echo `"$name" `as a string, take out the `"` – ahervin Nov 18 '15 at 09:42
  • 1
    @ahervin No it won't -_- – Jonnix Nov 18 '15 at 09:42
  • @ahervin `echo '$name';` would but not `echo "$name";`. Ya, PHP is quite boring sometimes... – A. Wolff Nov 18 '15 at 09:43
  • I suppose he would of seen a div containing "$name".....what even is a PHP – ahervin Nov 18 '15 at 09:45
  • 1
    I think you are on the wrong track my friend. AJAX is used for getting data without redirecting if you want to go to the customer_details.php then just create a form at runtime and post the vale to the page OR if you want not to redirect then remove the line window.location = "customer_details.php"; and set the response to html – Aman Rawat Nov 18 '15 at 09:46
  • @AmanRawat Could you explain me in greater detail what did you mean to create a form at runtime and post the value to the page? – SMW Nov 18 '15 at 12:46

1 Answers1

1

AJAX is used to get data into an already loaded page without having to re-load.

What the current code does is, first, it sends ajax POST request to the customer_details.php page. What this ajax request does is get the results from the POST request using a XMLHttpRequest object.
(You can read up on it here: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp)

Then, you redirect the browser to that page.

What you want to do instead is send a POST request using a form.
(This question has a good explanation how you would accomplish it: JavaScript post request like a form submit)

Community
  • 1
  • 1
nipuna-g
  • 6,252
  • 3
  • 31
  • 48
  • Where should I use form? In table? Could you explain it in greater detail? I have no idea what you mean. – SMW Nov 18 '15 at 12:42
  • The form itself is hidden. It's only used to submit the POST request. You should be able to get a good understanding form the second link. I'll make this answer more detailed when I get home in about two hours. – nipuna-g Nov 18 '15 at 13:09