Pass variable value to input in another PHP file

Asked

Viewed 1,071 times

0

Good morning!

I have the following situation:

A customer needs to fill out a form, and one of the fields on this form is for sending images. To adapt to the customer, the image upload window opens in a modal, in a different file. Client chooses image, upload is done and file goes as md5.

So far, all in peace.

Well, the client is a very basic user, even for a "Ctrl C" and "Ctrl V", so I need to pass the name of the generated file (image) to the form before the modal, in the input.

I’ll try to demonstrate from images:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

On the same page (modal) of the upload, I already gave a echo and I get the file name normally. The problem is that I am not getting to the previous form.

Could I explain? It’s simple, but I’m finding it difficult.

Thank you in advance!

  • Have you tried putting it in an input and via jQuery or Javascript grab the value and pass the previous "screen" input? It’s quite simple to be done.

  • Good morning, thank you for the prompt reply. I didn’t do it, I don’t know how to do it. If you can give me a reference to read it, thank you!

  • Jquery is not my strong suit, so I need the reference.

  • Okay, but I have a question. This second page is a frame or are you getting PHP echo via AJAX?

  • It’s a frame, a new php file, an image upload.

  • Yeah, then it got a little bit tricky kkk. I can still do it, but I need to think. I’ll try to figure something out here.

  • In the upload modal, had already put a echo to see the name of the generated file, and had placed inside an input. I just don’t know how to pass to the previous form. In PHP, I don’t know!

  • 2

    Yes, with pure PHP you won’t be able to. You have to use Javascript. I’ll be right back with the answer.

Show 3 more comments

1 answer

1

Come on.

Include jQuery in your project (you can do it with pure Javascript, just search the internet).

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>

Include the code below, it will call via AJAX the page responsible for sending the images when any of the buttons is clicked.

<script>
    function chamarUpload(btnId) {
        $.ajax({
            url: 'upload.php', //Altere pelo arquivo de upload
            type: 'GET',
            data: 'btnId=' + btnId,
            success: function(data) {
                $('#divLightbox').html(data); //Altere para o ID do modal

                //Faça com que seu modal seja exibido
            },
            beforeSend: function() {
                //Se quiser, coloque uma animação, tipo loading...
            }
        });
    }
</script>

Change the url value to the path of your file where you have the upload form, and within the Function Success, change the divLightbox pro ID of your "modal".

PS: If by clicking the button the page is updated, add return false; at the end of the above Function (before the last key).

PS²: If you notice the code, we are making a request GET, and we’re passing the parameter btnId with the btnId value we received when calling Function. This value must refer to your ID input (each input must have a different ID).

Your upload page must have a code similar to this

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        //Faça tudo que for necessário pro upload da imagem
        //e conversão para MD5

        //Não esqueça de dar o echo.
        //O echo deve ser limpo, para exibir apenas o MD5, nada mais.

        //Não remova o exit.
        exit;
    }
?>
<div id="content-upload">
    <p>Somente arquivos JPEG, PNG e JPG são permitidos. O tamanho da imagem precisa ser menor que 5000KB</p>
    <hr>
    <form aciton="" method="post" id="form-upload">
        <input type="file" id="imagem" name="imagem">
        <button type="submit" id="btn-send">Enviar imagem</button>
    </form>
</div>
<script>
    $(function() {
        $("#btn-send").click(function() {
            $.ajax({
                url: 'upload.php', //chame essa mesma página
                type: 'POST',
                data: $('#form-upload').serialize(),
                success: function(data) {
                    $("#<?= $_GET['btnId']; ?>").val(data);

                    //Nesse momento, o MD5 da imagem já deve ter sido atribuido
                    //ao campo, então feche o modal.
                },
                beforeSend: function() {
                    //Se quiser defina um loading pro upload
                }
            });

            return false;
        });
    });
</script>

Finally, to call the upload page and make it be included in the div of your modal, use the href of links as: javascript:chamarUpload(id do input que vai receber o md5);

Thus remaining

<a href="javascript:chamarUpload('input1');" title="Fazer upload">Upload</a>

Or, in other elements, use onclick, so:

<img src="..." onclick="chamarUpload('input1');">
  • 1

    Okay, buddy. I’ll shoot you right here and I’ll get back to you if I have any trouble. Thanks for your time!

  • I cannot load the content, the modal opens blank... where I must post the code to show?

  • It gives an element inspect (maybe just inspect) in the browser, and goes on console and sees if there are no errors there. I think you can take a print and post the link here, or edit your question and add in it msm.

  • Uncaught Referenceerror: chamarUpload is not defined(Anonymous Function) @ jquery-Latest.min.js:3 (program):1 Uncaught Referenceerror: chamarUpload is not defined(Anonymous Function) @ jquery-Latest.min.js:3

  • Thanks, error found... rs

  • I don’t know how to solve... some hint?

  • He’s saying that the function called Pload was not set. Did you change the function name? Did you type correctly on the buttons? Did you put Pload and jQuery on the same page? jQuery was?

  • On the page I speak, it is on the page where the fields are.

  • http://imagizer.imageshack.us/a/img922/5711/UjLSPA.jpg

  • Where is jQuery? Your modal does not use an ID, so in Function, in sucess, exchange #venobox_custom for . venobox_custom or add an id called venobox_custom to the modal. In the parameter you are passing to call the function calledUpload, you have defined btnId, so I saw that Code would be bancoimg1 which is the input ID. Fix this, add jQuery inside the site’s tag head and test again.

  • Buddy, take a look at the Modal code. It looks so simple, but I don’t see the error! http://imageshack.com/a/img921/9632/nzkzDB.jpg

  • Unfortunately, I had to go away for a few days, family problems... but anyway... I’m taking the venobox away from here and trying to create a modal window with the bootstrap itself, I think this venobox that is giving problems here... as soon as I have news, I return to comment.

Show 7 more comments

Browser other questions tagged

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