Environment is OSX 10.4 with registered_globals off
I have a single index.php within my admin folder to handle all my administrative processes in my script.
I use functions throughout.
My problem is passing the value of the variable $op between the functions.
For example this works and displays all my questions that are waiting
//List the Questions in the Queue
function listQuestions($op) {
global $db;
echo "<form method=\"post\" action=\"" .$_SERVER['PHP_SELF'] ."\">\n";
echo "<table border='0' align='center' width='100%' cellpadding='2' cellspacing='0'>\n";
echo "<tr>";
echo "<div class=\"title\">";
echo "<td align='left' valign='top' width='50%'> <b><u>Title</u></b> </td>\n";
echo "<td align='center' valign='top' width='25%'> <b><u>Time Posted</u></b> </td>\n";
echo "<td align='right' valign='top' width='25%'> <b><u>Preview</u></b> </td>\n";
echo "</div>";
echo "</tr>\n";
echo "</font>";
$result = $db->sql_query("SELECT * FROM comment_queue");
while ($row = $db->sql_fetchrow($result))
{
$cid = $row['cid'];
$title = $row['title'];
$comment_text = $row['comment_text'];
$timestamp = $row['timestamp'];
$formatdate = date("M j, Y g: i A",strtotime("$timestamp"));
OpenTable();
echo "<table width=\"100%\" border=\"0\">";
echo "<tr>";
echo "<td align=\"left\" width=\"50%\"><div class=\"content\">$title</div></td>";
echo "<td align=\"center\" width=\"25%\"><div class=\"content\">$formatdate</div></td>";
echo "<td align=\"right\" width=\"25%\"><div class=\"content\"><a href=\"index.php?op=previewQuestion&cid=$cid\">Preview</a></div></td>";
echo "</tr>";
echo "<input name=\"cid\" type=\"hidden\" value=\"$cid\" />";
echo "</form>";
echo "</table>";
CloseTable();
}
}
I then go to Preview Question
//Preview the Question
function previewQuestion($op, $cid) {
global $AllowableHTML, $db;
$cid = $_GET['cid'];
$title = $_POST['title'];
$comment_text = $_POST['comment_text'];
$result = $db->sql_query("SELECT * FROM comment_queue WHERE cid = '$cid'");
while ($row = $db->sql_fetchrow($result)) {
$title = stripslashes(check_html($row['title'], 'nohtml'));
$comment_text = stripslashes(check_html($row['comment_text'], 'nohtml'));
}
$pre_title = $title;
$pre_comment_text = $comment_text;
OpenTable();
echo "<form method=\"post\" action=\"" .$_SERVER['PHP_SELF'] ."\">\n";
echo "<b>Question Title:</b><br />"
."<input type=\"text\" name=\"title\" size=\"50\" maxlength=\"80\" value=\"$pre_title\" /><br />";
echo "<b>Text of Question:</b> HTML is fine. <br />"
."<textarea cols=\"50\" rows=\"15\" style=\"background:#EFEFEF\" name=\"pre_comment_text\">$pre_comment_text</textarea><br />";
echo "<p>If you included any URLs or html, be sure to double check them for typos.</p>"
."<p>Allowed HTML:<br />"
."</p>"
."<p>";
while (list($key,) = each($AllowableHTML)) echo ' <'.$key.'>';
echo "</p>";
echo"<select name=\"op\">"
."<option value=\"deleteQuestion\">Delete Question</option>"
."<option value=\"preview\" selected=\"selected\">Preview</option>"
."<option value=\"postReply\">Post Reply</option>"
."</select>"
."<input type=\"submit\" value=\"OK\" />";
echo "<input name=\"cid\" type=\"hidden\" value=\"$cid\" />";
echo "</form>";
CloseTable();
}
Which displays the information in a textarea for edits.
If I choose Delete Question in the dropdown which assigns deleteQuestion to the variable $op the page does not go to the deleteQuestion function but instead goes to default in my switch statement.
//Delete the Question
function deleteQuestion($op, $cid) {
global $db;
$cid = $_GET[intval($cid)];
$result = $db->sql_query("DELETE FROM comment_queue where cid = '$cid'");
if (!$result) {
return;
die();
}
Header('Location: index.php?op=listQuestions');
}
switch ($_GET['op']) {
case "listQuestions":
listQuestions($op);
break;
case "info":
info();
break;
case "listCourses":
listCourses($op);
break;
case "previewQuestion":
previewQuestion($op, $cid);
break;
case "saveQuestion":
saveQuestion($cid, $pre_title, $pre_comment_text);
break;
case "deleteQuestion":
deleteQuestion($op, $cid);
break;
default:
admin_menu($op);
break;
}
}
?>
Sould I pass the variable $op in my functions as in
function previewQuestion($op, $cid) {
and in my switch statement as in
admin_menu($op);
Do I need to $_GET the value of $op within each of my functions?
$op = $_GET['op'];
If $op was within <form> tags in my sending function then in the receiving function I should use $op = $_POST['op']; correct?
I learned most of my PHP while coding in PHPNuke and now with this standalone app with globals are off, I am finding I have been learning bad practices.