简体   繁体   中英

sharing variables in between php and javascript

Following is my job:

  1. Consist two iframes

  2. In 1st a form asks for user name and address and have a button "Add".

  3. On clicking Add this info is added to mysql database, and again the form is shown with a message that your info has been inserted to database.

  4. In the 2nd iframe there is some interesting. It will show in a table all the users in database. In background a php function will regularly checks for new user added to database at some interval(say 5 sec), and if any new row is found in MySQL DB (which is not in the table in HTML iframe), it will be added to the table in HTML iframe page (may be using javascript functions).

I have done with first 3 steps. Please help me for the 4th step. I want to use PHP and Javascript.

Use AJAX - acquire new rows or all rows. And just write it in database.

Example (with jQuery - without JSON-driven data transfer):

1, getData.php

$dbData = array(); // YOUR DB DATA HERE
$output = "";
foreach ($dbData as $row) {
   $output .= "<tr><td>{$row['name']}</td><td>{$row['email']}</td></tr>";
}
echo $output;

2a, JS code (using jQuery - downloading whole table from database)

function autoRefresh() {
   $.get('getData.php', null, function(data){
       $("#myUltimateTable tbody").html(data);
   }, 'text');
   setTimeout("autoRefresh();", 5000);
}

2b, JS code (using jQuery - downloading only new rows from database)

function autoRefresh() {
   $.get('getData.php', null, function(data){
       $("#myUltimateTable tbody").append(data);
   }, 'text');
   setTimeout("autoRefresh();", 5000);
}

Try to create hidden text box and assign php variable value to that textbox and than you can access that value by doing

document.getElementById('id_of_textbox').value;

Or in your case for accessing HTML elements in iframe maybe you need to do:

top.frame_name.document.getElementById('id_of_textbox').value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM