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

时间:2022-09-25 21:40:45

I can display the entire table of my database on my partial view, but what I want is to filter that and compare one of them with the output from my form. This is how I display my entire table.

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

server/fetch.php

<?php 

  $connect = mysqli_connect("localhost", "root", "ralph2012", "mipadsignup");
  $result = mysqli_query($connect, "SELECT * FROM tbl_codes");

  $data = array();

  while ($row = mysqli_fetch_array($result)) {
    $data[] = $row;
  }

  print json_encode($data);
?>

app.js (where my controller is)

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

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

registration.php

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

The formData.branches.alias is taken from the output produced by one of my form elements. I want its value to be compared to codes.branch, which is from the database. I do not want to list everything. I just want a portion of my codes.branch to run a check within its similar row values.

formData.branches.alias取自我的一个表单元素生成的输出。我希望将它的值与codes.branch进行比较,它来自数据库。我不想列出一切。我只想要我的一部分codes.branch在其类似的行值内运行检查。

After comparing the two, I want to run again through its columns and check if any of them are available. If they are, I will randomly select one of the values to be inserted to my other table in the database.

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

[UPDATE]: As requested, this is my form

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

<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>

I run my checks here (registration.php):

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

<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>

I want to assign just one codes.code to the registered user and mark the taken column with 1 as the code can never be reused by other registered users, until it rans out and gives a message that no more codes are available for registration.

我想只为注册用户分配一个codes.code,并将获取的列标记为1,因为其他注册用户永远不会重复使用该代码,直到它出现并发出一条消息,表明没有更多的代码可用于注册。

This is where I insert all the inputs to the other table in my database.

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

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");
    });

and the server/insert.php

和server / insert.php

$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 . "')";

1 个解决方案

#1


You can check them against themselves and display them if they are equal see below code as per seeing your form. i'm wondering wouldn't this suffice your needs? add the code as an input in your form like below:

您可以自行检查它们并在它们相同时显示它们。根据下面的代码查看您的表单。我想知道这不足以满足你的需求吗?将代码添加为表单中的输入,如下所示:

    <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 for random order :

Javascript为随机顺序:

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

#1


You can check them against themselves and display them if they are equal see below code as per seeing your form. i'm wondering wouldn't this suffice your needs? add the code as an input in your form like below:

您可以自行检查它们并在它们相同时显示它们。根据下面的代码查看您的表单。我想知道这不足以满足你的需求吗?将代码添加为表单中的输入,如下所示:

    <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 for random order :

Javascript为随机顺序:

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