Write Hmtl Table data to Database | Laravel 5.1

Asked

Viewed 598 times

1

I have a question regarding recovering the information from an html table and saving it in the database using Windows.

This part of the application consists of storing in a table user_exame information related to exams that this user should perform.

In the table I store the user_id, exame_id, dt_utlimo_exame, dt_proximo_exame

I’m populating the html table with following code:

eventsForm: function(){	

    jQuery('#btn-adicionar-exame').click(function(){

	moment.locale('pt-br');

	var id = jQuery('#exame').find('option:selected').val();
	var exame = jQuery('#exame').find('option:selected').text();
	var periodical = jQuery('#periodical').find('option:selected').val();
	var periodicalText = jQuery('#periodical').find('option:selected').text();
	var ultimoExame = jQuery('#dtUltimoExame').val();
	var newRow = $("<tr>"); 
	var cols   = "";
	var proximoExame = '';
			
	//Define a proxima data do exame
	proximoExame = moment(ultimoExame, "DD/MM/YYYY").add(periodical, 'months');

	cols += '<td>' + id + '</td>';
	cols += '<td>' + exame + '</td>';
	cols += '<td>' + periodicalText + '</td>';
	cols += '<td>' + ultimoExame + '</td>';
	cols += '<td>' + moment(proximoExame, "DD/MM/YYYY").calendar() + '</td>';
			
	cols += '<td>';
	cols += '<button type="button" class="btn btn-link " id="btn-delet" onclick="employee.removeTableRow(this)"><i class="fa fa-trash fa-1x color-red"></i></button>';
	cols += '</td>';

	newRow.append(cols);
			
	/Adicionando a row a tabela
	jQuery("#table-list-exames").append(newRow);

        //Limpando os campos
	jQuery('#exame').val("").change();
	jQuery('#periodical').val("").change();
	jQuery('#dtUltimoExame').val('');

});
},

My question is: how to recover each row of the table and store in the bank using Laravel?

I know that in javascript could do something like:

tableExames.find('tr').each(function (i) {...} 

but how to do this in the Laravel controller?

1 answer

1

You will have to do an ajax for each row included in that table there. To do this, use a selector for the table rows: $('#table-list-exames tr') and then just use the each from jQuery to get the information:

$('#table-list-exames tr').each(function(i, linha){
   // pega as colunas de cada linha
   var colunas = $(linha).find('td'); 
   // transforma em um objeto
  var registro = {};
  $(colunas).each(function(j, col){
    registro[j] = $(col).text();
  });
  console.log(registro);
  // manda por ajax pro servidor
  //cadastrar_exame(registro);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody id="table-list-exames">
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
  </tbody>
</table>

Of course, process the data before sending by Ajax. Needing help with Ajax, this documentation from jQuery is pretty cool: http://jqapi.com/#p=jQuery.post

  • I only realized later that your question is about the Laravel’s controllers part. Sorry... but once you handle the data, the backend part isn’t crazy, right?

  • No problem. I still can’t find a solution. I thought about calling a javascript function by the onclick event instead of giving a Submit in the form. In this function I would retrieve table information and store it in an object. Then I would call the controller method by passing this object. Well, I don’t know if it would work or if it would be the right way. Chagando at home I will try to do this.

  • So, this solution I described would do the part of sending the data to PHP using Ajax, I just didn’t put the part of sending to the server using Javascript, because I thought it would be a lot. Once you send using the jQuery.post no error, will get there as if it were a normal PHP array, staying inside the $_POST variable. The part of Laravel that I don’t know because I never used this framework.

  • Guys, I still can’t solve my problem. Could you help me?

  • What is going on? You have to give the details, man, we’re not telepathic :) Anyway, better ask a new question, unless you’re still in the same trouble.

Browser other questions tagged

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