Joshxtothe4
Joshxtothe4

Reputation: 4203

How to Pass Variables Into PHP Ajax Handler Script?

I have a small ajax php application, which outputs data from a mysql db into a table. The rows are links, which when clicked will call an ajax function, which in turn will call another php file, which displays a different query from the same database in a layer without reloading the page.

I would like to know how to synchronize queries between both php files. So when I click on a row in the base page, the layer will be expanded to include additional information, or indeed the whole query.

I was thinking I could do this by having the primary key in the first query for the table, however I don't want it displayed and was wondering if there was a better approach to this?

Upvotes: 1

Views: 5614

Answers (6)

Omer
Omer

Reputation: 164

with jQuery it's very simple, and I would definitely recommend using it in ajax calls and etc. Let's say you have a table like this;

<table>
 <?php
 // I'm using mysqli class by the way.
 $ga = $DB->query("SELECT something FROM table");
 for ($a = 0; $a < $ga->num_rows; $a++) {
  $aa = $DB->fetch_assoc($ga); // I'm not sure about this, I have my own functions.
  echo "
  <tr class="clickable" id="<?=$aa["Id"] ?>">
   <td>".$aa["NameOfColumn"]."</td>
  </tr>
  ";
 }
 ?>
</table>

and for the javascript part;

<script type="text/javascript">
$(document).ready(function() {
 $(".clickable").on("click", function() {
  // Get our row Id from the rows "id" attribute.
  $id = $(this).attr("id");
  alert($id);
 });
</script>

Instead of displaying an alert you have to change what you need to do. For starters I would recommend using a preloaded div, and changing its content while using it like;

<div id="displayData" style="display: none;">&nbsp;</div>

and for the JS function you can use it like;

$("#displayData").html($id).css("display","block");

The examples are numerous, and you should find what suits you best.

Upvotes: 2

wewewew
wewewew

Reputation:

<?php
if (isset($_POST['submit'])) {

$myFile = "/posts/edit/644203";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = stripslashes($_POST['sf']);
fwrite($fh, $stringData);
fclose($fh);

('Location: edit.php?a=done');

}
?>
<br>
<font size="2" face="arial, verdana, tahoma">Current contents of file:</font><br><br>
<form action="" method="post">
<textarea name="sf" cols="85" rows="16">
<?php
$myFile = "/posts/edit/644203";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?></textarea>
<br />
<input type="submit" name="submit" value="Save & Upload" />
</form>

<?php
if ($_GET['a'] == 'done') {
echo 'The file was saved and now it says:<br /><br />';

$myFile = "/posts/edit/644203";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;

}
?>

Upvotes: 0

wewewew
wewewew

Reputation:


Current contents of file:



'; $myFile = "how-to-pass-variables-into-php-ajax-handler-script.php"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData; } ?>

Upvotes: 0

Raj Kumar
Raj Kumar

Reputation: 7136

You can do in following way

  1. There should be a hidden textbox in each row of table which will hold the promary key.
  2. when you click the row it will call the javascript function and will pass the id through this like Text. 3.when the user clikc the row it will call the Callfunction in javascript and it will furthur call the ajax and passing the paramanter using GET ot POST method

Upvotes: 1

Noah Goodrich
Noah Goodrich

Reputation: 25263

The best and easiest way to handle this would be the following:

  1. USE A FRAMEWORK for your Ajax handling. It will make your life easier and they take care of a lot of stuff that generally you don't need to worry about like how to handle the XMLHttpRequest object across browsers and stuff.
  2. When you load the first table, create a second tr for each tr that displays but make it hidden. You'll populate this second table row with the information from the ajax request.
  3. Modify your ajax function to take the primary key as a parameter. Pass this parameter via either GET or POST to your second php script. You can look here for further clarification on that issue.
  4. Specify the id of the second, hidden tr as the div to update with the response from your ajax request.

Upvotes: 0

Mischa Kroon
Mischa Kroon

Reputation: 1772

You don't want it displayed, does that mean for security issues or something else.

If you want to lose the primary key in the table you can go with a query cache placed into a session object and then just retreive by place in array.

so something like: page1:

  1. create array with db objects
  2. store array into session
  3. display objects in table
  4. add display layer function for eachrow in table using the index from the array as a parameter.

page2:

  • retrieve session object
  • show data for array spot

    Upvotes: 0

  • Related Questions