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');">
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.
– Clayderson Ferreira
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!
– Hebert Richard
Jquery is not my strong suit, so I need the reference.
– Hebert Richard
Okay, but I have a question. This second page is a frame or are you getting PHP echo via AJAX?
– Clayderson Ferreira
It’s a frame, a new php file, an image upload.
– Hebert Richard
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.
– Clayderson Ferreira
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!– Hebert Richard
Yes, with pure PHP you won’t be able to. You have to use Javascript. I’ll be right back with the answer.
– Clayderson Ferreira