automatic fill in input with jquery php

3

I would like to convert this code to jquery.

To say. Instead of appearing in the iframe, I would like it to be a single page normally, and the insertion would be 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)) ,

Only here returns only a single response 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>
    
asked by anonymous 25.03.2016 / 05:02

1 answer

1

I GOT IT.

<?
$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>functionrest(rst){$.post('temp.php',{rs_id:rst},function(data){varobj=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've been taking it from piece to piece, here and there.

Dynamically fill input.

Thanks Paul.

    
25.03.2016 / 16:52