Currently i have a form and calling a few data using foreach's like below:
目前我有一个表单并使用foreach调用一些数据,如下所示:
@foreach($db as $intentName => $questionAnswer)
<div class="form-group edit-response-container">
<label data-toggle="collapse" data-target="#collapse-{{$number}}" aria-expanded="false" aria-controls="collapse-{{$number}}"> {{$intentName}} <i class="fa fa-chevron-down"></i></label>
<div class="collapse" id="collapse-{{$number}}">
<div class="well">
<form action="{{action('AltHr\Chatbot\ChatbotController@savedmb', [$companyID, $entityType])}}" method="post">
{{csrf_field()}}
<div class="col-md-12 btn-toolbar">
<button type="button" class="btn alt-btn alt-btn-green pull-right add-new">Add Answer</button>
<button type="submit" class="btn alt-btn alt-btn-green pull-right">Save</button>
</div>
@foreach($questionAnswer['q1'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['q2'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['q3'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['intent'] as $intent => $intent)
<input type="hidden" name="intent" value="{{$intent}}">
@endforeach
<input type="hidden" name="entityValue" value="{{$q}}" readonly/>
<br><br>
<div class="new-field">
<table class="response">
<tr>
<td class="labels"><p>Answer:</p></td>
<td><input type="text" name="newAnswer[]" value=""></td>
</tr>
</table>
</div>
</form>
</div>
</div>
</div>
<?php
$number++;
?>
@endforeach
so the function is all working fine but im having one problem as you can see from the picture below the form is the same but currently the button is showing me on each foreach if im not mistaken but i only want to have ONE submit and ONE add button and not on all foreach. How can i do that?
所以功能一切正常但我有一个问题,你可以从下面的图片看到表格是相同的但是目前按钮显示我每个foreach如果我没有弄错但我只想有一个提交和一个添加按钮而不是所有的foreach。我怎样才能做到这一点?
在此处输入图像描述
my controller as below:
我的控制器如下:
public function savedmb(Request $request, $companyID, $entityType)
{
$a = count($request->question);
for ($p=0; $p < $a; $p++) {
$nq = new DiraQuestion;
$nq->intent = strtolower($request->intent);
$nq->eType = $entityType;
$nq->eVal = strtolower($request->entityValue);
//$nq->synonym = strtolower($request->syn);
$nq->queries = $request->question[$p];
$nq->company_id = $companyID;
$nq->save();
}
$x = count($request->newAnswer);
for ($i=0; $i < $x; $i++) {
$nr = new DiraResponses;
$nr->intent = strtolower($request->intent);
$nr->eType = $entityType;
$nr->eVal = strtolower($request->entityValue);
$nr->reply = strtolower($request->newAnswer[$i]);
$nr->company_id = $companyID;
$nr->save();
}
return back();
}
and my route:
和我的路线:
Route::post('savedmb/{companyID}/{entityType}','Chatbot\ChatbotController@savedmb');
1 个解决方案
#1
1
Place your button out of
loop:
将按钮置于循环之外:
<div class="form-group edit-response-container">
<label data-toggle="collapse" data-target="#collapse-{{$number}}" aria-expanded="false" aria-controls="collapse-{{$number}}"> {{$intentName}} <i class="fa fa-chevron-down"></i></label>
<div class="collapse" id="collapse-{{$number}}">
<div class="well">
<form action="{{action('AltHr\Chatbot\ChatbotController@savedmb', [$companyID, $entityType])}}" method="post">
{{csrf_field()}}
<div class="col-md-12 btn-toolbar">
<button type="button" class="btn alt-btn alt-btn-green pull-right add-new">Add Answer</button>
<button type="submit" class="btn alt-btn alt-btn-green pull-right">Save</button>
</div>
@foreach($db as $intentName => $questionAnswer)
@foreach($questionAnswer['q1'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['q2'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['q3'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['intent'] as $intent => $intent)
<input type="hidden" name="intent" value="{{$intent}}">
@endforeach
<input type="hidden" name="entityValue" value="{{$q}}" readonly/>
<br><br>
<div class="new-field">
<table class="response">
<tr>
<td class="labels"><p>Answer:</p></td>
<td><input type="text" name="newAnswer[]" value=""></td>
</tr>
</table>
</div>
<?php
$number++;
?>
@endforeach
</form>
</div>
#1
1
Place your button out of
loop:
将按钮置于循环之外:
<div class="form-group edit-response-container">
<label data-toggle="collapse" data-target="#collapse-{{$number}}" aria-expanded="false" aria-controls="collapse-{{$number}}"> {{$intentName}} <i class="fa fa-chevron-down"></i></label>
<div class="collapse" id="collapse-{{$number}}">
<div class="well">
<form action="{{action('AltHr\Chatbot\ChatbotController@savedmb', [$companyID, $entityType])}}" method="post">
{{csrf_field()}}
<div class="col-md-12 btn-toolbar">
<button type="button" class="btn alt-btn alt-btn-green pull-right add-new">Add Answer</button>
<button type="submit" class="btn alt-btn alt-btn-green pull-right">Save</button>
</div>
@foreach($db as $intentName => $questionAnswer)
@foreach($questionAnswer['q1'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['q2'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['q3'] as $question => $questionID)
<table class="question">
<tr>
<td class="labels"><p>User:</p></td>
<td><p> {{ str_replace("<title>", "$q", $question) }}</p></td>
<input type="hidden" name="question[]" value="{{ str_replace("<title>", "$q", $question) }}">
</tr>
</table>
@endforeach
@foreach($questionAnswer['intent'] as $intent => $intent)
<input type="hidden" name="intent" value="{{$intent}}">
@endforeach
<input type="hidden" name="entityValue" value="{{$q}}" readonly/>
<br><br>
<div class="new-field">
<table class="response">
<tr>
<td class="labels"><p>Answer:</p></td>
<td><input type="text" name="newAnswer[]" value=""></td>
</tr>
</table>
</div>
<?php
$number++;
?>
@endforeach
</form>
</div>