Jump to content

Trouble passing variables to popup from php while loop


scdawg

Recommended Posts

I have a while loop that displays all files associated with a project that display in a table. A project can have one file or 100 associated. I want to use a button to open those files in a popup. My problem is that all the buttons, open the same file instead of their respective files, which is the first file displayed(the variables passed to the popup never change). If I do use a simple link (href) in the php code, I can open the correct file, but it doesn't display in a popup, it opens in the same window which is a problem. Does anyone have any suggestions on how to use a link or button to open the correct files in a popup? Below is my code:

 

While(list($date, $filename, $title) =  mysql_fetch_array($resultd)) {

echo
"<tr>\n".
"<td align=center>$date</td>\n".
"<td align=center>$title</td>\n".
"<td align=center><a href='docs/$coid/$filename'>$filename</a></td>\n".
"<td align=center><input type='hidden' value='$filename' name='file_name'></input></td>\n";
?>

<td align="center"><input type="button" value="View Document" onclick="return viewdoc(); return false">
<?
echo

"<td align=center><input type='button' value='Delete Document' name='file_delete'></input></td>\n".
"</tr>\n";
}

echo
"</table>\n".
"<br><br><INPUT TYPE='submit' NAME='submit' VALUE='Submit'></p>\n".
"<input type='hidden' name='comp_id' value='$coid'>\n".
"</FORM>\n".
"</BODY>\n".
"</HTML>\n";

?>

<script type="text/javascript" language="javascript">
<!-- Document open

function viewdoc() {
var filename = document.getElementById("file_name").value;
var compid = document.getElementById("comp_id").value;

newwindow = window.open('/'+'docs/'+compid+ '/' +filename,'DocViewer','width=800,height=600,top=150,left=350,scrollbars=1,status=1');
if (window.focus) {newwindow.focus()}
return false;
}

 

attached is a photo of what the list looks like

post-129102-13482403788805_thumb.jpg

Link to comment
Share on other sites

First of all, I've cleaned up your code a bit for you. To make it easier to read, as your mixed syntax usage made things unnecessarily complicated.

Do not that you still need to escape output with htmlspecialchars () in the loop, to prevent XSS attacks and other (unintentional or not) HTML injection attacks.

<?php
while (list ($date, $filename, $title) = mysql_fetch_array ($resultd)) {
echo <<<OutHTML
	<tr>
		<td align=center>$date</td>
		<td align=center>$title</td>
		<td align=center><a href='docs/$coid/$filename'>$filename</a></td>
		<td align=center><input type='hidden' value='$filename' name='file_name'></input></td>
		<td align="center"><input type="button" value="View Document" onclick="return viewdoc(); return false">
		<td align=center><input type='button' value='Delete Document' name='file_delete'></input></td>
	</tr>
OutHTML;
}

?>
</table>

<input type="hidden" name="comp_id" value="<?php echo intval ($coid); ?>">
<input type="submit" name="submit" value="Submit">
</form>

<script type="text/javascript" language="javascript">
<!-- Document open

function viewdoc() {
var filename = document.getElementById("file_name").value;
var compid = document.getElementById("comp_id").value;
var windowURL = '/'+'docs/'+compid+ '/' +filename;

var windowAttr = 'width=800,height=600,top=150,left=350,scrollbars=1,status=1';

newwindow = window.open(windowURL,'DocViewer',windowAttr);
if (window.focus) {
	newwindow.focus()
}
return false;
}
// -->
</script>

</body>
</html>

 

You had a series of problems with your HTML code as well, such as ending the HTML document before adding the script, invalid nesting of tags, and some minor potential issues such as uppercasing some elements, putting the submit button before some of the other input elements, and improper indenting.

 

The main problems, however, are the JS and the way you call it. First and foremost you don't have any elements with the IDs file_name or comp_id, and even if you did IDs are unique so this script would pull up the same file every time.

Secondly, when you call the script you're not sending any identifying information to it, which means the script has no way to tell what file to show. You need to rethink your logic on this one, as you've planned it a bit too poorly. Instead of looking up the (same)  value inside the function itself, use PHP to add it to the function call instead.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.