Multipart form-data upload file - HTML PHP form

0

Doubts:

1) Is it possible to put a form inside another form? I explain. I have a form called "add" where I want to add another form called "upload_files" because I want that by pressing "Save", previously selecting the desired images, the data of the form "add" is sent.

2) It only lets me select an image, can you choose several to upload them at once? Could you upload several images?

Code:

<?php
    require_once "Clases/BD.php";
    require_once "Clases/Cabanas.php";
    require_once "conexion.php";

    //Iniciar una nueva sesión o reanudar la existente.
    session_start();
    //Si existe la sesión "administrador"..., la guardamos en una variable.
    if (isset($_SESSION['administrador'])){
        $administrador = $_SESSION['administrador'];
    }

    //Si pulsamos el botón "Guardar"... 
    //Realizamos el "INSERT" antes que el "SELECT" para que se actualice la tabla correctamente.
    if(isset($_POST["guardar"])){
        $nombre = $_POST["nombre"];
        $capacidad = $_POST["capacidad"];
        $descripcion = $_POST["descripcion"];
        $precio = $_POST["precio"];
        //Llamamos al método "anadirCabana" y le pasamos los parámetros del formulario.
        BD::anadirCabana($nombre, $capacidad, $descripcion, $precio);
    }
?>


<!DOCTYPE html>
<html lang="es">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Panel del administrador</title>
    </head>
    <body>
        <div id="sesion_administrador">
            <?php 
            if(isset($_SESSION['administrador'])){
                echo "Bienvenido ".$administrador."&nbsp;&nbsp;&nbsp;";
                echo "<a href='salir_administrador.php?salir=1'>Salir</a>"; //GET
                //_REQUEST = $_POST o $_GET
                if(isset($_REQUEST["salir"])){
                    unset($_SESSION["administrador"]);
                    header("Refresh:0; url=iniciar_sesion_administrador.php");
                }
            }
            ?>
        </div>

        <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="tabla_datos_cabana" id="tabla_datos_cabana" method="POST">
            <div id="mostrar_cabanas">
            <br/><br/><br/>
                <table class="table table-striped" name="tabla" width="600" border="2" cellspacing="3" cellpadding="3" style="font-size: 10pt">
                    <center>
                        <tr>
                            <thead style="background-color:#A9F5A9">
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>ID Cabaña</center></b></font>
                                </td>
                                <td width=\"250\">
                                    <font face="verdana" color="blue"><b><center>Nombre</center></b></font>
                                </td>
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>Modificar</center></b></font>
                                </td>
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>Eliminar</center></b></font>
                                </td>
                            </thead>
                        </tr>   
                        <?php
                        $datos = BD::obtenerCabanas();
                        foreach($datos as $cabana){
                            echo "<tr>";
                                echo "<td width=\"150\"><font face=\"verdana\"><font size='2'><center>".$cabana->getIdcabana()."</center></font></font></td>";
                                echo "<td width=\"250\"><font face=\"verdana\"><font size='2'><center>".$cabana->getNombre()."</center></font></font></td>";
                                echo "<td width=\"150\"><font face=\"verdana\"><font size='2'><center>"?> 
                                    <a href="modificar.php?idcabana=<?=$cabana->getIdcabana();?>">
                                        <img src="imagenes/modificar.png" height='24' width='26' onmouseover="this.src='imagenes/modificar_in.png';" onmouseout="this.src='imagenes/modificar.png';">
                                    </a> <?php "</center></font></font></td>";
                                echo "<td width=\"150\"><center><input type='checkbox' name='marcados[]' id='marcados[]' value=".$cabana->getIdcabana()."></center>";
                         echo "</tr>";
                        }
                        ?>
                    </center>
                </table>

                <!-- Botón NUEVA cabaña -->
                <div class="boton_anadir" class="table-responsive" align="left">
                    <font face="verdana">
                        <b><input type="button" style="width:200px; height:28px;" name="nueva_cabana" id="nueva_cabana" value="Añadir cabaña"></b>
                    </font><br/>
                </div>

                <!-- Botón ELIMINAR cabaña/s -->
                <div class="boton_eliminar" class="table-responsive" align="left">
                    <font face="verdana">
                        <b><input type="submit" style="width:200px; height:28px;" name="eliminar_cabanas" id="eliminar_cabanas" onclick="return confirm('¿Deseas realmente eliminar estas cabañas?');" value="Eliminar cabañas"></b>
                    </font><br/>
                </div>

                <?php
                //Si pulsamos el botón "Eliminar cabañas"...
                if(isset($_POST['eliminar_cabanas'])){
                    if(empty($_POST['marcados'])){
                        echo "<h4><center>No se ha seleccionado ninguna cabaña.</center></h4>";
                    }else{
                        foreach($_POST['marcados'] as $valor){
                            //Nos conectamos a la base de datos.
                            $conexion = mysqli_connect("localhost", "root", "root", "osmarrural");
                            //Realizamos la consulta.
                            $sql = sprintf("DELETE FROM cabanas WHERE idcabana='%d'", $valor);
                            $resultado = mysqli_query($conexion, $sql);
                            /*
                            $mysqli = new mysqli_connect("localhost", "root", "root", "osmarrural");
                            $stmt = $mysqli->prepare("DELETE FROM cabanas WHERE idcabana = ?");
                            $stmt->bind_param('i', $valor);
                            $stmt->execute(); 
                            $stmt->close();
                            */
                        }
                        echo "<meta http-equiv=\"refresh\" content=\"0; URL=panel_administrador.php\">";
                    }
                }
                ?>
            </div>
        </form>

        <br/><br/><br/>
        <!-- Añadir una cabaña -->
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="anadir" id="anadir" method="POST" onsubmit="return validar_formulario_cabana();">
            <label for="nombre">Nombre: </label>
                <input type="text" id="nombre" name="nombre" />
            <br/><br/>
            <label for="capacidad">Capacidad: </label>
                <?php
                echo "<select name='capacidad'>";
                for($i=1; $i<11; $i++){
                    if($i==1){
                        echo "<option value='$i' selected='selected'>$i</option>";
                    }else{
                        echo "<option value='$i'>$i</option>";
                    }
                }
                echo "</select>";
                ?>
            <br/><br/>
            <label for="descripcion">Descripción: </label>
                <input type="text" id="descripcion" name="descripcion">
            <br/><br/>
            <label for="precio">Precio: </label>
                <input type="text" id="precio" name="precio">
            <br/><br/>

            <input type="submit" value="Guardar" id="guardar" name="guardar"/>
            <input type="reset" value="Resetear" id="resetear" name="resetear"/>
        </form>
        <!-- ¿Es necesario añadir otro "formulario" para el botón o se puede realizar mediante el botón "Guardar" -->
        <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" name="subir_archivos" method="POST">
            <input name="uploadedfile" type="file"/>
            <input type="submit" value="Subir archivos"/>
        </form>
    </body>
</html>
    
asked by omaza1990 29.11.2017 в 23:20
source

1 answer

0
  

1) Can you put a form in another form? Me   I explain. I have a form called "add" where I want to add   another form called "upload_files" because I want the   Press "Save", previously selecting the desired images,   Send the data from the "add" form.

Of power you can. What you can not do is send the data of both forms at the same time doing a submit because a submit redirects the page and you can not redirect a page 2 times at the same time.

In order to send both data together they must be in the same form or sent separately using ajax.

  

2) Just let me select an image, can you choose several for   upload them once? Could you upload several images?

Use attribute attribute multiple in input[type=file] :

<input name="uploadedfile" type="file" multiple="multiple"/>
    
answered by 29.11.2017 в 23:33