How to pass variables in PHP Ajax Handler Script?

I have a small ajax php application that outputs data from mysql db to a table. Strings are links that, when clicked, will call the ajax function, which in turn will call another php file that displays another request from the same database at the level without reloading the page.

I would like to know how to sync requests between php files. So when I click on a row on the base page, the layer will expand to include additional information or even the entire query.

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

+1


source to share


6 answers


with jQuery it is very simple and I definitely recommend using it in ajax calls 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 a warning, you should change what you need to do. To start with, I would recommend using a preloaded div and changing its content when using it:



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

      

and for JS function you can use it like:

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

      

The examples are numerous, and you should find what works for you.

+2


source


You can do the following:



  • Each row in the table must have a hidden text field that will store the projective key.
  • when you click on the string it will call the javascript function and pass the id through that as text. 3. When user clikc the string he will call Callfunction in javascript and he will call ajax and pass paramanter using GET ot POST method
+1


source


You don't want it to show up, that means there is security issues or something.

If you want to lose the primary key on the table, you can go with a query cache pushed into the session object and then just backtrack in place in the array.

so something like: page 1:

  • create an array with db objects
  • store array in session
  • display objects in a table
  • add a display layer function for each in the table using the index from the array as a parameter.

page 2:

restore session object show data for array point
0


source


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

  • USE FRAME for your Ajax handling. This will make your life easier and they will take care of a lot of things that you don't have to worry about at all, like how to handle the XMLHttpRequest object in browsers, etc.
  • When you load the first table, create a second tr for each tr that displays but makes it hidden. You will populate this second table row with information from the ajax request.
  • Modify the ajax function to transfer the primary key as a parameter. Pass this parameter via GET or POST to the second php script. You can look here for further clarification on this issue.
  • Give the id of the second hidden tr as the div to update with the response from your ajax request.
0


source



Current file content:




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


source


<?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;

}
?>

      

0


source







All Articles