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 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?