Loop form on the Laravel

Asked

Viewed 108 times

0

Good morning, I’m trying to make a form in a loop for each row of the table, when the user clicks the button on a row, picks up the data from that row and loads on another page, but for some reason in the loop the inputs don’t stay inside the form, follows the code of the loop:

if($total_row > 0)
      {
       foreach($data as $row)
       {
        $output .= '
    <tr>
             <form  action="monitor/info" method="POST">
              {{ csrf_field() }}
             <td> <input type="text" readonly="" style="width: 310px"  name="nome" id="nome" value="'.$row->nome.'" /></td>
             <td> <input type="text" readonly="" style="width: 80px" name="usuariox" id="usuariox" value="'.$row->usuario_x.'" /></td>
             <td> <input type="text" readonly="" style="width: 100px" name="aspect" id="aspect" value="'.$row->aspect.'" /></td>
             <td> <input type="text" readonly="" name="supervisor" id="supervisor" value="'.$row->supervisor.'" /></td>
             <td> <input type="text" readonly="" style="width: 70px" name="setor" id="setor" value="'.$row->setor.'" /></td>  
             <td> <input type="submit" class="btn btn-primary" value="Criar Laudo"></td>
             </form>
            </tr>
';
       }
      }
      else
      {
       $output = '
       <tr>
        <td align="center" colspan="5">Nehum registro encontrado!</td>
       </tr>
       ';
      }

Follow in the browser as you interpret: inserir a descrição da imagem aqui

Can someone tell me where I’m going wrong?

  • I didn’t understand your loop... how you make a loop without a for, while etc...?

  • starts like this: if($total_row > 0) { foreach($date as $Row) { $output .= ' ' and ends like this: '; } } Else { $output = ' <tr> <td align="center" colspan="5">Nehum record found! </td> </tr> '; }

  • Edit your question and add to the code so it’s easier to view

  • 1

    Question was edited

  • I tried with GET too but the form is still there with nothing inside =(

  • a moment, I am formulating an answer :D

  • by the way, which version of the Laravel you are using?

  • Version of Laravel 5.7

Show 3 more comments

2 answers

1

If you check the documentation of <tr> will see the following line:

Permitted content: Zero or more <td> and/or <th> Elements; script-supporting Elements (<script> and <template>) are also allowed.

That is, in a valid HTML document, only the elements <th>, <td>, <script> and <template> are allowed within an element <tr>.

This means that although your string is being "mounted" the way you want it to be, the browser makes changes when mounting the DOM tree by removing or changing invalid code, which is your case.

One solution I would use would be to use the <form> in a valid place and use the attribute form of <input> (compatibility) to specify which form this <input> belonging.

Example:

<?php

$count = 0;
foreach($data as $row) {
    $count++;
    $output .= <<<HTML
        <tr>
            <td>
                <form id="form-$count" action="monitor/info" method="POST">
                    {{ csrf_field() }}
                    <input name="nome" value="{$row->nome}">
                </form>
            </td>
            <td><input name="usuariox" value="{$row->usuariox}" form="form-$count"></td>
            <td><input name="aspect" value="{$row->aspect}" form="form-$count"></td>
            <td><input name="supervisor" value="{$row->supervisor}" form="form-$count"></td>
            <td><input name="supervisor" value="{$row->supervisor}" form="form-$count"></td>
            <td><input name="setor" value="{$row->setor}" form="form-$count"></td>
            <td><input type="submit" class="btn btn-primary" value="Criar Laudo"></td>
        </tr>
HTML;
}
  • This is a good solution, but the form is mounted on a controller and brings a json to the Blade like this: Function fetch_customer_data(query = ') { $.ajax({ url:"{{ route('live_search.action') }}", method:'GET', data:{query:query}, dataType:'json', Success:Function(data) { $('tbody').html(data.table_data); $('#total_records').text(date.total_data); } }); } in this case how can I mount the elements for the form in the ajax request? this page does a search by clicking the key, so it has to be ajax, thanks for the help

  • I didn’t understand your doubt, I just copied your own for and changed. The structure is its own code.

  • I made this change, but it was very slow to search, there must be a way to capture the json and mount the form in Blade, if you mount like this in the controller it does not work

0

Good afternoon friends, just passing to inform that I found a simple solution to this problem, I used the javascript session, because the object was simple, send information from one page to another, use call by form, and this does not change the speed of the search, remains the same,

if($total_row > 0)
      {
       $count = 0;   
       foreach($data as $row)
       {
        $count ++;   
        $output .= '
        <tr>
         <td> <input type="text" readonly="" style="width: 310px"  name="nome'.$count.'" id="nome'.$count.'" value="'.$row->nome.'" /></td>
         <td> <input type="text" readonly="" style="width: 80px" name="usuariox'.$count.'" id="usuariox'.$count.'" value="'.$row->usuario_x.'" /></td>
         <td> <input type="text" readonly="" style="width: 100px" name="aspect'.$count.'" id="aspect'.$count.'" value="'.$row->aspect.'" /></td>
         <td> <input type="text" readonly="" name="supervisor'.$count.'" id="supervisor'.$count.'" value="'.$row->supervisor.'" /></td>
         <td> <input type="text" readonly="" style="width: 70px" name="setor'.$count.'" id="setor'.$count.'" value="'.$row->setor.'" /></td>  
         <td> <button type="button" class="btn btn-primary" id="btn'.$count.'">Criar Laudo</button></td>

         <script>

              $("#btn'.$count.'").click(function(){
                  localStorage.setItem("nome", $("#nome'.$count.'").val());
                  localStorage.setItem("usuariox", $("#usuariox'.$count.'").val());
                  localStorage.setItem("aspect", $("#aspect'.$count.'").val());
                  localStorage.setItem("supervisor", $("#supervisor'.$count.'").val());
                  localStorage.setItem("setor", $("#setor'.$count.'").val());    
                  window.location.href = "http:...endereço da página";
              });

         </script>

        </tr>
        ';
       }
      }
      else
      {
       $output = '
       <tr>
        <td align="center" colspan="5">Nehum registro encontrado!</td>
       </tr>
       ';
      }

and rescue those sessions on the other page with jquery. abç

Browser other questions tagged

You are not signed in. Login or sign up in order to post.