【发布时间】:2013-12-16 02:25:58
【问题描述】:
我正在考虑建立我的第一个真正的班级,我玩过一些零碎的东西,但现在是时候真正尝试一下了:)
我想要做的是有一个表单类来处理我所有的表单提交,检查输入的数据并返回错误消息或成功消息。
所以这是我的一种形式,(我在 1 页上有 5 个)
<form action="include/mform.php" method="post" name="business_listing">
<input name="biz_name" type="text" value="Business or Place Name" />
<select name="biz_department">
<option value="">Business Sector</option>
<?php
$query = $user->database->query("SELECT * FROM tbl_sectors");
while($row=$user->database->fetchArray($query))
{
$id = $row['sectorID'];
$dept = $row['sectorName'];
echo "<option value='$id'>$dept</option>";
}?>
</select>
<input name="biz_address1" type="text" value="Street Name" />
<select name="job_location">
<option value="">Location</option>
<?php
$query = $user->database->query("SELECT * FROM tbl_places");
while($row=$user->database->fetchArray($query))
{
$id = $row['placeID'];
$dept = $row['placeName'];
echo "<option value='$id'>$dept</option>";
}?>
</select>
<input name="biz_phone" type="text" value="Contact Number" />
<input name="businessSubmit" type="submit" value="Submit" />
</form>
</div>
每个表单的操作都设置为包含我的类的 include/mform.php。在类中,它所做的第一件事就是检查提交的表单,然后检查已提交的数据并对其进行必要的处理
我的问题是,一旦我的班级知道提交了哪个表单,检查提交数据的最佳方法是什么?我应该在函数中创建变量以获取所有发布数据并从那里获取它,还是应该在实际函数中将它们作为参数传递? ,还是有关系?
这是我当前的类文件,它有点裸露的atm
class Mform
{
private $values = array(); //Holds submitted form field values
private $errors = array(); //Holds submitted form error messages
private $num_errors; //The number of errors in submitted form
public function __construct()
{
if(isset($_POST['businessSubmit']))
{
$this->chkBusiness();
}
if(isset($_POST['jobSubmit']))
{
$this->chkJob();
}
if(isset($_POST['accommodationSubmit']))
{
$this->chkAccommodation();
}
if(isset($_POST['tradeSubmit']))
{
$this->chkTrade();
}
if(isset($_POST['eventSubmit']))
{
$this->chkEvent();
}
}
public function chkBusiness()
{
$field = "business";
}
public function chkJob()
{
return "job";
}
public function chkAccommodation()
{
return "accommodation";
}
public function chkTrade()
{
return "trade";
}
public function chkEvent()
{
return "event";
}
/**
* setValue - Records the value typed into the given
* form field by the user.
*/
public function setValue($field, $value)
{
$this->values[$field] = $value;
}
/**
* setError - Records new form error given the form
* field name and the error message attached to it.
*/
public function setError($field, $errmsg)
{
$this->errors[$field] = $errmsg;
$this->num_errors = count($this->errors);
}
/**
* value - Returns the value attached to the given
* field, if none exists, the empty string is returned.
*/
public function value($field)
{
if(array_key_exists($field,$this->values))
{
return htmlspecialchars(stripslashes($this->values[$field]));
}
else
{
return "";
}
}
/**
* error - Returns the error message attached to the
* given field, if none exists, the empty string is returned.
*/
public function error($field)
{
if(array_key_exists($field,$this->errors))
{
return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>";
}
else
{
return "";
}
}
/* getErrorArray - Returns the array of error messages */
public function getErrorArray()
{
return $this->errors;
}
}
/* Initialize mform */
$mform = new Mform();
大多数单独的函数只是返回“word”作为占位符,所以我不会忘记在以后执行该函数。
这就是我想为每个单独的表单功能做的事情
public function chkBusiness()
{
$field = "business";
$name = $_POST['biz_name'];// all need to be sanitized!!
$dept = $_POST['biz_dept'];
$address = $_POST['biz_address'];
$location = $_POST['biz_location'];
$phone = $_POST['biz_phone'];
//start checking the input
if(!$name || strlen($name = trim($name)) == 0)
{
$this->mform->setError($field, "* Name not entered");
}
...
...
}
任何帮助将不胜感激
卢克
【问题讨论】:
-
我不会在该课程中直接使用
$_POST。这是测试的毒药。最简单的改进是将 post 值作为构造函数参数传递 -
感谢您的帮助 hek2mgl
-
不客气。我可以告诉你很多关于如何改进的事情,但我认为你自己尝试一些东西是件好事。 (+1)有耐心,继续玩代码,你就会进入“大师级别”.. :) 大多数“大师级别”是通过从你犯的错误中学习来获得的。在这一点上不要太在意完美。继续玩吧..这很好!