2017-05-10 12 views
0

次のフォームがあり、データをコントローラに送信します。問題は、動的に作成された入力からのデータがコントローラに渡されていないことです。この動的に作成された入力のデータがコントローラに渡されません

` 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
     <script 

    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 

      <META http-equiv="Content-Type" content="text/html; charset=utf-8"></head> 
     <body> 
      <div> 
       @if(count($errors)>0) 
        <div class="alert alert-danger"> 
         @foreach($errors->all() as $error) 
         <p>{{$error}}</p> 
         @endforeach 
        </div> 
       @endif 
      </div> 
      <div> 

       <form action="{{route('course.save')}}" method="post"> 
       <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
       <label>Select a category</label> 
        <select> 
         <option>Category 1</option> 
         <option>Category 2</option> 
         <option>Category 3</option> 
         <option>Category 4</option> 
        </select> 
        <br><br> 
       <label>Select a subcategory</label> 
       <select> 
        <option>SubCategory 1</option> 
        <option>SubCategory 2</option> 
        <option>SubCategory 3</option> 
        <option>SubCategory 4</option> 
       </select> 
       <br><br> 

       <label for="course">Course Name:</label> 
       <input type="text" name="course" id="course"/> 

       <div id="content"> 
        <div id="section-content-1"> 
        <div id="secs"> 
         <label for="section">Section 1:</label> 
         <input type="text" name="section" id="section"/> 
        </div> 
        <div id="lects"> 
         <label for="lecture">Lecture 1:</label> 
         <input type="text" name="lecture" id="lecture"/> 
        </div> 
        <button type="button" id="add-lect" data-lect="1">Add New Lecture</button><br><br> 
        </div> 
       </div> 

       <button type="button" id="add-sect" data-sect="1" >Add New Section</button> 
       <br><br><br><br> 

       <button class="btn-primary" type="submit">Save</button> 
       </form> 

      </div> 
     </body> 
    </html> 


    <script src="{{asset('/js/code.js')}}"></script>` 

welcome.blade.php dd($request->all());

`array:4 [▼ 
    "_token" => "gzpNTHPfZl8GCtNbCPGJrc4S4zTUmhhMUve4gyet" 
    "course" => "ICTP" 
    "section" => "BB3" 
    "lecture" => "functions and pointers" 
] 
` 

enter image description here の出力は、ここでjqueryのファイルcode.jsが

//$('#add-lect').click(function 
$('#content').on('click', '#add-lect', function() { 
    var count = parseInt($(this).parent().children('#lects').children().length/2) + 1; 
    lectures = '<label>Lecture ' + count + ':</label><input type="text" name="lecture" id="lecture"/>' 
    $(this).parent().find('#lects').append(lectures); 
}); 

$('#add-sect').click(function(){ 
    sect_id = parseInt($('#add-sect').attr('data-sect')) + 1; 
    $('#add-sect').attr('data-sect', sect_id) 
    var count = ($("#secs").children().length/2) + 1; 

    //section = '<div id="section-content-'+sect_id+'"> <div id="secs"> <label for="section">Section'+sect_id+':</label><input type="text" name="section" id="section-"/></div><div id="lects"><label for="lecture">Lecture 1:</label><input type="text" name="lecture" id="lecture"/></div><button type="button" id="add-lect" data-lect="1">Add New Lecture</button<br><br></div>' 
    section = '<div id="section-content-'+sect_id+'"><div id="secs"><label for="section">Section '+sect_id+':</label> <input type="text" name="section" id="section"/></div><div id="lects"><label for="lecture">Lecture 1:</label><input type="text" name="lecture" id="lecture"/></div><button type="button" id="add-lect" data-lect="1">Add New Lecture</button><br><br></div>' 

    $('#content').append(section); 
}); 
+0

これは 'name =" lecture "ではありません。' 'name =" lecture [] "' –

+0

@u_mulderになりました。私は講義の価値を得ています。 – Amjad

答えて

1

あなたが入力フィールドがたくさんあるです同じ名前で

<input type="text" name="section" id="section"/> 
<input type="text" name="section" id="section"/> 
<input type="text" name="section" id="section"/> 
<input type="text" name="section" id="section"/> 

最後入力フィールドはサーバーに渡されます。 name属性のための特別な記法があります同様のデータの配列を渡す

:これを使用して

<input type="text" name="section[]"/> 
<input type="text" name="section[]"/> 
<input type="text" name="section[]"/> 

は、サーバー側で、あなたは値を反復して取得することができます配列として$_POST['section']を、持っています。

id="section"属性をid属性として削除する必要があります。ページ上では一意である必要があります。

入力の名前を変更するには、bladeテンプレートとjavascriptコードの両方で変更する必要があります。

更新:あなたが見るように、そう

<input type="text" name="section[1]"/> 
<input type="text" name="lecture[1][]"/> 
<input type="text" name="lecture[1][]"/> 

<input type="text" name="section[2]"/> 
<input type="text" name="lecture[2][]"/> 
<input type="text" name="lecture[2][]"/> 
<input type="text" name="lecture[2][]"/> 

<input type="text" name="section[3]"/> 
<input type="text" name="lecture[3][]"/> 
<input type="text" name="lecture[3][]"/> 

- あなたは明示的に名前のインデックスを定義することができます。

one-to-many関係の場合、あなたはあなたの命名を拡張することができます。

上記のケースでは、$_POST['section']を繰り返し処理し、セクションと同じインデックスの$_POST['lecture']を取得できます。

関連する問題