dongluni0568 2014-01-30 04:14
浏览 33
已采纳

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> ';

}
  • 写回答

1条回答 默认 最新

  • dpyic24480 2014-01-30 04:26
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用