wordpress为数据库添加多个复选框

时间:2016-10-13 07:42:52

标签: php wordpress

我在如何在wordpress $ wpdb中插入多个复选框时遇到问题。请帮助我或我可以学习的任何资源。非常感谢你

   <div class="form-group">
    <ul class = "arrangement">
        <li><label class="checkbox-inline"><input type="checkbox" name = "meal_arrangement[]"  value="Drinking Water">Drinking Water</label></li>
        <li><label class="checkbox-inline"><input type="checkbox" name = "meal_arrangement[]"  value="Snack">Snack</label></li>
        <li><label class="checkbox-inline"><input type="checkbox" name = "meal_arrangement[]"  value="Lunch">Lunch</label></li>
        <li><label class="checkbox-inline"><input type="checkbox" name = "meal_arrangement[]"  value="Dinner">Dinner</label></li>
    </ul>
    </div>

这是我的ajax

 $('#submit').click(function(){

 var dataform  =  $('#v_form').serialize();
 var checkValues =  $('input[name="meal_arrangement[]"]:checked').map(function(){return $(this).val();}).get();

 $.ajax({
            type: "post",
            url: ajax_params.ajax_url,
            data: {"action": "data", 
                    "meal_arrangement":checkValues,
             },
               success: function(data){
               console.log('inserted');
           alert('ok');
        }
    });  
 });

我的wordpress功能:

  $table_client_event_meal = $wpdb->prefix."spaces_client_event_meal";
                 $meal_arrangement = sanitize_text_field($_POST["meal_arrangement"]);

                 $result2 =  $wpdb->insert( $table_client_event_meal, 
                    array( 
                      'meal_arrangement'=>$meal_arrangement,
                   )
                 );

1 个答案:

答案 0 :(得分:1)

大家好我通过在wordpress功能中添加for循环条件来获得解决方案:

                 $meal_arrangement = $_POST["meal_arrangement"];
                 $count = count($meal_arrangement);   
                 for ( $i=0; $i < $count; $i++ ){

                 $result2 =  $wpdb->insert( $table_client_event_meal, 
                    array( 
                      'meal_arrangement'=>$meal_arrangement[$i],
                   )
                 );
               }