php注册类用前端表单连接?

时间:2013-11-12 18:50:13

标签: javascript php oracle api

使用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课程?

$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'];
      }
}

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

1 个答案:

答案 0 :(得分:0)

如果您想沿着Model–view–controller (MVC)路径走,我建议您创建一个控制器类来处理购物者。

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

/**
* 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框架。