Make Submit text and image at the same time

0

Good night everyone, I have the following question, can someone help me do this (?):

Make submit image and text at the same time when doing submit . My idea was to type="file" to make submit of an image be in id="Cover" .

And by giving submit or even before the file goes to a folder selected by me in the code and at the same time the file name is in the database.

Can someone put me in the code? I do not know how to do it, it seems very complete, but I really need it: ss

<?
require('cdn/inc/header.php'); 

if(isset($_SESSION['user_data'])):
$user_level = $_SESSION['user_data']['level'];


switch($user_level):
case 1:
case 2: 
case 3: 
case 4:
case 5:
case 6:

if ( empty( $_POST)) {
?>


 <div id="user-bar" style="background: #007E99;">
  <i>
   <a href="list-films.php">Editar filmes</a>
  </i>
 </div>

 <br />
 <br />

<form method="post" action=""> 
 <div id="films-add-bars">
  <label for="Title">Titulo do filme</label>
  <input class="films-input" id="Title" type="text" name="Title" required />
 </div>

 <div id="films-add-bars">
  <label for="Cover">Capa</label>
  <input class="films-input" id="Cover" type="text" name="Cover" required />
 </div> 

 <div id="films-add-bars">
  <label for="Duration">Duração do filme</label>
  <input class="films-input" id="Duration" type="text" name="Duration"  pattern = "[0-9:]*" title="Só numeros e ' : ' " maxlength="8" required />
 </div>

 <div id="films-add-bars">
  <label for="Year">Ano de lançamento</label>
  <input class="films-input" id="Year" type="text" maxlength="4" name="Year" required />
 </div> 

 <div id="films-add-bars">
  <label for="Trailer">Trailer (youtube.com)</label>
  <input class="films-input" id="Trailer" type="text" name="Trailer" readonly/> 
 </div>

 <div id="films-add-bars">
  <label for="Embed">Código/Embed</label>
  <textarea class="films-input" id="Embed" type="text" name="Embed"></textarea> 
 </div>

 <br />

 <input type="submit" class="films-insert-btn" name="Submit" value="Salvar alterações" />
</form>

<?
 }  
 else {
 $Author = $_SESSION[ 'username' ];

 $Title = $_POST['Title'];
 $Cover = $_POST['Cover'];
 $Duration = $_POST['Duration'];
 $Year = $_POST['Year'];
 $Trailer = $_POST['Trailer'];
 $Embed = $_POST['Embed'];

 $LastEditUser = $_SESSION[ 'username' ];
 $LastEditDate = date("d-m-Y"); 
 $LastEditHour = date("H:i:s"); 

 $sql = "INSERT INTO films (Author, Title, Cover, Duration, Year, Trailer, Embed, LastEditUser, LastEditDate, LastEditHour) VALUES ( :Author,  :Title, :Cover, :Duration, :Year, :Trailer, :Embed, :LastEditUser, :LastEditDate, :LastEditHour)";

 $query = $db->prepare( $sql );
 $query->execute( array(':Author' => $Author, ':Title' => $Title, ":Cover" => $Cover, ':Duration' => $Duration, ':Year' => $Year, ':Trailer' => $Trailer , ':Embed' => $Embed, ':LastEditUser' => $LastEditUser, ':LastEditDate' => $LastEditDate, 'LastEditHour' => $LastEditHour) );
 header ('Location: edit-films.php'); 
 }
?>

 <br />
 <br />

 <center>

 </center>


<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</body>
</html>

<?
 endswitch;
 else:
 header( 'Location: ../ ');
 endif;
?>
    
asked by anonymous 26.04.2015 / 00:47

1 answer

1

It's simple. Change the header of your form to

<form method="post" action="" enctype="multipart/form-data">

Add the input that will upload the image:

<input id="Cover" type="file" name="Cover">

After the submit, it is necessary to treat the form submission in PHP. Add this along to the part of the code that POST handles:

//verifica se houve o input "imagem" não veio vazio 
if($_FILES['Cover']['size'] > 0){
    move_uploaded_file($_FILES['Cover']['tmp_name'], $caminhoDeDestinoDaImagem);
}

This is a simple way to do so, it is important to make further validations to address any errors about type or filename, among others.

    
26.04.2015 / 02:42