Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   javascript function over table (http://www.programmingforums.org/showthread.php?t=12516)

thechristelegacy Feb 6th, 2007 1:04 AM

javascript function over table
 
Ok, so I wrote some code to make links act like the submit button to a form. I wrote a test file (test.php) which used analyze.php to work with the form data. When I put it up to the scale on the main page the javascript stopped work, so I'm thinking it's because I'm putting it into a table, below is the code. Any ideas or thoughts is much appreciated! Thanks guys!

test.php
[php]
<html>
<script language="JavaScript" type="text/javascript">
<!--
function submit ( selectedType )
{
document.form1.value.value = selectedType ;
document.form1.submit() ;
}
-->
</script>
<form name="form1" method="post" action="analyze.php">
<input type="hidden" name="value" />
<a href="javascript:submit('Delete')">Delete</a> or
<a href="javascript:submit('Edit')">Edit</a>or
<a href="javascript:submit('Share')">Share</a>
</form>
</html>
[/php]

analyze.php
[php]
<html>
<body>
<?php
$file = $_POST['value'];

echo $file;

if ($file == 'Delete')
{
echo "Delete has been chosen.";
}
elseif ($file =='Edit')
{
echo "Edit function has been chosen.";
}
elseif ($file =='Share')
{
echo "Share function has been chosen.";
}
else
{
echo "Nothing has been chosen.";
}

?>
</body>
</html>
[/php]

And when I use test.php everything works, now below is the code that uses it in a table and doesn't work. I

main.php (The entire file is not included. If you think the problem could be in the rest of the file, let me know, but I think it's a waste of space and tiem for you guys to look at)
[php]
html>

<body>
<script language="JavaScript" type="text/javascript">
<!--
function submit ( selectedType )
{
document.form1.value.value = selectedType ;
document.form1.submit() ;
}
-->
</script>
<table>
<tr><td>


<?
if($session->logged_in){
echo "<h1>Logged In</h1>";
echo "Welcome <b>$session->username</b>!<br><br>"
."[<a href=\"userinfo.php?user=$session->username\">My Account</a>] &nbsp;&nbsp;"
."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;"
."[<a href=\"saver.php\">New Document</a>] &nbsp;&nbsp;";
if($session->isAdmin()){
echo "[<a href=\"admin/admin.php\">Admin Center</a>] &nbsp;&nbsp;";
}
echo "[<a href=\"process.php\">Logout</a>]<br><br><br>- [$session->username's Files] -<br>";


// -------------------------------------------------------------------------------------------------- Directory Code
// This is the directory to list files for.
$theDirectory = $session->username;
// Do you want to show directories? change to false to hide directories.
$listDirectories = false;
echo "</ br></ br>";
if(is_dir($theDirectory))
{
// Title for top of columns
echo "<table><tr>
<td><u>Name</u></td>
<td><u>Size</u></td>
<td><u>Action</u></td>
<td><u>Action</u></td>
<td><u>Action</u></td>
<td><u>Action</u></td>
</tr>";
$dir = opendir($theDirectory);
while(false !== ($file = readdir($dir)))
{
$type = filetype($theDirectory ."/". $file);
if($listDirectories || $type != "dir")
{
echo "<tr><td>" . $file . "</td>";
// echo "<td>" . $type . "</td>";

echo "<td>";
if($type == "file")
$fileSize = filesize($theDirectory."/".$file)/1024; // Divide 1024 to get into kilobytes
echo number_format($fileSize, 2); // Limit the decimals to two places
echo " Kilobytes.";
echo "</td>";

echo "<td><a href='".$theDirectory."/".$file."'>View</a></td>"; // Works

////////////////////////////////////////////////////////////////////////////////


echo "<form name\"form1\" method=\"post\" action=\"analyze.php\">";
echo "<input type=\"hidden\" name=\"value>";
echo "<td><a href=\"javascript:submit('Edit')\">Edit</a></td>"; // Doesn't Work
echo "<td><a href=\"javascript:submit('Delete')\">Delete</a></td>"; // Doesn't Work
echo "<td><a href=\"javascript:submit('Share')\">Share</a></td>"; // Doesn't Work
echo "</form>";


echo "</tr>";
////////////////////////////////////////////////////////////////////////////
}
}
closedir($dir);
echo "</table>---";
}
else
{
echo $theDirectory . " is not a directory";
}
//----------------------------------------------------------------------------------------------------------------------------------------------End Directory Code
[/php]

I enclosed what I think to be the problematic area in ///////////////////////////

grimpirate Feb 6th, 2007 4:30 AM

I'm not so sure I can identify what the problem is but the $session->logged_in doesn't make much sense if you haven't declared $session somewhere to be a new type of object. Unless you're trying to refer to the session superglobal array somehow, but for PHP 5 that would be of the sort $_SESSION, and you'd have to test that it's set or something. Not exactly sure what you're going for.

Furthermore I think javascript is unnecessary in what you're trying to achieve. If you're already using javascript might as well just do everything with it. In other words the use of the form isn't exactly necessary. To stick with PHP I would suggest using 3 radio buttons something of the form:
:

<input type="radio" name="type" value="delete"> Delete
<input type="radio" name="type" value="edit"> Edit
<input type="radio" name="type" value="share"> Share

Then it just becomes a matter of checking $_POST['type'] for the given operation you wish performed. Could even do it with numbers rather than strings:
:

<input type="radio" name="type" value="0"> Delete
<input type="radio" name="type" value="1"> Edit
<input type="radio" name="type" value="2"> Share

This would allow you to perform a switch statement for each particular case:
:

switch((int)$_POST['type']){
case 0: ...  // Delete code
case 1: ...  // Edit code
case 2: ...  // Share code
default: ... // In case of an error
}

I don't know that these suggestions actually help the problem you're currently experiencing which seems to be more of a session problem than it does a javascript or php error.

thechristelegacy Feb 6th, 2007 11:08 AM

Grimppirate, it's my fault for not including the entire script, I was trying to cut back on how much code you guys had to search through. At the beginning of the code is
[php]
<?
include("include/session.php");
?>
[/php]


All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC