I want to open the image upload file dialogue box if i click the button tag.is it possible.if so how can i do it in php
while{
echo "<td><button><img src='".$cfet['productimage']."' width='50' height='40'></button></td>";
}
This question is related to
javascript
php
html
<label for="profileImage">
<a style="cursor: pointer;"><em class="fa fa-upload"></em> Change Profile
Image</a></label>
<input type="file" name="profileImage" id="profileImage" style="display: none;">
you can show the file selection dialog with a onclick function, and if a file is choosen (onchange event) then send the form to upload the file
<form id='foto' method='post' action='upload' method="POST" enctype="multipart/form-data" >
<div style="height:0px;overflow:hidden">
<input type="file" id="fileInput" name="fileInput" onchange="this.form.submit()"/>
</div>
<i class='fa fa-camera' onclick="fileInput.click();"></i>
</form>
Also, You can write all inline, direct at html code:
<input type="file" id="imgupload">
<a href="#" onclick="$('#imgupload').trigger('click'); return false;">Upload file</a>
return false; - will be useful to decline anchor action after link was clicked.
HTML Code:
<form method="post" action="#" id="#">
<div class="form-group files color">
<input type="file" class="form-control" multiple="">
</div>
CSS:
.files input {
outline: 2px dashed #92b0b3;
outline-offset: -10px;
-webkit-transition: outline-offset .15s ease-in-out, background-color .15s linear;
transition: outline-offset .15s ease-in-out, background-color .15s linear;
padding: 120px 0px 85px 35%;
text-align: center !important;
margin: 0;
width: 100% !important;
height: 400px;
}
.files input:focus{
outline: 2px dashed #92b0b3;
outline-offset: -10px;
-webkit-transition: outline-offset .15s ease-in-out, background-color .15s linear;
transition: outline-offset .15s ease-in-out, background-color .15s linear;
border:1px solid #92b0b3;
}
.files{ position:relative}
.files:after { pointer-events: none;
position: absolute;
top: 60px;
left: 0;
width: 50px;
right: 0;
height: 400px;
content: "";
background-image: url('../../images/');
display: block;
margin: 0 auto;
background-size: 100%;
background-repeat: no-repeat;
}
.color input{ background-color:#f1f1f1;}
.files:before {
position: absolute;
bottom: 10px;
left: 0; pointer-events: none;
width: 100%;
right: 0;
height: 400px;
display: block;
margin: 0 auto;
color: #2ea591;
font-weight: 600;
text-transform: capitalize;
text-align: center;
}
<!-- File input (hidden) -->
<input type="file" id="file1" style="display:none"/>
<!-- Trigger button -->
<a href="javascript:void(0)" onClick="openSelect('#file1')">
<script type="text/javascript">
function openSelect(file)
{
$(file).trigger('click');
}
</script>
<input type="file" id="imgupload" style="display:none"/>
<label for='imgupload'> <button id="OpenImgUpload">Image Upload</button></label>
On click of for= attribute will automatically focus on "file input" and upload dialog box will open
<button id="OpenImgUpload" onclick="$('#imgupload').trigger('click');">Image Upload</button>
<input type="file" id="imgupload" style="display:none"/>
you need to add a little hack to achieve this.
You can hide a file upload(input type=file
) behind your button
.
and onclick of your button you can trigger your file upload click.
It will open a file upload window on click of button
<button id="btnfile">
<img src='".$cfet['productimage']."' width='50' height='40'>
</button>
<div class="wrapper"> //set wrapper `display:hidden`
<input type="file" id="uploadfile" />
</div>
and some javascript
$("#btnfile").click(function () {
$("#uploadfile").click();
});
here is a fiddle for this example: http://jsfiddle.net/ravi441988/QmyHV/1/embedded/result/
Source: Stackoverflow.com