Pass value from mysql to input

1

Hello good afternoon I need to pass value from mysql to input (text box), I'm doing it in the following way but it throws me the following error:

    $conexion=mysqli_connect($nombre_host,$nombre_usuario,$contrasena_usuario,$nombre_bd);

$consulta="SELECT * FROM clientes WHERE cedula='08522002'";
$resultados=mysqli_query($conexion,$consulta);
$fila=mysqli_fetch_row($resultados);

echo "<script type='text/javascript'>
document.getElementById('nombre_cliente').value =" . $fila[1] .  "</script>";
}

    
asked by Javier 04.08.2018 в 20:07
source

2 answers

1

Apart from the fact that the code seems to be badly organized haha, the error that is being thrown is because it is interpreted as follows:

document.getElementById('nombre_cliente').value = Brayan</script>";

Where Javascript is waiting for Brayan to be the name of a variable and not a string.

Try to do the following:

echo "<script type='text/javascript'>";
echo 'document.getElementById("nombre_cliente").value ='.'"'.$fila[1].'"'.'</script>';

It should work that way. Greetings.

    
answered by 04.08.2018 / 20:43
source
0

It's because when you put $ row [1] you're not putting it in quotes and that makes JavaScript interpret it as a variable. And as that bariable Brayan is not defined it throws the error.

You should change this:

 echo "<script type='text/javascript'>document.getElementById('nombre_cliente').value =" . $fila[1] .  "</script>";

for something like:

 echo "<script type='text/javascript'>document.getElementById('nombre_cliente').value ='" . $fila[1] .  "'</script>";

or

 echo "<script type='text/javascript'>document.getElementById('nombre_cliente').value ="."'" . $fila[1] ."'".  "</script>";

I have not tried the code so there may be errors, but the error is that.

    
answered by 04.08.2018 в 20:50