如何使用CodeIgniter中的ajax将数组数据插入数据库? 在我看来,我使用下面的代码,我有堆栈来找到解决方案,因为有许多不同的结构
<form class="form-story" action="" onsubmit="return false" id="form-story">
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #1</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #1" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #1">
</div>
</div>
</div>
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #2</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #2" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #2">
</div>
</div>
</div>
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #3</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #3" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #3">
</div>
</div>
</div>
<div id="btn-add-more-wrapper" class="form-group text-center btn-add-more-wrapper">
<a href="javascript:void(0)" id="btn-add-more">
<i class="fa fa-plus"></i> <br> Add More
</a>
</div>
<div class="btn-groups-wrapper">
<div class="form-group">
<input type="submit" onClick="" value="SUBMIT" id="btnSubmit" class="btn btn-default is-disabled">
</div>
</div>
</form>
<script>
$('#form-story').submit(function() {
var dataString = $(this).serialize();
$.ajax({
type : 'post',
url : '<?php echo base_url()?>index.php/the_flow_crew/save_tag_member',
data : dataString,
dataType : 'json',
success : function(res) {
console.log(res);
}
});
return false;
});
</script>
请帮我解决,谢谢
答案 0 :(得分:0)
将您的帖子数据制作成jsonencode并尝试存储在数据库中。它会起作用。
例如
$crew_name = json_encode($_POST['crew_name']);
上面的代码会将您的数组转换为json字符串。当您尝试以数组格式检索数据时,尝试使用json_decode($column_name)
它可以帮助您解决问题
如果您想将整个帖子存储到数据库中,请在json_encode($_POST)
如果您需要更多指导,请与我们联系。我很乐意帮助你。