用复选框填充数组,并通过http发布将其发送到php

时间:2018-08-19 08:04:30

标签: php angular typescript

因此,我是Angular2的新手,对此我一无所知。我声明了array并通过选中复选框将其填满。我想将array发送到PHP并用它来填写表格。

这是我填写array的代码。

selectedValue:Array<{name:string}>=[];

change(e, type) {
  console.log(e.target.checked);
  console.log(type.users); // users is of type User class that has attribute name

  if(e.target.checked) {
    this.selectedValue.push(type.name);
  } else {
   let updateItem = this.selectedValue.find(this.findIndexToUpdate, type.name);
   let index = this.selectedValue.indexOf(updateItem);
   this.selectedValue.splice(index, 1);
  }
}

findIndexToUpdate(type) { 
      return type.name === this;
} 

array列出了我要检查的名称列表,现在我将其发送给方法:

 addioassign(){
      this.userService
          .insioassign(this.selectedValue)
          .subscribe();
      }
 }

以及在我的服务课程中:

insioassign(info){
  return this._http.post("http://localhost/try/api/fill.php", info)
    .pipe(map(() => ""));
} 

我的php rest api:

<?php

$data1 = json_decode(file_get_contents("php://input"));

include "db.php";

//$report_id = $data1->report_id;    
$report_id = '8';

$sql="SELECT Eid FROM employee WHERE name IN ('$data1->name')";
if ($data1->name) {
    $result = $conn->query($sql);
}

if ($result->num_rows > 0) {
    // output data of each row
    $data = array();
    while($row = $result->fetch_assoc()) {
         $data[] = $row;
    }

    echo json_encode($data);
}

$r = count($data);    
for($x = 0; $x < $r; $x++) { 
    $s = implode('', $data[$x]);
    $sql1 = "INSERT INTO ioassign (report_id, Eid) VALUES($report_id, $s)";
    $result1 = $conn->query($sql1);
}
$conn->close();

请帮助。自过去一个半星期以来,我一直在研究这个问题。尚未找到最终答案。我已经尽力了。

0 个答案:

没有答案