0

I'm trying to retrieve database information based on what row a user clicks in a table, then later use that data in another page to display other database information. I don't know the best way to achieve it. Should I use Ajax and $.post() or are there other better/simpler ways?

I can retrieve the data from the table by

 echo "<tr data-href=". $row["id"] . " class=\"tableclass\"><td>"

and then in jQuery

$(".tableclass").click(function () {
    data = $(this).data("href");
    alert(data);

The alert shows that I do get the database information (column ID). Now, I would like to post that information, preferably in a secure manner, to another PHP page where I can retrieve it and use it to get other information from the database.

How do I post it and then how should I retrieve it in the next php page?

2
  • You should add ' or " signs around the data-href's value. Commented Mar 17, 2015 at 16:06
  • And to post the value, use jQuery's $.post. Commented Mar 17, 2015 at 16:06

1 Answer 1

1

Both works in same manner. 1. $.post is just a call with $.ajax(), just with the type set. 2. $.ajax() defaults to a GET

$.post( "/ajax", {"data" : json }) //is nothing but



  $.ajax({ 
  type: "POST", 
  url: "/ajax", 
  data: {"data": json} 
}); 

If you want the data to be passed securely, use json or don't do thing if the request is not properly authenticated. Typically, when you make an AJAX request, cookies are also sent: along with the request so you should just be able to use the same authentication method that you use for your regular requests with your AJAX requests.

Nothing much to do that, you need to use $_POST to access all the values in php in file save.php

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your quick response! I've never used Ajax, so I will read up on it. How do I fetch the data in the next page after I have posted it like you wrote? Say that page is save.php
Use $_POST[] to access all the values in php in file save.php
Use following method in JS file: $.ajax({ type: "POST", url: "/save.php", data: {"data": json} });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.