2017-05-24 12 views
0

私はチェックボックス、テキストボックス、ラジオボタンを持つhtmlフォームを持っています。保存ボタンをクリックすると、フォームデータがデータベースに挿入されます。私は、フォームデータとPHPをmysqlに挿入するために、angularjsコントローラを使用しています。phpとangularjsを使用して複数の選択されたチェックボックスの値をデータベースに挿入します

質問:コントローラとPHPに選択したチェックボックスの値を挿入するにはどうすればよいですか?コード例を私に説明してください。以下は

私のコードです:

htmlコード:

<form class=rform align="center"> 
<b>Product Name:<input type="text" name="name" ng-model="newProduct.name" required=""/><br> 
Product Category: <select name="catg" ng-model="newProduct.catg" ng-options="x for x in catg" ></select><br> 
TAGS : <br> 
<ul> 
<li ng-repeat="tag in tags"> 
    <input type="checkbox" name="tags" ng-model="newProduct.tags" value="tag" ng-true-value="tag"> {{tag}} 
</li> 
</ul> 
<Status :<br><br> 
<input type="radio" ng-model="newProduct.stat" value="Active">Active 
<input type="radio" ng-model="newProduct.stat" value="Deactive">Deactive<br><br> 
<input type="hidden" ng-model="newProduct.id" /></b> 
<div class="btn"> <button type="submit" ng-disabled="rform.$invalid" ng-click="saveRecord(newProduct)">Save</button></div> 
</form> 

app.js

app.controller('ProductCtrl',function($scope,$http){ 
    $scope.tags = ["Appliances","Electronics","Men&Women","Others"] ; 

    $scope.catg = ["mobile","Air Conditioner","Kitchen appliances","Footwear","SportsWear","clothes", 
       "watches","Lptops","Televisions","Camera","Furniture","Kitchen Dining","Music","Stationery"]; 

    $scope.saveRecord = function (newProduct) { 
    $http.post("php/pinsert.php",{ 
        'name' : $scope.newProduct.name, 
        'catg' : $scope.newProduct.catg, 
        'tags' : $scope.newProduct.tags, 
        'stat' : $scope.newProduct.stat 
        }) 

        // data:$scope.products, 

         .success(function(data){ 
           alert(data); 
         }) 

       angular.forEach($scope.tags, function(tag){ 
       if (tag.selected) $scope.albumNameArray.push(tag.name); 
       tag.selected= false ; 
        }); 

       tag.selected= false ; 
    } 

    $http.get("php/pselect.php").then(function (response) { 
    $scope.myproducts = response.data.records; 
}); 


}); 

PHP:

 <?php 

    $connect = mysqli_connect("localhost", "root", "","user"); 
    $data = json_decode(file_get_contents("php://input")); 


$p_name = mysqli_real_escape_string($connect, $data->name); 
$p_catg = mysqli_real_escape_string($connect, $data->catg); 
$tags = mysqli_real_escape_string($connect, $data->tags); 
$status = mysqli_real_escape_string($connect, $data->stat); 

$query = "INSERT INTO products(pname,pcatg,tag,status) VALUES ('$p_name','$p_catg','$tags','$status')"; 
$result = mysqli_query($connect, $query) ; 
if($result == TRUE) 
    { 
     echo "Data Inserted..."; 
    } 
    else 
    { 
     echo 'Error'; 
    } 

?> 
+0

名前=タグは名前=タグにする必要があります。 – Strawberry

+0

同じエラーが発生しています。 –

+0

取得しているエラーメッセージは何ですか? –

答えて

1

私は同様にあなたのタグ配列を再構築します。チェックボックスをオンにすると、選択したプロパティがtrueに設定されます。名前は表示用です。

$scope.tags = [ 
    {"selected":false, "name":"Appliances"}, 
    {"selected": false, "name":"Electronics"}, 
    {"selected":false, "name":"Men&Women"}, 
    {"selected":false, "name":"Others"} 
]; 

チェックボックスのマークアップも再構成する必要があります。 ng-modelは$scope.newProduct.tags.selectedプロパティを使用しています。これにより、DBに保存するときに選択されたタグのプロパティが表示されます。

<li ng-repeat="tag.name for tag in tags"> 
    <input type="checkbox" name="tags" ng-model="tag.selected" value="tag" ng-true-value="tag"> {{tag.name}} 
</li> 

$scope.saveRecord()にパラメータとして渡す必要がない範囲にnewProductを割り当てます。ポスト本体にオブジェクト全体を渡すこともできます。 AJAXの呼び出しは簡略化せずに書かれていますが、いずれの方法も問題ありませんが、これを読むのが簡単です。$http.postバックエンドで

$scope.saveRecord = function(){ 
    $http({ 
     url: "php/pinsert.php", 
     method: "POST", 
     data: $scope.newProduct 
    }).success(function(data){ 
     // process returned data  
    }); 
} 

データは同じあなた$scope.newProductオブジェクトが構造化された構造化されます。私にはわからない

テーブルにチェックイン(selected.true)タグ値保存

  • 、このデータをループ
    1. 選択したタグを探す:あなたがする必要があります。 productsテーブルの正確な構造ですが、上記の3つのステップは複雑なデータをDBに保存するためのガイドです。

      $connect = mysqli_connect("localhost", "root", "","user"); 
      $data = json_decode(file_get_contents("php://input")); 
      
      foreach($data['tags'] as $tag){ 
          // Save only selected tags to products table 
          if($tag->selected){ 
           $query = " 
            INSERT INTO products(tag) 
            VALUES('$p_name','$p_catg','$tags','$status') 
           "; 
           $result = mysqli_query($connect, $query); 
          } 
      } 
      

      これはあなたの歓声を上げます。

  • 関連する問題