Return URL that is fixed

Asked

Viewed 44 times

1

I cannot return my page because it does not traffic data on URL.

I have this following Form:

<form name="formulario_arquivo" id="formulario" method="POST" action="upload_foto/proc_upload.php" enctype="multipart/form-data">
    <table class="formulario">
        <tbody> 
            <tr class="cabecalho">
                <td>
                    <label for="arquivo">Arquivo </label>
                </td>
                <td>
                    <input name="arquivo" id="nome_arquivo" type="file" class="elemento_a_esquerda">
                </td>
            </tr>
        </tbody>
        <tr class="rodape">
            <td></td>
            <td>
                <button type="submit" value="Cadastrar" class="elemento_a_direita">
                    <img src="mgm/importar.png" alt="Importar">
                </button>
            </td>
        </tr>
    </table>
</form> 

That is sending data to save the imported file to another page, but need to return from page proc_upload.php to my current page.

if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $_UP['pasta']. $nome_final)){
    //Upload efetuado com sucesso, exibe a mensagem
    $query = mysqli_query($conn, "INSERT INTO arquivos (
    codigo,arquivo,data_) VALUES(NULL,'$nome_final',NOW())");
    echo "
        <META HTTP-EQUIV=REFRESH CONTENT = '0;URL=http://localhost/site_opus/public_html/sistema/_sistema.php'>
        <script type=\"text/javascript\">
            alert(\"Imagem cadastrada com Sucesso.\");
        </script>
    ";  
}

This is the page that returns the data, however mine URL is fixed.

  • In this case you would have to use javascript, to upload the image without Reload or change the url of your page.

  • Cara vc could give me an idea of how I’ve never done information traffic with javascript just by php.

  • I’m using this function here: Function loads_data(id_called){ //-------> Was working $.ajax({ type: 'POST', url: 'scriptPHP3.php', date: 'id=' + id_called, Success: Function(data){ $('#test').html(data); } }); } but I don’t know how to adapt it to my site

2 answers

0

You can try on your Alert, put it this way?

window.onload = function(){alert('Imagem cadastrada com Sucesso.');}

You may be loading the page but you are not running . js.

0

One way to do this using javascript would be like this

Your HTML

<input type="file" name="inp_img" id="inp_img" accept="image/*" onchange="javascript:upload_logo();">

Your javascript

function upload_logo() {
  var file_data = $('#inp_img').prop('files')[0];
  var form_data = new FormData();
  form_data.append('file', file_data);
  $.ajax({
    url: 'upload_img.php',
    cache: false,
    contentType: false,
    processData: false,
    data: form_data,
    type: 'post',
    success: function(data){
      if(data == 'erro'){
        console.log("Não foi possivel enviar seu arquivo!");
      }else if(data == 'formato_invalido'){
        console.log("Formato Inválido, sua imagem deve está no formato 'jpeg / jpg / png / bmp'");
      }else if(data == 'maior') {
        console.log("Sua imagem está muito grande, o tamanho máximo é 500Kbs");
      }else{
        console.log('sucesso');
      }
    }
  });
}

In your php, upload_img.php

<?php
  $arquivo_nome = $_FILES['file']['name'];
  $arquivo_temporario = $_FILES['file']['tmp_name'];

  $extensao = end(explode('.', $arquivo_nome));
  $novo_nome_arquivo = uniqid().'.'.$extensao;
  $valid_formats = array("jpg", "png", "bmp","jpeg");

  if ($arquivo_nome != '' && $arquivo_temporario != '') {
    if (in_array($extensao,$valid_formats)) {
      $size = $_FILES['file']['size'];
      if ($size <= 512000) {
        if(move_uploaded_file($arquivo_temporario, '../../imagens/logo/'.$novo_nome_arquivo)):
          echo "sucesso";
        else:
          echo "erro";
        endif;
      }else{
        echo "maior";
      }

    }else {
      echo "formato_invalido";
    }
  }else{
    echo "vazio";
  }
?>

Remembering that you should import your js into your HTML page, something like

<script src="meu_upload.js"></script>

  • All right, I’ll implement

  • Thank you very much worked. Agr only got ajax_txt error 0 Access denied from my page but took the other vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.