auto-fill input with jquery php

Asked

Viewed 1,288 times

3

I would like to convert this code into jquery.

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

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

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

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

The result page.php:

<?
$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>
  • What gives console.log(result);? That page "resultado.php" is the same as in the question? in which case I don’t see where you’re sending back to ajax/post the data you want.

  • Pablo, Thanks for commenting. log(result), is the default return..

  • I don’t really know how to explain it. But what I would like is that,, instead of using this IFRAME, it gave me a return via jquery (which I still don’t quite understand JQUERY,, and it was recently that I learned this $.post/$.get function). Pablo,, look at an example of what I would like,, only without iframe >> . www.diegosti.Hol.es/temp.php . . , but,, I don’t know how to do exactly this, in jquery Jquery, takes the ID, and inserts the SQL into the next input..

1 answer

1

I got.

<?
$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 was picking it up piece by piece, around and around..

Dynamically populate input.

Valeu Pablo.

Browser other questions tagged

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