Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用php和angularjs在数据库中插入多个选中的复选框值_Javascript_Php_Mysql_Angularjs_Checkbox - Fatal编程技术网

Javascript 使用php和angularjs在数据库中插入多个选中的复选框值

Javascript 使用php和angularjs在数据库中插入多个选中的复选框值,javascript,php,mysql,angularjs,checkbox,Javascript,Php,Mysql,Angularjs,Checkbox,我有一个带有复选框、文本框和单选按钮的html表单。单击“保存”按钮时,表单数据将插入数据库。我使用angularjs控制器获取表单数据,并将PHP插入mysql 问题:如何在控制器和PHP中插入选中的复选框值?用代码示例向我解释 下面是我的代码: html代码: <form class=rform align="center"> <b>Product Name:<input type="text" name="name" ng-model="newProduc

我有一个带有复选框、文本框和单选按钮的html表单。单击“保存”按钮时,表单数据将插入数据库。我使用angularjs控制器获取表单数据,并将PHP插入mysql

问题:如何在控制器和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>
PHP:


我也会重新构造您的标记数组。如果选中该复选框,则所选属性将设置为true。该名称仅用于显示

$scope.tags = [
    {"selected":false, "name":"Appliances"},
    {"selected": false, "name":"Electronics"},
    {"selected":false, "name":"Men&Women"},
    {"selected":false, "name":"Others"}
]; 
复选框的标记也应重新构造。请注意,ng模型正在使用
$scope.newProduct.tags
.selected
属性。此设置允许您在保存到数据库时查看选择了哪些标记属性

<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.newProduct
对象的结构相同。您需要:

  • 循环浏览这些数据
  • 查找选定的标记
  • 将选中的(selected.true)标记值保存到表中
  • 我不知道
    产品表的确切结构,但上面的3个步骤是将复杂数据保存到数据库中的指南

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

    希望这能让你开始,干杯

    我怀疑name=tags应该是name=tags[]我收到了相同的错误。您收到的错误消息是什么?
    <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 = function(){
        $http({
            url: "php/pinsert.php",
            method: "POST",
            data: $scope.newProduct
        }).success(function(data){
            // process returned data     
        });
    }
    
    $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);
        }
    }