简体   繁体   English

PHP-Javascript-在循环的页面之间传递动态变量

[英]PHP - Javascript - Carrying dynamic variables between pages that are in a loop

I have a loop that that retrieves rows from the database and spits it out in a table. 我有一个循环,该循环从数据库中检索行并将其吐到表中。

For every row I have a button that is generated. 对于每一行,我都会生成一个按钮。 So if I have 5 rows, 5 buttons appear. 因此,如果我有5行,则会出现5个按钮。 Currently when I press any of the buttons, JavaScript will hide the button, replace it with "Button Pressed" and then open a window. 当前,当我按下任何按钮时,JavaScript会隐藏该按钮,将其替换为“ Button Pressed”,然后打开一个窗口。

I would like where when the user presses the button that the #StartDate variable and the $StartTime variable both be carried over to the new window where the user can do other functions with that specific row. 我想在用户按下按钮时将#StartDate变量和$StartTime变量都带到新窗口,用户可以在该窗口中执行该特定行的其他功能。 I tried using forms within the echo but that did not return any values. 我尝试在回显中使用表单,但未返回任何值。 I also tried the $_SESSION function with no luck. 我也尝试了$_SESSION函数,但是没有运气。 How would one be able to carry each row over to a window when that row's button is clicked on? 单击该行的按钮后,如何将每一行移到窗口上?

function buttonpressed(button) {
   button.style.visibility = "hidden";
   document.getElementById("ChangeButton1").innerHTML="Button Pressed";
   window.open ('test2.html','newWin', 'width=400,height=400');
}


while($row = odbc_fetch_array($rs)) 
{
$StartDate=odbc_result($rs,"StartDate");
$StartTime=odbc_result($rs,"StartTime");
echo '
<tr>
<td class="td" valign="top"><p id="ChangeButton"><input type="submit" name="buttonpressed" value="buttonpressed" onclick="SendEmail(this); return false;"></p></td>';
 echo '
 <td class="td" valign="top">' . $StartDate . '</td>
 <td class="td" valign="top">' . $StartTime . '</td>
 </tr> ';

}

Instead of trying to carry over the row's data to a new window, you may consider simply passing the row's primary key to the new window, eg as a GET parameter, and fetching the row in the new window. 您可以考虑将行的主键传递给新窗口,而不是尝试将行的数据传递到新窗口,例如作为GET参数,然后在新窗口中获取该行。

There are a few reasons I'd recommend this: 我建议这样做的原因有几个:

  1. This will make it easier to change things in the future if the information you want to use from the row in the new window changes. 如果您要在新窗口的行中使用的信息发生更改,这将使将来更容易更改内容。 You only have to modify the code in the new window and not the code that opens the window. 您只需要修改新窗口中的代码,而无需打开窗口的代码。
  2. If the new window performs some action with the data (eg sending an email, as the function name suggests), this will ensure that users only perform this action using data that is from a row in the database. 如果新窗口对数据执行了某些操作(例如,按照功能名称的建议发送电子邮件),这将确保用户仅使用来自数据库中一行的数据来执行此操作。 If you pass along the data explicitly instead of the primary key, the users could modify your code to pass along whatever parameters they want instead. 如果您显式传递数据而不是主键,则用户可以修改您的代码以传递所需的任何参数。
  3. If the new window performs some database operation on the row, then it would be nice to have the primary key anyway. 如果新窗口在该行上执行了一些数据库操作,那么无论如何都要有主键会很好。

For example: 例如:

// JavaScript
function SendEmail(button, id) {
    // ...
    window.open('test2.php?id=' + id, 'newWin', 'width=400,height=400');
}

// PHP
while($row = odbc_fetch_array($rs))
{
    // ...
    $id = odbc_result($rs, "id"); // or whatever your primary key is
    echo '... <input type="submit" ... onclick="SendEmail(this, ' . $id . '); return false;"> ...';
    // ...
}

Then in test2.php: 然后在test2.php中:

$id = $_GET['id']; // make sure to sanitize before inserting into a query
// SELECT ... FROM ... WHERE id = $id;

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

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