[英]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: 我建议这样做的原因有几个:
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.