PHP - Javascript - 在循环中的页面之间携带动态变量

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. Currently when I press any of the buttons, JavaScript will hide the button, replace it with "Button Pressed" and then open a window.

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. I tried using forms within the echo but that did not return any values. I also tried the $_SESSION function with no luck. 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> ';

}
duandui2803
duandui2803 -那不行,因为我不能在父PHP中放一个<?php?>
6 年多之前 回复
dtmjqyfz21793
dtmjqyfz21793 尝试sessionStorage.setItem(“startdate”,<?phpecho$StartDate?>);<==在会话中存储值..sessionStorage.getItem(“startdate”)<==从会话中检索值
6 年多之前 回复
dongqiong8021
dongqiong8021 我试过,但由于变量是PHO变量,我需要通常像test2.PHP?StartDate=<?php$StartDate?>-但问题在于它是在echo中。所以,当我关闭标签时?>它导致代码变得怪异
6 年多之前 回复
dongqu9972
dongqu9972 我不太了解javascript,但你可以将你的变量传递给test2.htmluing$_GET吗?...像test2.html?StartDate=xxxx&StartTime=xxx
6 年多之前 回复

1个回答

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, e.g. as a GET parameter, and fetching the row in the new window.

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 (e.g. 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:

$id = $_GET['id']; // make sure to sanitize before inserting into a query
// SELECT ... FROM ... WHERE id = $id;
dongxing7530
dongxing7530 啊! 非常感谢您查看代码并帮助我解决问题!
6 年多之前 回复
duanqinjiao5244
duanqinjiao5244 抱歉,我最初没有在那里,因为我忘记了你仍然需要使用其余代码的按钮的引用。 很高兴现在正在工作。
6 年多之前 回复
douweibeng5219
douweibeng5219 我简直不敢相信......我太近了! 它现在有效! - 结果我错过了你放置的部分(这个'。$ id。')。 我离开了#this!
6 年多之前 回复
douchou8935
douchou8935 剥离原始代码的版本:)立即尝试。
6 年多之前 回复
dongxia19772008
dongxia19772008 那是你的原始代码吗? 它有用吗? 如果有的话尝试这个包含我的建议的修改版本:pastebin.com/Q9tRxLQf
6 年多之前 回复
douzhan1868
douzhan1868 pastebin.com/EvtD0dH3
6 年多之前 回复
dttnb997315
dttnb997315 你能发布更新的代码吗? 例如 通过Pastebin。 我不确定按钮在你的原始代码中是如何工作的,因为它调用了一个名为SendEmail()的函数,这个函数不在你的代码中(我假设是buttonpressed()应该是它)。
6 年多之前 回复
doumo1807831
doumo1807831 由于某种原因,这一切都打破了按钮。
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问