你如何比较AngularJS表单数据和MySQL数据库(PHP)的值?

时间:2015-05-08 08:20:57

标签: php mysql angularjs

我可以在部分视图中显示我的数据库的整个表,但我想要的是过滤它并将其中一个与我的表单的输出进行比较。这就是我显示整个表格的方式。

服务器/ fetch.php

http.get("server/fetch.php").success(function(response){
  scope.response = response;
}).error(function() {
  scope.response = "error in fetching data";
});

app.js(我的控制器在哪里)

<ul ng-repeat="codes in response">
  <li>{{codes.id}} {{codes.code}} {{codes.branch}} {{ formData.branches.alias }}</li>
</ul>

为registration.php

formData.branches.alias

codes.branch取自我的一个表单元素生成的输出。我希望将其值与来自数据库的codes.branch进行比较。我不想列出一切。我只想让我<div class="wrapper"> <div class="form-group"> <input class="form-control" ng-class="{'has-error': $submitted && signup.name.$invalid}" type="text" name="name" ng-model="formData.name" required minlength="2" placeholder="Full Name"> </div> <div class="form-group"> <input class="form-control" ng-class="{'has-error': $submitted && signup.igname.$invalid}" type="text" name="igname" ng-model="formData.igname" minlength="2" placeholder="Instagram Name (Optional)"> </div> <div class="form-group"> <input type="email" class="form-control" name="email" ng-class="{'has-error': $submitted && signup.email.$invalid}" ng-model="formData.email" ng-pattern="/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/" placeholder="E-mail Address" required> </div> <div class="form-group"> <input type="number" name="mobile" ng-class="{'has-error': $submitted && signup.mobile.$invalid}" ng-model="formData.mobile" required class="form-control" minlength="11" maxlength="11" placeholder="Mobile Number"> </div> <div class="form-group form-inline"> <span class="nullable"> <select id="regions" class="form-control" ng-model="formData.location" required ng-options="rg as rg.type for rg in region"> <option value="">Choose Location</option> </select> </span> <span class="nullable"> <select id="branches" class="form-control" ng-model="formData.branches" required ng-options="c as c[formData.location.displayName] for c in formData.location.data | orderBy:'branch'"> <option value="">Choose Branch</option> </select> </span> </div> <div class="form-group row"> <button type="submit" class="submit btn">Submit</button> </div> </div> 的一部分在其类似的行值中运行检查。

在比较两者之后,我想再次运行其列并检查它们是否可用。如果是,我会随机选择一个值插入数据库中的另一个表。

[更新]:根据要求,这是我的表格

<form id="signup-form" name="signup" ng-submit="processForm(signup.$valid)" novalidate>
  <!-- our nested state views will be injected here -->
  <div id="form-views" ui-view></div>
</form>

<pre style="margin-top: 2em;">
  <h3 style="margin:0 0 0 12px; padding: 0; line-height: 0;">Form Validation Test</h3>
  <span>If it displays, it's valid.</span>
  {{ formData.name }}
  {{ formData.igname }}
  {{ formData.email }}
  {{ formData.mobile }}
  {{ formData.location.type }}
  {{ formData.branches.branch }}
  {{ formData.branches.alias }}
</pre>

我在这里运行我的支票(registration.php):

codes.code

我只想为注册用户分配一个taken并使用1标记http.post("server/insert.php",{'code': $code, 'fullname': scope.formData.name, 'instagram': scope.formData.igname, 'email': scope.formData.email, 'mobile': scope.formData.mobile, 'location': scope.formData.location.type, 'branch': scope.formData.branches.branch}).success(function(data, status, headers, config){ console.log("inserted successfully"); }); 列,因为其他注册用户永远不会重复使用该代码,直到它出现并且给出一条消息,表示没有更多的代码可供注册。

这是我将所有输入插入数据库中另一个表的位置。

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$code = $request->code;
@$fullname = $request->fullname;
@$instagram = $request->instagram;
@$email = $request->email;
@$mobile = $request->mobile;
@$location = $request->location;
@$branch = $request->branch;
$date = date('Y-m-d H:i:s');

$query = "INSERT INTO tbl_registration (code,fullname,instagram,email,mobile,location,branch,date_registered) VALUES ('" . $code . "', '" . $fullname . "', '" . $instagram . "', '" . $email . "','" . $mobile . "','" . $location . "','" . $branch . "','" . $date . "')";

server / insert.php

void getList(List<String> listcriteria)

1 个答案:

答案 0 :(得分:0)

您可以对照自己进行检查,如果相同则显示它们,请参阅下面的代码,了解您的表单。我想知道这不足以满足你的需求吗?将代码添加为表单中的输入,如下所示:

    <div class="wrapper">
    <div class="form-group">
        <input class="form-control" ng-class="{'has-error': $submitted && signup.name.$invalid}" type="text" name="name" ng-model="formData.name" required minlength="2" placeholder="Full Name">
    </div>

  <div class="form-group" ng-repeat="codes in response | orderBy:random | limitTo:1" ng-if="codes.branch == formData.branches.alias && codes.taken == 0">
      <input class="form-control" type="text" name="code" ng-model="formData.code" ng-value="codes.id">
  </ul>

  <div class="form-group">
    <input class="form-control" ng-class="{'has-error': $submitted && signup.igname.$invalid}" type="text" name="igname" ng-model="formData.igname" minlength="2" placeholder="Instagram Name (Optional)">
  </div>

    <div class="form-group">
    <input type="email" class="form-control" name="email" ng-class="{'has-error': $submitted && signup.email.$invalid}" ng-model="formData.email" ng-pattern="/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/" placeholder="E-mail Address" required>
  </div>

  <div class="form-group">
    <input type="number" name="mobile" ng-class="{'has-error': $submitted && signup.mobile.$invalid}" ng-model="formData.mobile" required class="form-control" minlength="11" maxlength="11" placeholder="Mobile Number">
  </div>

  <div class="form-group form-inline">
    <span class="nullable">
      <select id="regions" class="form-control" ng-model="formData.location" required ng-options="rg as rg.type for rg in region">
        <option value="">Choose Location</option>
      </select>
    </span>
    <span class="nullable">
      <select id="branches" class="form-control" ng-model="formData.branches" required ng-options="c as c[formData.location.displayName] for c in formData.location.data | orderBy:'branch'">
        <option value="">Choose Branch</option>
      </select>   
    </span>
  </div>
  <div class="form-group row">
    <button type="submit" class="submit btn">Submit</button>
  </div>
</div>

随机顺序的Javascript:

$scope.random = function(){
    return 0.5 - Math.random();
};