auto-complete input with jquery php

Question:

I would like to convert this code into jquery.

To say. Instead of appearing in the iframe, I would like it to be a single page normally, and the insertion was without refresh.

$.post("resultado.php", function(result){
          $("#id").val(result.id);
          $("#nome").val(result.nome);
          $("#idade").val(result.idade);
      }
);

I had thought of $.get / post(function(resposta)) ,

but here only returns a single answer and not the full result that I would like.

The result.php page:

<?
$mysqli = new mysqli("localhost", "usuario1", "senha1","banco1");   

if($_GET[id]) {

$res_a = $mysqli->query("select id, nome, idade from usuarios where id='$_GET[id]'");
$res_b = $res_a->fetch_assoc(); 

$id = $res_b[id];
$nome = $res_b[nome];
$idade = $res_b[idade];

?>
<table>
<tr>
<td>ID</td><td><input value="<?=$id?>"></td></tr>
<td>NOME</td><td><input value="<?=$nome?>"></td></tr>
<td>IDADE</td><td><input value="<?=$idade?>"></td></tr>
</table>
<? exit; } ?>


<table width="100%">
<tr>
   <td>

<table>
<?
$result = $mysqli->query("select id, nome from usuario");
while($row = $result->fetch_assoc()) {
?>
<tr>
   <td><a href="?id=<?=$row[id]?>" target="frame"> <?=$row[id]?> </a></td>
   <td><a href="?id=<?=$row[id]?>" target="frame"> <?=$row[nome]?> </a></td>
</tr>
<? } ?>
</table>
</td>
<td>
<iframe name="frame" />
</td>
</tr>
</table>

Answer:

I ACHIEVED.

<?
$my_sql = "localhost";
$mysqli = new mysqli($my_sql, "usuario1", "senha1", "banco1");  

if($_POST[rs_id]) {
    $id=$_POST[rs_id];
    $res_a = $mysqli->query("select id, nome, idade, musica from temp where id='$id'");
    $ln = $res_a->fetch_assoc();

    $resultado = array(
        "a" => $ln['id'],
        "b" => $ln['nome'],
        "c" => $ln['idade'],
        "d" => $ln['musica']
    );
    echo json_encode($resultado);
exit();
}

if($_GET[id]) {
$id = addslashes($_GET[id]);
$res_a = $mysqli->query("select id, nome, idade, musica from temp where id='$id'");
$res_b = $res_a->fetch_assoc();

$id = $res_b[id];
$nome = $res_b[nome]; 
$idade = $res_b[idade];
$musica = $res_b[musica]; 
?>
<table>
<tr><td>ID</td>    <td><input value="<?=$id?>"></td></tr>
<tr><td>NOME</td>  <td><input value="<?=$nome?>"></td></tr>
<tr><td>IDADE</td> <td><input value="<?=$idade?>"></td></tr>
<tr><td>MUSICA</td><td><input value="<?=$musica?>"></td></tr>
</table>
<? exit; } ?>

<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.2.js"></script>
<script>
function rest(rst) {
    $.post('temp.php',{rs_id: rst},function(data){
        var obj = jQuery.parseJSON( data );
        $("#id").val(obj.a);
        $("#nome").val(obj.b);
        $("#idade").val(obj.c);
        $("#musica").val(obj.d);
    });
}
</script>
</head>
<body>
<table width="400" border="1">
    <tr>
        <td width="100" colspan="2">
        <h3>Tabela Com IFRAME</h3>
        </td></tr>
        <tr><td>
    <table>
    <? $result = $mysqli->query("select id, nome from temp"); while($row = $result->fetch_assoc()) { ?>
        <tr>
            <td><a href="?id=<?=$row[id]?>" target="frame"> <?=$row[id]?> </a></td>
            <td><a href="?id=<?=$row[id]?>" target="frame"> <?=$row[nome]?> </a></td>
        </tr>
    <? } ?>
        </table>
    </td>
    <td>
        <iframe name="frame" frameBorder="0">
            <table>
                <tr><td>ID</td>    <td><input value="<?=$id?>"></td></tr>
                <tr><td>NOME</td>  <td><input value="<?=$nome?>"></td></tr>
                <tr><td>IDADE</td> <td><input value="<?=$idade?>"></td></tr>
                <tr><td>MUSICA</td><td><input value="<?=$musica?>"></td></tr>
            </table>
        </iframe>
    </td>
</tr>
</table>
<br>
<hr>
<br>
<table width="400" border="1">
    <tr>
        <td width="100" colspan="2">
            <h3>TABELA COM JQUERY</h3>
        </td>
    </tr>
    <tr>
        <td>
            <table><? $result = $mysqli->query("select id, nome from temp"); while($row = $result->fetch_assoc()) { ?> 
                <tr>
                    <td><button onclick="javascript:rest(<?=$row[id]?>)" value="<?=$row[id]?>"> ID <?=$row[id]?></button></td>
                    <td><?=$row[id]?> <?=$row[nome]?></td>
                </tr><? } ?> 
            </table>
        </td>
        <td>
            <table>
                <tr><td>ID</td>    <td><input id="id"></td></tr>
                <tr><td>NOME</td>  <td><input id="nome"></td></tr>
                <tr><td>IDADE</td> <td><input id="idade"></td></tr>
                <tr><td>MUSICA</td><td><input id="musica"></td></tr>
            </table>
        </td>
    </tr>
</table>

I picked it up piece by piece, around and around…

Dynamically populate input.

Thanks Pablo.

Scroll to Top
AllEscort