0

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

}
4
  • 1
    i dont know much of javascript, but can you pass your variables to test2.html uing $_GET? ... like test2.html?StartDate=xxxx&StartTime=xxx Commented Jan 30, 2014 at 4:18
  • @kraysak I tried that but since the variables are PHO variables I would need to go typically like test2.PHP? StartDate=<?php $ Start Date ?> - but the problem with that is that this is in echo. So when I'm closing the tag ?> its causing the code to freak Commented Jan 30, 2014 at 4:26
  • try sessionStorage.setItem("startdate", <?php echo $StartDate?>); <== store value in session.. sessionStorage.getItem("startdate") <== retrieve value from session Commented Jan 30, 2014 at 4:36
  • @rynhe - That woudlnt work since I can't put a <?php ?> within a parent PHP Commented Jan 30, 2014 at 4:40

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

7 Comments

That broke the button all together for some reason.
Can you post your updated code? e.g. via Pastebin. I'm not sure how the button worked in your original code since it was calling a function named SendEmail() that was not in your code (I assumed buttonpressed() was supposed to be it).
Is that your original code? Does it work? If so try this modified version which incorporates my suggestions: pastebin.com/Q9tRxLQf
Stripped down version of the original code :) Trying that right now.
I can't believe it...I was SOOO close! It works now! - Turns out I missed the part where you put (this, ' . $id . '). I left #this out!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.