My interaction between PHP and JavaScript is not working . What can i do to solve this?

Viewed 31

I am echoing in the screen the (id,name,city) from MySQL.

But the problem and the challenge is inserting the values from MySQl into some inputs.

I am using the code:

verificar_id($pdo);

function verificar_id($pdo){
if(isset($_POST['verificar']) && isset($_POST['id_cliente'])){
    session_start();

    $id_cliente = $_POST['id_cliente'];

    $sql= $pdo->prepare("SELECT * FROM `clientes` where `id`=?");
    $sql->execute(array($id_cliente));

    $info = $sql->fetchAll(PDO::FETCH_ASSOC);

    echo "<pre>";
    print_r($info);
    echo "</pre>";

    foreach ($info as $key => $value) {
        $_SESSION['id'] = $value['id'];
        $_SESSION['nome'] = $value['nome'];
        $_SESSION['cidade'] = $value['cidade'];
    }
}
}


?>

 <script type='text/javascript'>
 var nome = document.getElementById('id').value = "<?php echo $_SESSION['id'] ?>"
 var nome = document.getElementById('nome').value = "<?php echo $_SESSION['nome'];?>"
 var nome = document.getElementById('cidade').value = "<?php echo $_SESSION['cidade'];? 
 >"
 </script>

And My HTML:

<body>
<form method="post" id="form">
    <input type="number" name="id_cliente">
    <input type="submit" name="verificar">
</form>
<input type="text" name="id" id="id">
<input type="text" name="nome" id="nome">
<input type="text" name="sobrenome" id="cidade">
</body>

And this is the result: enter image description here

How you can see the result is almost exactly what i wanted: The only problem in all this is the browser is not doing what should do. Of some how, the browser is understanding that this a Js code(and inside the code, it is appearing the same result tha the "print_r", but is not running and i don't know how to solve this.

Someone can help me to solve this problem? Or solve this or show another way to me get the result? Pls.

Maybe another way to insert the values into a input.

0 Answers
Related