多FORM提交


最近想写一个投票系统,类似于微信的投票系统。
数据库:三个库,分别为投票信息库、投票问题库和投票选项库。三者都以上级的ID进行关联。
WEB界面:一个主FORM
<form method="POST" id="vote" action="{{route('admin/updatevote')}}" class="form-horizontal">
{{csrf_field()}}
<div class="form-group">
<label for="votename" class="col-sm-2 control-label">投票名称</label>
<div class="col-sm-9">
<input class="form-control" required type="text" placeholder="请输入投票名称" value="{{old('votename')}}" name="votename"/>
</div>
</div>
<div class="form-group">
<label for="enddate" class="col-sm-2 control-label">截止时间</label>
<div class="col-sm-4">
<div class='input-group date' id='datetimepicker1'>
<input type='text' required class="form-control" name="enddate" />
<span class="input-group-addon">
<i class="fa fa-calendar" aria-hidden="true"></i>
</span>
</div>
</div>
</div>
</form>
N个从FORM
<form id="question_0" class="form-horizontal">
<div class="panel panel-default ">
<div class="panel-heading"><span>问题一</span>
<div class="pull-right">
<a href="javarscript:;" class="delete_question" >删除问题</a>
</div>
</div>
<div class="panel-body">
<div class="form-group">
<label for="questiondesc" class="col-sm-2 control-label">标题</label>
<div class="col-sm-7">
<input type='text' class="form-control" name="questiondesc_0" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-1">
<label class="radio-inline">
<input type="radio" name="questiontype" id="inlineRadio1" value="single"> 单选
</label>
<label class="radio-inline">
<input type="radio" name="questiontype" id="inlineRadio2" value="multi"> 多选
</label>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">选项一</label>
<div class="col-sm-7">
<input type='text' required class="form-control answer" name="answer_0" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">选项二</label>
<div class="col-sm-7">
<input type='text' required class="form-control answer" name="answer_1" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">选项三</label>
<div class="col-sm-7">
<input type='text' required class="form-control answer" name="answer_2" />
</div>
<div class="col-sm-1">
<a href="javascript:;" class="delete_option" data-tag="question_0" data-item="answer_3">删除选项</a>
</div>
</div>
</div>
<div class="panel-footer">
<a href="javascript:;" class="add_option" data-tag="question_0">新增选项</a>
</div>
</div>
</form>
<div>
<a href="javascript:;" style="width:100%;" class="btn btn-default add_question">新增问题</a>
</div>
我如何通过这些FORM来将投票系统更新?如果从FORM在上传时出错如何回滚使得所有已经插入的数据全部删除?

以下是我现在的方法,先提交主FORM获取投票ID,再循环一个个提交从FORM
$(".submit-vote").click(function(){
var voteid=0;
if($("input[name='votename']").val()=="")
{
alert("请输入投票名称!");
return;
}
else if( $("input[name='enddate']").val()=="")
{
alert("请输入投票截止日期!");
return;
}
else
{
$.ajax({
type: "POST",
url:"{{route('admin/updatevote')}}",
data:$('form#vote').serialize(),
async: false,
error: function(request) {
alert("连接出错!");
},
success: function(data) {
voteid=data;
}
});
}
questions=$(this).parent('div').siblings('form').not('#vote');
for(var i=0;i<
questions.length;i++)
{
questions.eq(i).append('{{csrf_field()}}');
questions.eq(i).append("<input class='sr-only' type='text' name='voteid' value='"+voteid+"'></input>")
$.ajax({
type: "POST",
url:"{{route('admin/updatequestion')}}",
data:_questions.eq(i).serialize(),
async: false,
error: function(request) {
alert("连接出错!");
},
success: function(data) {
alert('成功');
}
});
}
});


Vote Vote Cancel Collect Collect Cancel

<< 上一篇: 求助大神,我在使用排除csrf保护时,一直无效

>> 下一篇: laravel5.2版本,在iframe调用下,返回错误的跳转链接无法获取