PHP 注册类与前端表单连接起来


php registration class wire up with front end form?

Using oracle server 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 = 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'];
      }
}

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

如果你想

沿着模型-视图-控制器(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 
}   

注册中.php:

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

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