2

使用 oracle 服务器 11g。

我的前端只有4个输入框。我也有一些 javascript 验证,但它工作正常,所以我不会发布它,主要是我的类如何与表单元素交互存在问题。

我假设我仍然需要对服务器进行所有 php 验证。我对所有这些如何与我的表单元素交互有点困惑。

这是我的html表单:

<form id='register' action='register.php' onsubmit="return validateForm()" method='post' accept-charset='UTF-8'>
<fieldset>
<legend><br/>Create An Account</legend><br/>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Username*: </label>
<input type='text' name='username' id='username' maxlength="50" /><br/><br/>
<label for='email' >Email Address*:</label>
<input type='text' name='email' id='email' maxlength="50" /><br/><br/>
<label for="password">Password*:</label>  
<input type="password" name="password" placeholder="password" required><br/><br/>
<label for="password">Confirm Password*:</label>  
<input type="password" name="password" placeholder="password" required><br/><br/>
<label for='cpassword' >&zwnj;</label>
<input type="hidden" name="fsubmitted" value="TRUE"><input type='submit' name='Submit' value='Register' />
</fieldset>
</form>

这是我的课程和一些方法:

class Shopper extends Base {

protected $shopper_id;
protected $email;
protected $user_name;
protected $temp_token;
protected $sign_in_token;

protected $UserShoppingList;

function __construct($email = null) {

    if (strpos($email, '@') === false) {
        $this->sign_in_token = $email;
    } else {
        $this->email = $email;
    }
}


public function activate($temp_token) {
    global $db;

    $this->set_temp_token($temp_token);

    $vars = array();
    $vars[] = array(':i_temp_token', $this->get_temp_token());


    return $db->get_function_as_proc('custom.japi_shopper_identity.Activate_User(:i_temp_token)', $vars) == 'Y';
}

public function create($password) {
        global $db;

        if (!$this->get_email() || !$this->get_username()) {
            return false;
        }

        $vars = array();
        $vars[] = array(':email', $this->get_email());
        $vars[] = array(':username', $this->get_username());
        $vars[] = array(':password', $password);

        $id = $db->get_function_as_proc('custom.japi_shopper_identity.create_user(:email, :username,  :password)', $vars);
        $this->set_id($id);

        // If it failed, it'll puke on the procedure. If we've come this far, we
        // know it worked.
        return true;
    }

public function request_activation() {
        global $db;

        $vars = array();
        $vars[] = array(':i_shopper_id', $this->get_id());

        // Returns a temp token
        $temp_token = $db->get_function_as_proc('custom.japi_shopper_identity.activate_user_request(:i_shopper_id)', $vars);

        if ($temp_token == null) {
            return false;
        } else {
            $this->send_activation_email();
            return $temp_token;
        }
    }

public function set_email($email) {
        return $this->email = $email;
    }

 public function set_username($username) {
        return $this->user_name = $username;
    }  

当我点击注册按钮时,我应该在 action="register.php" 中有什么代码?

我应该能够将所有代码保留在一页上吗?

并且只是实例化 Shopper 类?

$shopper = new Shopper();
$shopper->set_email($new_username.'@example.com');
$shopper->set_username($new_username);
$shopper->create('password');
$token = $shopper->request_activation();

并且希望该request_activation功能会向他们发送电子邮件,让他们点击激活链接?任何帮助将不胜感激。提前致谢。

此外,我应该注意我的 php 验证方面的空刺。

我假设这应该没问题?

if (isset($_POST['formsubmitted'])) {
      $error = array(); //Declare An Array to store any error message
      if (empty($_POST['name'])) { //if no name has been supplied
          $error[] = 'Please Enter a name '; //add to array "error"
      } else {
          $name = $_POST['name']; //else assign it a variable
      }

      if (empty($_POST['e-mail'])) {
          $error[] = 'Please Enter your Email ';
      } else {

          if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",
              $_POST['e-mail'])) {
              //regular expression for email validation
              $Email = $_POST['e-mail'];
          } else {
              $error[] = 'Your EMail Address is invalid  ';
          }

    }
if (empty($_POST['Password'])) {
          $error[] = 'Please Enter Your Password ';
      } else {
          $Password = $_POST['Password'];
      }
}

还有什么我应该担心的吗?

4

1 回答 1

0

如果你想走模型-视图-控制器(MVC)路径,我建议创建一个控制器类来处理购物者。

这允许您封装用于在应用程序中处理资源的逻辑。并且比一堆嵌套的 if 更容易处理。

/**
* This class handles creating, showing and destroying shoppers
*/ 
class ShopperController {

    function newAction($shopper = null) {
        // render the form
        include "/views/shopper/signup.php"
    }

    function createAction($params) {
        $shopper = new Shopper($params);

        //@todo create validate method
        if ($shopper->validate()) {
            //@todo persist to database
        }
        else {
           // validation failed re-render form with values submitted 
           return $this->newAction($shopper); 
        }

        //Render some sort of response
        include "/views/shopper/show.php"
    }
    // ... more methods 
}   

在 register.php 中:

$controller = new ShopperController();

switch($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        $controller->newAction();
    case 'POST':
        $controller->createAction($_POST);
}

但是,如果您想完成工作,我并不建议您从头开始编写 MVC 框架。

于 2013-11-25T22:43:20.393 回答