I am trying to generate a code for certain dynamic queries, the specific case is the following, I have my users.php page, which will have registered users in a system, for this a search of users is done in a way dynamic and I put them in a div as an input each, this works correctly with the following code.
php code in which the content is displayed
<!DOCTYPE html public>
<html>
<div id="users">
<h2>Find User</h2>
<input type="text" id="busqueda" />
<br><br>
<div id="resultado">
<table id="tablesearch">
<tr>
<th>User ID</th>
<th>User Name</th>
</tr>
<?php
$sql = "SELECT * FROM WEBUser";
$i=0;
foreach($conn->query($sql) as $row){
echo '<tr>
<td "><input type="button" class="tdID n'.$i.'" value="'.$row['UserID'].'"</td>
<td "><input type="button" class="tdName n'.$i.'" value="'.$row['UserName'].' '.$row['UserLastName'].'"</td>
</tr>';
$i++;
echo '<input type="button" id="n" value="'.$i.'" hidden>';
}
?>
</table>
</div>
</div>
</html>
JQUERY that performs the real-time dynamic search of the users
$ (document) .ready (function () {
var consulta;
//hacemos focus al campo de búsqueda
$("#busqueda").focus();
//comprobamos si se pulsa una tecla
$("#busqueda").keyup(function(e){
//obtenemos el texto introducido en el campo de búsqueda
consulta = $("#busqueda").val();
//hace la búsqueda
$.ajax({
type: "POST",
url: "../buscar.php",
data: "b="+consulta,
dataType: "html",
error: function(){
alert("error petición ajax");
},
success: function(data){
$("#resultado").empty();
$("#resultado").append(data);
}
});
});
});
PHP that generates the search made with the jquery
<?php
$buscar = $_POST['b'];
buscar($buscar);
function buscar($b) {
include "connectionMXSLFWEB.php";
if($b != ""){
$sqlb = "SELECT * FROM WEBUser WHERE UserID LIKE '%".$b."%' OR UserName LIKE '%".$b."%' OR UserLastName LIKE '%".$b."%'";
}else{
$sqlb = "SELECT * FROM WEBUser";
}
echo '<table id="tablesearch">';
$i=0;
foreach($conn->query($sqlb) as $rowb){
echo '<tr>
<td "><input type="button" class="tdID n'.$i.'" value="'.$rowb['UserID'].'"</td>
<td "><input type="button" class="tdName n'.$i.'" value="'.$rowb['UserName'].' '.$rowb['UserLastName'].'"</td>
</tr>';
$i++;
echo '<input type="button" id="n" value="'.$i.'" hidden>';
}
echo '</table>';
}
$conn = null;
?>
My problem is that when I click on an input generated by the query it shows me the data I need in real time if I have to load the page, try the click () function but I do not know how to identify each one of the generated inputs and then I would not know which one to refer to this is the jquery code that I wanted to use
$(document).ready(function(){
var consulta2;
var j = $("#n").val();
for(var i = 0; i <= j; i++)
{
$(".n").click(function(f){
alert("el valor de jota es " + j);
consulta2 = $(".n"+j).val();
//hace la búsqueda
$.ajax({
type: "POST",
url: "../profile.php",
data: "p="+consulta2,
dataType: "html",
error: function(){
alert("error petición ajax");
},
success: function(data2){
$("#profile").empty();
$("#profile").append(data2);
}
});
});
}
});
And this php that makes the query
<?php
$buscar2 = $_POST['p'];
buscar2($buscar2);
function buscar2($p) {
include "connectionMXSLFWEB.php";
$sqlp = "SELECT * FROM WEBUser
JOIN WEBDepartment ON WEBUser.fk_DepartmentID = WEBDepartment.DepartmentID
WHERE UserID = '$p'";
foreach($conn->query($sqlp) as $rowp){
echo '<h3>UserID</h3>'.$rowp['UserID'].'<br><br>
<h3>Name</h3>'.$rowp['UserName'].' '.$rowp['UserLastName'].'<br><br>
<h3>Department</h3>'.$rowp['DepartmentDescription'].'<br><br>
<h3>Title</h3>'.$rowp['UserTitle'].'<br><br>
<h3>Password</h3>'.$rowp['Password'].'<br><br>
<h3>Email</h3>'.$rowp['Email'];
}
}
$conn = null;
?>