javascript - php 注册类连接前端表单?

标签 javascript php oracle api

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

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

最佳答案

如果你想下Model–view–controller (MVC)我建议创建一个 Controller 类来处理购物者。

这允许您封装处理应用程序中资源的逻辑。并且比一堆嵌套的 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 框架。

关于javascript - php 注册类连接前端表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19937484/

相关文章:

php - 代码点火器 : dependent dropdown not working

php - 试图获得非对象的属性

sql - Oracle:类似于 sysdate 但只返回时间和日期

javascript - 使用 AngularJS $watch 更改数组中的值

php - 如何向搜索引擎显示 HTML 页面而不是 Flash

Oracle 从两个表中选择数据,如果一个为空返回 null

sql - SQL问题-从列表中找到一个数字

javascript - 将数据从一个 $lookup 传递到另一个

javascript - 如何让变量脱离闭包?

javascript - 我应该如何获取整数 GMT 时间