Write value of a combobox when uploading files?

0

Good afternoon. I started this week in a PHP development company. I need to include a combobox on a page that uploads files. The upload already works and has been used.

** I would like help to save the selected value in the combobox in the same table of the files (which the client chose to upload). This combobox shows the "File Category" and will serve to classify the client's images / documents.

files.php // File containing the combobox and also the button to upload the files.

<div class="form-group"> Categoria do Arquivo
        <form name="formCatArq" id="formCatArq" method="post" >
          <select class="form-control" style="width:40%" name="arqcat_codigo" id="arqcat_codigo"  >
            <option value="-1" selected="selected">Selecione a Categoria do Arquivo</option>
            <?php										  
 //CARREGA LISTA DE CATEGORIAS										 
$sql_gru = "select * from arquivos_categorias $sql_cat where arqcat_ativo ='S'  order by arqcat_titulo asc ";
$qry_gru = mysql_query($sql_gru);						 
if (mysql_num_rows($qry_gru) > 0) {
	while ($row = mysql_fetch_array($qry_gru)) {?>
            <option value="<?php echo $row['arqcat_codigo']?>" <? if ($marqcat_codigo == $row['arqcat_codigo']) {echo 'selected="selected"'; } ?>> <?php echo $row['arqcat_titulo']; ?> </option>
            <?php }
}?>
          </select>
          <label>
            <input type="submit" name="btnEnviar" id="btnEnviar" value="Filtrar sua busca:"  />
          </label>
        </form>
      </div>
     
// Botão para realizar o "Upload"
      <div id="mulitplefileuploader_arquivos">Upload</div>

Index.php // contains script to get the value of the combobox and in the sequence calls the function "send_value" in an attempt to load the value of the combobox together with the upload function. I'm trying to send the variable "$ arqcat" along with the "url:" parameter of the "settings_arq" variable, but this value does not arrive on the "upload_arquivos.php" page that inserts into the database.

<script>
$(function() {
    $('#arqcat_codigo').change(function(){
       var tip = $("#arqcat_codigo").val();
       if (($('#arqcat_codigo').val() == '-1') || (tip == '-1')) 
       {
            alert('Selecione a Categoria do Arquivo');
         
        }
        else {
		  envia_valor(tip);
			}
    });
});
</script> 

<script>	
function envia_valor(tip) {
	 $arqcat = tip;
	 $(document).ready(function(){
	 // alert($arqcat);
var settings_arq = {
			url: "upload_arquivos.php?cod=<?=$codigo?>&usucatcod=<?=$usucat?>&arqcatcodigo=<?=$arqcat?>",
					method: "POST",
					fileName: "myfile",
					multiple: true,
					showProgress: true,
					showStatusAfterSuccess: true,
					dragDropStr: "<span><b>Selecione os arquivos para upload23332</b></span>",
					onSuccess:function(files,data,xhr)
					{
						$("#status").html("<font color='green'>Upload realizado com sucesso</font>");
						location.reload();
					},
					onError: function(files,status,errMsg)
					{		
						$("#status").html("<font color='red'>Falha no upload</font>");
					}
				}
				$("#mulitplefileuploader_arquivos").uploadFile(settings_arq);
});
};
</script> 

upload_files.php // receives the parameters and generates the sql.

<?php
if(isset($_FILES["myfile"]))
{
	$ret = array();
	$error =$_FILES["myfile"]["error"];
   {
    
    	if(!is_array($_FILES["myfile"]['name'])) //single file
    	{
       	 	
			$arquivo = $_FILES["myfile"];
			$nome_temp = $arquivo["tmp_name"];
			$nome_arquivo =  $cod . "-" . $arquivo["name"];
			$nome_arquivo = str_replace(' ', '', $nome_arquivo);
			move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $nome_arquivo);
       	 	      	$ret[$arquivo]= $output_dir.$arquivo;
			$sql = "insert into arquivos (usu_codigo, arqcat_codigo, usucat_codigo, arq_legenda, arq_arquivo, arq_data, arq_imagem)  values($cod, '$arqcatcodigo', '$usucatcod', '$arquivo[name]','$nome_arquivo',curdate(),'N')";
			$post = mysql_query($sql);
			$codigo = mysql_insert_id();
			$ext = strxchr($nome_arquivo, ".", 1, 1);
			$ext = substr(strtolower($ext[1]),1);
				if ($ext=='jpg' || $ext=='jpeg' || $ext=='png') {
					GerarImagens($nome_arquivo, 200, 'thumb_');
					GerarImagens($nome_arquivo, 227, 'gal_');
					$sql2 = "update arquivos set arq_imagem='S' where arq_codigo=$codigo";
					$qry2 = mysql_query($sql2);
				}
			
    	}
    	
    }  
    echo json_encode($ret); 
}
?>
    
asked by anonymous 07.07.2016 / 22:27

1 answer

0

@Allan I do this, then you adapt to your code.

form.php

<form action="insere.php" method="POST" enctype="multipart/form-data">
<select name="cat">
<?php
//conexão
include 'conect.php';
//seleciona as categorias
$sql = "select nome_carac from carac where nome_carac is not null";
$exec = $con->query( $sql ) or exit( $con->error );                      
$i = 1;
while ( $f = $exec->fetch_object() )
{
$cats[$i]['nome_carac'] = $f->nome_carac;
$i++;
}
//gera a lista de opcoes
foreach ($cats as $key => $value) {
echo "<option value='{$value['nome_carac']}' > {$value['nome_carac']}</option></br>";
}
$con->close();
?>
</select></br>

<input type="file" name="files[]" multiple/>

<input type="submit"/>

</form>

insere.php

include 'conect.php';
// muda o banco "teste"
mysqli_select_db($con,"test_stack");
//trata imagem
if(isset($_FILES['files'])){
$errors= array();
//pega as informacoes das imagens
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['files']['name'][$key];
    $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];  
    if($file_size > 2097152){
        $errors[]='Tamanho máximo do arquivo : 2 MB';
    }

$select_opt =  $_POST['cat'];

$sql="INSERT INTO _stack (nome_img, nome_cat) VALUES ('$file_name','$select_opt ')";

    $desired_dir="user_data";
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir("$desired_dir", 0700);// Cria o diretorio para imagens
        }
        if(is_dir("$desired_dir/".$file_name)==false){
            move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
        }else{                                  //Renomeia se existe
            $new_dir="$desired_dir/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }
     mysqli_query($con, $sql);          
    }else{
            print_r($errors);
    }
}
if(empty($error)){
    echo "Success";
}
}
$con->close();

Notice that I'm inserting only the name of the images and the category that comes from the select. See if it helps anything, it says the agent adjusts.

    
13.07.2016 / 15:39