简体   繁体   English

在php和javascript之间共享变量

[英]sharing variables in between php and javascript

Following is my job: 以下是我的工作:

  1. Consist two iframes 包含两个iframe

  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. 单击添加后,此信息将添加到mysql数据库,并再次显示该表单,并显示一条消息,提示您的信息已插入数据库。

  4. In the 2nd iframe there is some interesting. 在第二个iframe中,有一些有趣的地方。 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). 在后台,一个php函数将定期检查以一定间隔(例如5秒)添加到数据库的新用户,如果在MySQL DB中找到了任何新行(不在HTML iframe的表中),它将被添加到HTML iframe页面中的表格(可能正在使用javascript函数)。

I have done with first 3 steps. 我已经完成了前三个步骤。 Please help me for the 4th step. 请帮助我进行第四步。 I want to use PHP and Javascript. 我想使用PHP和Javascript。

Use AJAX - acquire new rows or all rows. 使用AJAX-获取新行或所有行。 And just write it in database. 并将其写入数据库。

Example (with jQuery - without JSON-driven data transfer): 示例(使用jQuery-不使用JSON驱动的数据传输):

1, getData.php 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) 2a,JS代码(使用jQuery-从数据库下载整个表)

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) 2b,JS代码(使用jQuery-仅从数据库下载新行)

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 尝试创建隐藏的文本框并将php变量值分配给该文本框,然后您可以通过执行以下操作来访问该值

document.getElementById('id_of_textbox').value;

Or in your case for accessing HTML elements in iframe maybe you need to do: 或者,如果要访问iframe中的HTML元素,则可能需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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