I retrieve the campos.idCurso
from a function recolher
where it sends the value of the selected row in the table. In this first code, where is it getting this value from the table, and selecting that value in select2:
function recolher(n1){
campos.idAluno = document.getElementById(“coluna”+n1+“0").innerHTML;
campos.nome = document.getElementById("coluna”+n1+“1").innerHTML;
campos.matricula = document.getElementById("coluna”+n1+“2").innerHTML;
campos.idCurso = document.getElementById("coluna”+n1+“3").value;
campos.anoEntrada = document.getElementById("coluna”+n1+“4").innerHTML;
campos.anoTermino = document.getElementById("coluna”+n1+“5").innerHTML;
campos.obs = document.getElementById("coluna”+n1+"6").innerHTML;
document.getElementById(“id_aluno”).value = campos.idAluno;
document.getElementById(“nome”).value = campos.nome;
document.getElementById(“matricula”).value = campos.matricula;
$(’#selcurso’).val(campos.idCurso);
$(’#selcurso’).trigger(‘change’);
document.getElementById(“anoEntrada”).value = campos.anoEntrada;
document.getElementById(“anoTermino”).value = campos.anoTermino;
document.getElementById(“obs”).value = campos.obs;
}
Already in this second code, it shows where eh is called the ajax function where it calls the function selectViaAjax:
<select class='js-example-basic-multiple form-control' id='selcurso' name='selCurso' size ='1'></select>
$(document).ready(function() {
selectViaAjax("#selcurso", "curso", "nome", "id_curso");
});
And finally this is the function selectViaAjax:
function selectViaAjax(identificador, tabela, principal, value) {
$(identificador).select2({
ajax: {
url: 'php_action/asJSONSelect2.php',
type: "POST",
dataType: 'json',
data: function (params) {
return {
q: params.term
}
},
data: jQuery.param({tabela: tabela,valor:principal,codigo:value}),
processResults: function (data, params) {
var search = params.term? params.term : "";
data.results = data.results.filter(function(value){
return value.text.includes(search);
});
return data;
}
}
});
}
Where it passes through this file called asJSONselect2.php that contains this code:
<?php
require "../_classe/metodos.php";
include("conexao.php");
$metodos = new Metodos;
$metodos->gerarJSONSelect2($conn, $_POST["tabela"], $_POST["valor"], $_POST["codigo"]);
?>
And it arrives to this last function filling the select2 with the information of the database:
function gerarJSONSelect2($conn, $tabela,$campoLiteral,$campoCodigo){
$db = new mysqli('localhost', 'root', '', 'estagio');
$sql = "SELECT * FROM ".$tabela;
$query = mysqli_query($conn, $sql);
$registros = array();
while ($registro=mysqli_fetch_array($query)){
$registros[] = array("text" => $registro[$campoLiteral], "id"=>$registro[$campoCodigo]);
}
$resultados = array("results" => $registros);
echo json_encode($resultados);
}
The only problem I am having to terminate the program, is not being able to do the function recolher
select the value I get from the table in select2, because the code I used is not working, I already checked to see if it was the value of campos.idCurso
, but this is getting the right amount. If you can give me the right code for this case. I thank you for your help.