user1886859
user1886859

Reputation: 1

Passing inserted value in a form textbox to display it in the same page in PHP

I have a HTML form with 6 textboxes that the user needs to fill and before register those values I'd like that the user Preview-them in the same webpage. It's like a preview of the values inserted above. The thing is I don't know how to pass those values to another variable (or the same one) and preview them. HOpe somebody can help me. Here's my code so far.

        <script type="text/javascript">
        function validarForm(formulario) {
          if(formulario.filmid.value.length==0) { //comprueba que no esté vacío
            formulario.filmid.focus();   
            alert('No has escrito la clave'); 
            return false; //devolvemos el foco
          }
          if(formulario.nombre.value.length==0) { //comprueba que no esté vacío
            formulario.nombre.focus();
            alert('No has escrito el nombre');
            return false;
          }

          if(formulario.sinopsis.value.length==0) {  //comprueba que no esté vacío
            formulario.sinopsis.focus();
            alert('No has escrito ninguna consulta');
            return false;
          }
            if(formulario.genero.value.length==0) { //comprueba que no esté vacío
            formulario.genero.focus();   
            alert('No has escrito el genero'); 
            return false; //devolvemos el foco
          }
          if(formulario.anio.value.length==0) { //comprueba que no esté vacío
            formulario.anio.focus();
            alert('No has escrito el año');
            return false;
          }

          if(formulario.precio.value.length==0) {  //comprueba que no esté vacío
            formulario.precio.focus();
            alert('No has escrito ninguna consulta');
            return false;
          }

          return true;
        }
        </script>





        <html>
        <body>
        <?php include 'header.php' ?>

        <?php

        //variables generales


        if(isset($_POST['filmid']) && isset($_POST['nombre']) &&  isset($_POST['sinopsis']) && isset($_POST['genero']) && isset($_POST['anio']) && isset($_POST['precio'])){  




             $insertid=$_POST["filmid"];
             $insertnombre=$_POST["nombre"]; 
             $insertsinopsis=$_POST["sinopsis"]; 
             $insertgenero=$_POST["genero"]; 
             $insertanio=$_POST["anio"]; 
             $insertprecio=$_POST["precio"]; 


             $insercion=$insertid."|".$insertnombre."|".$insertsinopsis."|".$insertgenero."|".$insertanio."|".$insertprecio."|A\n";


             InsertaPelicula($insercion);

            }



        ?>
        Registrar nueva pelicula:
        <form action="registro.php" method="post" onsubmit="return validarForm(this);">
          <table border="1"><tr><td>Clave:</td><td><input type="text" name="filmid"></td></tr>
            <tr><td>Nombre:</td><td><input type="text" name="nombre"></td></tr>
            <tr><td>Sinopsis:</td><td><input type="text" name="sinopsis"></td></tr>
            <tr><td>Genero:</td><td><input type="text" name="genero"></td></tr>
            <tr><td>A&nacute;o</td><td><input type="text" name="anio"></td></tr>
            <tr><td>Precio</td><td><input type="text" name="precio"></td></tr>
            <tr><td><input type="submit" value="Registrar"></td><td></td></tr>
          </table>


        </form>
        <form>
        <input type="button" name="Cancel" value="Cancelar" onclick="window.location='index.php'">
        </form>
        <?php include 'footer.php' ?>
        </body>
        </html>

Upvotes: 0

Views: 758

Answers (1)

Karl Adler
Karl Adler

Reputation: 16786

Grep the value using e.g. onchange event and set it as content where you like it using e.g. innerHTML. Would be easier with jQuery or another js Framework.

Raw Javascript example:

//HTML:
    <input type="text" name="nombre" onchange="passValue(this, 'preview_nombre')"/>
    <span id="preview_nombre">before</span>
//Javascript:
    function passValue(e, target){
        document.getElementById(target).innerHTML = e.value;
    } 

http://jsfiddle.net/ya5Ty/

Upvotes: 1

Related Questions