Show content of a variable in html from php

3

I'm new to php and I'm trying to show in a textbox the contents of a php variable but I do not get it, all the code is in the same file.

<html>

Number of places, time

    

<div id="contenedor">

    <div id="logo" >
        <img  src="img/logo coche-reloj.png"/>
    </div>

    <form method="post"  >

        <label>Nombre:</label> <input id="nombre" type="text" name="nombre" value=""
        <br/>
        <label>Hora:</label>  <input id="hora" type="text" name="hora" value=""> 
        <br/>             
        <label>Nº de Plazas:</label> <input id="plaza" type="text" name="plaza">

    </form>

</div>

and the php code is right on top of this and I do not know how to display the variable.

   <?php
/*header( "refresh:20;url=aa.php" );*/
    define('DB_HOST', 'localhost');
define('DB_NAME', 'parkingchulo');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$ID = mysql_real_escape_string($_POST['Nombre1']);
$Password =mysql_real_escape_string($_POST['passw1']);
function SignIn()
{
session_start();   //Inicia sesion
if(!empty($_POST['Nombre1']))   //Mira si el nombre que obtienes del login tiene contenido o esta vacio
{


        $ID = $row['IDUsuario'];
        $hora = date('H:i:s');
        $array = range (11,30);
        $Estado = "OCUPADO";
        $directivo2 = '2';
        $queryduplicado = mysql_query("Select IDUsuario from plazas where IDUsuario = $ID");
        $numerorows = mysql_num_rows($queryduplicado);
        $y = '1' ;
    mysql_query("INSERT INTO horas (ID,Nombre,FECHA,HORA,Accion) VALUES ('$ID','$nombre','$hoy','$hora','Entrada Parking')");

    /* Asignacion de plaza falta meter que lo guarde en la bbdd con una query */




    if  ($row['Cargo'] == "directivo1" && $y > $numerorows) {
    $sql = "UPDATE 'plazas' SET 'OCUPACION'='OCUPADO', 'IDUsuario'='$ID' WHERE  'NUMPLAZA'='1'";
    echo $numerorows ;
    $resultado = mysql_query($sql); 
    echo "Has entrado en el parking a las " . date("h:i:sa")." " . $row['Nombre'];
    echo "Tu plaza es la num 1";
    header( "refresh:5;url=aa.php" );
         }
    elseif ($row['Cargo'] == "directivo2" && $y > $numerorows) {
    /* Esta es la query miguel que cambia a ocupado la plaza num 2 */$sql = "UPDATE 'plazas' SET 'OCUPACION'='OCUPADO' , 'IDUsuario'='$ID'  WHERE  'NUMPLAZA'='2'";
    $resultado = mysql_query($sql); 
    echo "Has entrado en el parking a las " . date("h:i:sa")." " . $row['Nombre'];
    echo "Tu plaza es la num 2";

    header( "refresh:5;url=aa.php" ); /*Esta funcion es la que se encarga de almacenar el valor "ocupado"*/
}            /* Lo de los minusvalidos esta bien */
        elseif ($row['Cargo'] == "minusvalido" && $y > $numerorows){
            $plazaminusvalido= mysql_query("SELECT NUMPLAZA FROM plazas WHERE OCUPACION LIKE 'Libre' AND NUMPLAZA BETWEEN 3 AND 7 ORDER BY NUMPLAZA, NUMPLAZA LIMIT 1 ");


            $plazam = mysql_result($plazaminusvalido,0);
            $sql = "UPDATE 'plazas' SET 'OCUPACION'='OCUPADO','IDUsuario'='$ID' WHERE  'NUMPLAZA'='$plazam'";
             $resultado = mysql_query($sql);
             header( "refresh:5;url=aa.php" );
             echo "Has entrado en el parking a las " . date("h:i:sa")." " . $row['Nombre'];
             echo "Tu numero de plaza es:$plazam ";
            }
            /*Esto esta bien */
            elseif ($row['Cargo'] == "empleado" && $y > $numerorows){
        $plazaempleado= mysql_query("SELECT NUMPLAZA FROM plazas WHERE OCUPACION LIKE 'Libre' AND NUMPLAZA BETWEEN 8 AND 20 ORDER BY NUMPLAZA, NUMPLAZA LIMIT 1 ");
                echo "Has entrado en el parking a las " . date("h:i:sa")." " . $row['Nombre'];

                $plazaemple = mysql_result($plazaempleado,0);
                $sql = "UPDATE 'plazas' SET 'OCUPACION'='OCUPADO', 'IDUsuario'='$ID' WHERE  'NUMPLAZA'='$plazaemple'";
                 $resultado = mysql_query($sql);
                 echo "Tu plaza es la numero $plazaemple";
                 header( "refresh:5;url=aa.php" );
            }
                else echo "Tienes una plaza ya, antes de entrar de nuevo sal                     
asked by Mariopenguin 09.06.2017 в 16:48
source

3 answers

5

To print and display php variables in your html code you can use echo

<div id="contenedor">

<div id="logo" >
    <img  src="img/logo coche-reloj.png"/>
</div>

<form method="post"  >

    <label>Nombre:</label> <input id="nombre" type="text" name="nombre" value="<?php echo $nombre;?>"
    <br/>
    <label>Hora:</label>  <input id="hora" type="text" name="hora" value="<?php echo $hora;?>"> 
    <br/>             
    <label>Nº de Plazas:</label> <input id="plaza" type="text" name="plaza" value="<?php echo $numero_plazas;?>">

</form>

    
answered by 09.06.2017 / 16:55
source
2

In PHP echo it shows one or more strings the content that has that variable.

echo is not a function (it is a language construction), so the use of parentheses with it is not required.

EXAMPLE:

<?php
echo "Hola mundo";

// Se pueden usar variables dentro de una sentencia echo
$foo = "foobar";
$bar = "barbaz";

echo "foo es $foo"; // foo es foobar

?>

And given your example it would be in the html in the following way:

<div id="contenedor">

<form method="post">

    <label>Nombre:</label> 
    <input id="nombre" type="text" name="nombre" value="<?php echo $nombre;?>"
    <br/>
    <label>Hora:</label>  
    <input id="hora" type="text" name="hora" value="<?php echo $hora;?>"> 
    <br/>             
    <label>Plazas:</label> 
    <input id="plaza" type="text" name="plaza" value="<?php echo $plaza;?>">

</form>
    
answered by 09.06.2017 в 17:09
1

If you want to show the value of a variable in an input, I recommend the following.

  • change the extrension of the html file to .php Example (index.html = > index.php)
  • in the input field you should put in the value with the php and closing tags, and make an echo of the variable to be displayed.
  • Unfortunately he does not let me show the example, but I hope to see it was helpful.

    with this you can put the value in the inputs.

    I hope it serves you.

        
    answered by 09.06.2017 в 16:54