2017-10-19 95 views
0

我有一個頁面上的工作發佈列表。對於每個帖子,我都有一個名爲「發送給朋友」的按鈕。點擊按鈕,可以在表單中輸入朋友的電子郵件,然後提交表單。Laravel和Modals:將獨特的@foreach值傳遞給模態形式?

如何將相關工作發佈名稱提交給我的表單? {{$ posting->名稱}}

的職位信息頁面:

@foreach ($postings as $posting) 

        <a href="postings/{{ $posting->id }}"> <p> {{ $posting-> short_desc }} </p> </a> 
        <p> {{ $posting->location }} | {{ $posting->duration }} </p> 

        <button type="button" class="btn btn-outline" data-toggle="modal" href="postings/sendfriend" value="{{ $posting->short_desc }}" data-target="#sendfriend" style="background-color: #004AAE; color: #FFF; font-family: Lato; border-radius: 10px;">Send to a friend</button> 

@endforeach 

@include('postings.sendfriend') 

模態:

<div id="sendfriend" class="modal fade" role="dialog"> 
<div class="modal-dialog"> 


<div class="modal-content"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4 class="modal-title">Invite a friend to apply for this rotation</h4> 
    </div> 
     <div class="modal-body"> 
     <form class="form-horizontal" method="post" action="{{ action('[email protected]') }}"> 
    <div class="form-group"> 
    <label class="control-label col-sm-2" for="email">Email:</label> 
    <div class="col-sm-10"> 
     <input type="email" class="form-control" id="email" placeholder="Enter friend's email"> 
    </div> 
    </div> 

    <button type="submit" class="btn btn-outline"> Share the love </button> 

</div></div> 
</form></div></div> 

朋友控制器:

public function store() { 
    $friend = new Friend; 
    $friend->email = request('email'); 
    $friend->company = $posting->name; //Something like this 
} 

回答

0

發帖ID添加到您的模式按鈕

<button type="button" data-id="{{ $posting->id }}" class="btn btn-outline" data-toggle="modal" href="postings/sendfriend" value="{{ $posting->short_desc }}" data-target="#sendfriend" style="background-color: #004AAE; color: #FFF; font-family: Lato; border-radius: 10px;">Send to a friend</button> 

在你的模式窗體創建一個隱藏的輸入

<input type="hidden" name="posting"> 

使用以下JS的發佈ID傳遞到隱藏提起

$('#sendfriend').on('show.bs.modal', function (event) { 
    var button = $(event.relatedTarget) // Button that triggered the modal 
    var id = button.data('id') // Extract info from data-* attributes 
    var modal = $(this) 
    modal.find('.modal-body input[name="posting"]').val(id); 
}) 

獲取控制器中的ID

$friend->company = request('posting'); 
相關問題