How to join Datatables with PHP in a checkbox search?

Asked

Viewed 199 times

-1

Get ready because business is very complex.

I’m using the library datatables of Jquery (I’m pretty new at this) to list a table from my bank, when listing, each record received a checkbox so I could get the id of the selected records.

Intent: save the selected ones as foreign key in another table,

NOTE: the page should not reload until all records that the user wants to mark are selected.

Problem: The datatables only recognizes the id that is in the current tab, when changing tab they are not sent to my PHP.

To solve I need to be able to send all Ids selected in all tabs for the PHP, also tried with a search box, but could not send the result that appeared down to the DIV side without giving RELOAD on the page.

I’ll leave the repository link on Github so you want to try: Link to the Github

Don’t call the organization or anything else, I’d appreciate it if you’d focus on the problem first.


I found part of a possible solution:

I’m using Jquery to take the event click on checkbox, capture id and place in a input as text.

The problem now is to treat the ids, give the explode in them is easy but take out the ids you uncheck not yet managed.

Intention: To remove the Ids of input by unchecking a checkbox

Problem: I don’t know how to locate this string let alone remove it

To test use the save button after checking and unchecking a checkbox, you will realize that you will have repeated ids that should no longer be there;

I’ll leave the repository link on Github so you want to try:

Link to the Github

  • Hello, to point out that your question has been solved, do not edit the title to solved. Instead, accept the correct answer.

  • Thank you, I entered the forum a few days, so it is normal to err, but I’m getting used, thank you for warning.

1 answer

0

Well after much suffering for not having experience with Jquery I managed to use it to just put in the input when the marker is Checked = TRUE, when false I use the replace of Javascript to remove it from inside the input. This time I’m gonna put the code down for whoever wants to know how:

$(document).ready(function () {
    $('#habilidades').DataTable()
})

$(".hab").click(function () {
    var txt = $('#test').val()
    var h = $(this).val()
    if($(this).is(':checked')){
        txt = txt + (h + '-')
    }else{
        txt = txt.replace($(this).val()+'-', '')
    }
    $('#test').val(txt)
});
<!DOCTYPE HTML>
<html>

<head>
	<meta charset="utf-8">
	<title>Sistema de Busca sem Refresh</title>
	<script type="text/javascript" src="Jquery.js"></script>
	<link rel="stylesheet" href="datatables/datatables.css">
	<script type="text/javascript" src="javascriptpersonalizado.js"></script>
	<script type="text/javascript" src="datatables/datatables.js"></script>
	<script type='text/javascript' href='meujavascript.js'></script>
</head>

<body>
	<form action="salvar.php" method='POST' enctype='multipart/form-data'>
		<table id="habilidades">
			<thead>
				<tr>
					<th>ID</th>
					<th>Nome</th>
					<th>Tipo</th>
					<th>Descrição</TH>
				</tr>
			</thead>
			<tbody>
				<?php
				include "conexao.php";
				$dados = $pdo->query('SELECT * FROM lista_habilidade');
				while ($col = $dados->fetch(PDO::FETCH_ASSOC)) {
					$id = $col['id_habilidade'];
					$nome = $col['nome_habilidade'];
					$tipo = $col['tipo_habilidade'];
					$descricao = $col['descricao_habilidade'];
					echo "
					<tr>
						<td>$id</td>
						<td><input class='hab' type = 'checkbox' name='habilidade[]' onclick='teste(this)' value='$id'>$nome</td>
						<td>$tipo</td>
						<td>$descricao</td>
					</tr>
					";
				}
				?>
			</tbody>
		</table>
		<button type='submit'>Salvar</button>
		<div>
			<input type='text' name='habilidades' id='test'>
		</div>
	</form>
	<script type="text/javascript" src="meujavascrip.js"></script>
</body>

</html>

It will not be working here for missing the Bank but it will still be useful, anyone who wants to see working will be as always in my Github, I thank everyone who saw, even without getting answer, because I know that this problem was very complex, even more for dealing with external libraries.

Link to the Github

Browser other questions tagged

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