addCategory.php
<?php
$parentId = isset($_GET['parentId']) ? $_GET['parentId'] : 0;
$categoryName = $categoryDescription = "";
$fail = "";
if (isset($_POST['submit'])) {
if (isset($_POST['categoryName']))
$categoryName = fix_string($_POST['categoryName']);
if (isset($_POST['categoryDescription']))
$categoryDescription = fix_string($_POST['categoryDescription']);
$hidParentId = $_POST['hidParentId'];
}
$fail = validate_category_name($categoryName);
$fail .= validate_category_description($categoryDescription);
echo "<html><head><title>An Example Form</title>";
if ($fail == "") {
echo "success";
header("Location: processCategory.php?action=add&categoryName=$categoryName&categoryDescription=$categoryDescription&hidparentId=$hidParentId");
exit;
}
// Now output the HTML and JavaScript code
?>
<!-- The HTML section -->
<style>.signup { border: 1px solid #999999;
font: normal 14px helvetica; color:#444444; }</style>
<script type="text/javascript">
function validate(form)
{
fail = validateCategoryName(form.categoryName.value)
fail += validateCategoryDescription(form.categoryDescription.value)
if (fail == "") return true
else { alert(fail); return false }
}
</script></head><body>
<table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">Add Category</th>
<?php
if (isset($_POST['submit'])) {
?>
<tr><td colspan="2">Sorry, the following errors were found<br />
in your form: <p><font color=red size=1><i><?php echo $fail ?></i></font></p>
</td></tr>
<?php
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?parentId=<?php echo $parentId; ?>"
onSubmit="return validate(this)">
<tr><td>Category Name</td><td><input type="text" maxlength="32"
name="categoryName" value="<?php echo $categoryName; ?>" /></td>
</tr><tr><td>Category Description</td><td><input type="text" maxlength="32"
name="categoryDescription" value="<?php echo $categoryDescription; ?>" /></td>
<input type="hidden" name="hidparentId" value="<?php echo $parentId; ?>" />
</tr><tr><td colspan="2" align="center">
<input type="submit" name="submit" value="ok" /></td>
</tr></form></table>
<!-- The JavaScript section -->
<script type="text/javascript">
function validateCategoryName(field) {
if (field == "") return "No name entered.\n"
return ""
}
function validateCategoryDescription(field) {
if (field == "") return "No description entered.\n"
return ""
}
</script></body></html>
<?php
// Finally, here are the PHP functions
function validate_category_name($field) {
if ($field == "") return "No name entered<br />";
return "";
}
function validate_category_description($field) {
if ($field == "") return "No description entered<br />";
return "";
}
function fix_string($string) {
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?>
processCategory.php
<?php
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'add' :
addCategory();
break;
case 'modify' :
modifyCategory();
break;
case 'delete' :
deleteCategory();
break;
default :
// if action is not defined or unknown
// move to main category page
header('Location: index.php');
}
/*
Add a category
*/
function addCategory() {
$name = $_GET['categoryName'];
$description = $_GET['categoryDescription'];
$parentId = $_GET['hidparentId'];
$sql = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description)
VALUES ($parentId, '$name', '$description')";
$result = dbQuery($sql) or die('Cannot add category' . mysql_error());
header('Location: index.php?catId=' . $parentId);
}
function modifyCategory() {
}
function deleteCategory() {
}
?>
请注意,我通过 POST 获取用户输入,然后将该 POST 数据发送到相同的 .php 文件......然后在验证后将这些数据发送到另一个 .php 文件 THRU GET ......将这些 GET 数据插入DB 然后在插入这些数据后,我们重定向到另一个页面。
我读到如果你改变数据库的状态,你应该使用 POST 而不是 GET。我认为这很糟糕,因为可以在 URL 中看到 GET 数据,因此用户可以在 URL 中更改数据库的状态,另一个是当您单击刷新时,如果您使用 POST,那么浏览器会警告您尝试重复再次使用相同的方法,但是如果您使用 GET 浏览器将不会警告您,因此最终会两次发布相同的数据。
在我的代码中,用户无法操纵 url 来更改数据库,因为一旦插入数据,我们就会重定向到不同的页面,而且刷新问题在这里也不是问题。
我想要一个分开的地方来处理我的输入,那就是 processCategory.php。我是菜鸟,如果我做得对,请告诉我。