I am using the jquery uploader from here.
This is my button:
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>إضافة صورة</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" type="file" accept="image/png" name="files[]">
</span>
<br>
<small>الحد الأقصى لحجم الملف هو 2.5 ميجابايت</small><br>
</td>
<td align="left" valign="middle">
<!-- The global progress bar -->
<div id="progress" class="progress" style="width: 300px">
<div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
and this is my jquery:
/*jslint unparam: true */
/*global window, $ */
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
var url = window.location.hostname === 'blueimp.github.io' ?
'//jquery-file-upload.appspot.com/' : 'jqueryupload/server/php/articlemedia/';
$('#fileupload').fileupload({
url: url,
dataType: 'json',
maxFileSize: 2500000,
done: function (e, data) {
$.each(data.result.files, function (index, file) {
// $('<p/>').text(file.name).appendTo('#files');
console.log("file", file.url) // see what this shows.
$("#imgLogo").attr("src", file.url);
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
window.location = '<?php echo $_SESSION["volow_domain_name"]; ?>memberarticlemedia?id=' + GetQueryStringValue('id');
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
I want to know how can I add a then pass it to the UploadHandler.php so that I can process it there (add it to a database record)?
Thanks,
Jassim
Related
The issue is I can't upload file to the folder but the file name is storing in DB. I am getting an image if I copy and paste an image to the folder but I can't able to upload while update or add an image.
I not good in jQuery. Also, this project using Jquery.file upload plugin which I really don't know.
product-edit.php
<div class="col-sm-6">
<table id="files" class="files">
<tr>
<td>
<div class="col-sm-12 text-center">
<img style="width: 300px; height: 200px;" src="<?= base_url() ?>userfiles/product/<?php echo $product['image'] . '?c=' . rand(1, 9999) ?>"/>
</div>
<div class="col-sm-12 my-2" > <?php echo $product['image'] ?> </div>
<div class="col-sm-12 my-2" >
<a class="btn-danger btn-sm px-1 " data-url="<?= base_url() ?>products/file_handling?op=delete&name=<?php echo $product['image'] ?>" class="aj_delete">
<i class="fa fa-trash mx-1 "></i>Delete Image</a>
</div>
</td>
</tr>
</table>
</div>
<div class="col-sm-6">
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span class="my-2">Select files...</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" type="file" name="files[]">
</span>
<div class="my-1">
<q>Allowed: gif, jpeg, png</q>
</div>
<!-- The global progress bar -->
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
</div>
</div>
jQuery
<script src="<?php echo assets_url('assets/myjs/jquery.ui.widget.js'); $invoice['tid'] = 0; ?>"></script>
<script src="<?php echo assets_url('assets/myjs/jquery.fileupload.js') ?>"></script>
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
var url = '<?php echo base_url() ?>products/file_handling?id=<?php echo $product['pid'] ?>';
$('#fileupload').fileupload({
url: url,
dataType: 'json',
formData: {'<?=$this->security->get_csrf_token_name()?>': crsf_hash},
done: function (e, data) {
var img = 'default.png';
$.each(data.result.files, function (index, file) {
$('#files').html('<tr><td><a data-url="<?php echo base_url() ?>products/file_handling?op=delete&name=' + file.name + '" class="aj_delete"><i class="btn-danger btn-sm icon-trash-a"></i> ' + file.name + ' </a><img style="max-height:200px;" src="<?php echo base_url() ?>userfiles/product/' + file.name + '"></td></tr>');
img = file.name;
});
$('#image').val(img);
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
Here the controller method for upload file to the folder
Products.php
public function file_handling()
{
if ($this->input->get('op')) {
$name = $this->input->get('name');
if ($this->products->meta_delete($name)) {
echo json_encode(array('status' => 'Success'));
}
} else {
$id = $this->input->get('id');
$this->load->library("Uploadhandler_generic", array(
'accept_file_types' => '/\.(gif|jpe?g|png)$/i', 'max_file_size' => 2048, 'upload_dir' => FCPATH . 'userfiles/product/', 'upload_url' => base_url() . 'userfiles/product/'
));
}
}
The problem is you are loading the upload library config but are not actually processing the upload.
After this, which loads the upload library wih your configuration:
$this->load->library("Uploadhandler_generic", array(
'accept_file_types' => '/\.(gif|jpe?g|png)$/i', 'max_file_size' => 2048, 'upload_dir' => FCPATH . 'userfiles/product/', 'upload_url' => base_url() . 'userfiles/product/'
));
You need to actually process the upload:
if (!$this->upload->do_upload('userfile'))
{
// do something if the upload processing fails. You can output the errors using $this->upload->display_errors()
}
else
{
// do something if the upload is successful
// upload data will be stored in $this->upload->data()
}
By doing this, Codeigniter will take the file from your server's tmp directory and do with it what you want (such as moving it to its destination). Change userfile for the name of the file field.
Please note that if you need to process multiple files at once you'll need to loop through all files (which means a little tweaking on the above code)
I'm using laravel5.5 Ajax paging. I am the HTML part assembled in the controller. When I clicked on page second, I jumped to this method. How to make the compiled HTML code continue to display on the page?
After clicking on the second page, a string of JSON data is returned. This is a problem. How can we make the data on the second page continue to display in the original page?
PHP
//Screen video, playback volume and key points.
public function accept(Request $ request){
$id=$_GET['id'];
$summer=$_GET['key'];
$name=DB::table('vd_category')->where('address',$summer)->value('name');
if($id=='1'){
$video=DB::table('vd_video_'.$summer)->orderBy('state','desc')->paginate(25);
}else if($id=='2'){
$video=DB::table('vd_video_'.$summer)->orderBy('thumbs','desc')-
>paginate(25);
}
$data='';
foreach($video as $list){
$data.='<div class="col-md-1-5 col-sm-4 col-xs-6" style="padding:0
5px;"><div class="movie-item">';
$data.="<a style='position:relative;display:block;' title='$list-
>video_name' target='_blank' href='details?id=$list->id&key =$summer'>";
$data.="<img alt='$list->video_name' title='$list->video_name'
src=\"uploads/$list->image_address\" height=\"230px\" width='100%'>";
$data.="<button class='bdtag'></button>";
$data.="</a>";
$data.="<div class=\"meta\"><div
style=\"width:100%;overflow:hidden;height:20px;\">";
$data.="<a href='/details?id='$list->id'&key='$summer'
target='_blank' title='$list->video_name' class=\"movie-name\">$list-
>video_name</a>";
$data.="<span class=\"otherinfo\"> 5.0分</span></div><div
class=\"otherinfo\">类型:$name</div></div></div></div>";
}
$datav['1']=$data;
$datav['2']="<div>"."{$video->appends(['id' =>
$id,'key'=>$summer,'name'=>'page'])->links()}"."</div>";
return json_encode($datav);
}
HTML
<div id="data">
#foreach($video as $list)
<div class="col-md-1-5 col-sm-4 col-xs-6" style="padding:0 5px;">
<div class="movie-item">
<a style="position:relative;display:block;" title="{{$list-
>video_name}}" target="_blank" href="/details?id={{$list->id}}&key=
{{$status}}">
<img alt="{{$list->video_name}}" title="{{$list-
>video_name}}" src="{{asset('uploads')}}/{{$list->image_address}}"
height="230"
width="100%">
<button class="bdtag"></button>
</a>
<div class="meta">
<div style="width:100%;overflow:hidden;height:20px;"><a
href="/details?id={{$list->id}}&key={{$status}}" target="_blank" title="
{{$list-
>video_name}}" class="movie-name">{{$list->video_name}}</a><span
class="otherinfo"> 5.0分</span></div>
<div class="otherinfo">类型:{{$namme}}</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
<div id="datav" style="background:#FFF;padding-left:15px;">
{{$video->appends(['id' => $page,'name'=>'page'])->links()}}
</div>
JavaScript
<select name="" required id="where_id" style="float: right;padding-
left:10px;border-left:8px;width: 90px;height: 30px;">
<option value="">Click screening</option>
<option value="1">Sort by play volume</option>
<option value="2">Ranking by points</option>
</select>
<input type="hidden" id="summer" value="{{$status}}">
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#where_id").change(function () {
var id=$("#where_id").val();
var summer=$("#summer").val();
$.ajax({
type:"get",
url:"accept",
dataType:'json',
data:{id:id,key:summer},
success:function (event){
$("#data").html(event[1]);
$("#datav").html(event[2]);
}
});
});
</script>
It has been solved.js has been modified.
That is, the parameters carried by the URL through the Ajax request to the
back-end again, and then the back-end returned JSON data inserted into the
specified location again, probably this is the solution, my English is not
very good, please forgive me.
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#where_id").change(function () {
fun();
});
function fun() {
var id=$("#where_id").val();
var summer=$("#summer").val();
$.ajax({
type:"get",
url:"accept",
dataType:'json',
data:{id:id,key:summer},
success:function (event){
$("#data").html(event[1]);
$("#datav").html(event[2]);
pagedata();
}
});
}
function pagedata(){
$(".page-item a").click(function (){
var href=this.href;
this.href="javascript:void(0);";
var id=getQueryVariable('page',href);
var key=getQueryVariable('key',href);
var page=getQueryVariable('page',href);
$.ajax({
type: "get",
url: "accept",
dataType: 'json',
data: {id: id, key: key,page:page},
success:function (event){
$("#data").html(event[1]);
$("#datav").html(event[2]);
pagedata();
}
})
})
}
// Intercept the parameter values carried by URL, such as page parameters
//and some custom parameters.
function getQueryVariable(name,url) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var data=url.slice(url.indexOf('?'));
var r =data.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
</script>
I'm using this plugin to upload files:
https://github.com/hashmode/cakephp-jquery-file-upload
The upload works, but the progress bar always completes before the file is uploaded. Prior to using this plugin, I attempted to use the blueimp uploader standalone. I had the same problem.
Any idea how to make the progress bar work properly?
I'm testing this via a proxy. I can see the proxy is still awaiting the response. Yet the progress bar is 100%.
This is the code I'm using in my view:
<?php echo $this->JqueryFileUpload->loadCss(); ?>
<?php echo $this->JqueryFileUpload->loadScripts(); ?>
<div class="row">
<div class="col-md-12">
<div class="card ">
<div class="header">
<h4 class="title">Import Session Data</h4>
<p class="category">category text</p>
</div>
<div class="content">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Select files...</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" type="file" name="files[]" multiple>
</span>
<br>
<br>
<!-- The global progress bar -->
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
<div id="files" class="files"></div>
</div>
<div class="footer">
<hr>
<div class="stats">
<i class="fa fa-history"></i> footer
</div>
</div>
</div>
</div>
</div>
<script>
/*jslint unparam: true */
/*global window, $ */
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
var url = '/sessions/import';
$('#fileupload').fileupload({
url: url,
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo('#files');
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
},
success: function(response) {
var json = $.parseJSON(response);
alert(json['message']);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>
In my controller I have:
public function import()
{
if ($this->request->is('post')) {
$options = array(
'max_file_size' => 2048000,
'max_number_of_files' => 10,
'access_control_allow_methods' => array(
'POST'
),
'accept_file_types' => '/\.(png|jpg)$/i',
'upload_dir' => '../uploads/workbench/', //WWW_ROOT . 'files' . DS . 'uploads' . DS,
'upload_url' => '/files/uploads/',
'print_response' => false
);
$result = $this->JqueryFileUpload->upload($options);
print_r($result);
die();
}
}
Solved. It was something to do with me using a proxy. When I removed the proxy it worked fine. No idea why that happens though.
Hi I have problem of sending my variable to another PHP page using ajax.
My problem is am getting the id of the element from main page. Its for edit operation.
Main page modal:
<a href='javascript:' data-id={$row['customer_id']} class='btn small bg-blue-alt tooltip-button modal-customeredit' data-placement='top' title='Edit'><i class='glyph-icon icon-edit' ></i>
</a>
it will open modal on same page:
<div class="hide" id="modal-projedit" title="Edit Project Info">
<div class="pad10A">
<h3>Edit Project Info</h3>
<p class="font-gray-dark"> Fides Admin uses colors & styles from both the default theme color schemes and the included core color helpers. </p>
<div class="divider mrg25B"></div>
<form id="project-edit" action="" class="col-md-12 center-margin" method="">
<div class="form-row">
<div class="form-label col-md-3">
<label for="name">
Project Name:
<span class="required">*</span>
</label>
</div>
<div class="form-input col-md-9">
<input id="project_name" name="project_name" placeholder="Name" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="divider"></div>
<div class="form-row">
<div class="form-input col-md-8 col-md-offset-3">
<a href="javascript:;" class="btn medium primary-bg radius-all-4" id="project-edit-valid" onclick="javascript:$('#project-edit').parsley( 'validate' );" title="Validate!">
<span class="button-content">
Update
</span>
</a>
</div>
</div>
</form>
</div>
</div>
SCRIPT:
$( ".modal-customeredit" ).click(function() {
var myGroupId = $(this).data('id');
alert( myGroupId);
$.ajax({
type: "POST",
url: "sample.php",
data: { id: myGroupId }, // data retrieve on server-side ($_POST['id'])
})
$( "#modal-customeredit" ).dialog({
modal: true,
minWidth: 700,
minHeight: 200,
dialogClass: "modal-dialog",
show: "fadeIn"
});
$('.ui-widget-overlay').addClass('bg-black opacity-60');
});
});
Try the following:
Change the ajax function,
var myGroupId = $(this).attr('data-id'); // data-id
data = {'id':myGroupId };
$.ajax({
type: "POST",
url: "sample.php",
data: data, // data retrieve on server-side ($_POST['id'])
success:function(response){
alert(response); //do the rest of operations.
}
});
You didn't mention what the alert says.
Though I guess it shows a Null value since the ajax request seems good.
My bet goes for your selector, try this :
var myGroupId = $(this).attr('data-id');
HTML (with quotes) :
<a href="javascript:void(0)" data-id="{$row['customer_id']}" class="...">
<i class='glyph-icon icon-edit' ></i>
</a>
jQuery (width data-id and callback):
$( ".modal-customeredit" ).click(function() {
var myGroupId = $(this).attr('data-id'); // data-id
$.ajax({
type: "POST",
url: "sample.php",
data: { id: myGroupId },
}).done(function(response) { // data returned by server
var d = $('<div id="dialog">'+response+'</div>').appendTo('body');
d.load(function(){
$( "#dialog").dialog({
modal: true,
minWidth: 700,
minHeight: 200,
dialogClass: "modal-dialog",
show: "fadeIn"
});
$('.ui-widget-overlay').addClass('bg-black opacity-60');
});
});
});
You will not get the value because you are using echo to your php code in data-id attribute.
This is what you need to do to your html
<a href="javascript:void(0)" data-id="<?php echo $row['customer_id']?>" class="...">
<i class='glyph-icon icon-edit' ></i>
</a>
and use #seenath's code to check if you get the value
I have jquery dialog and when i click over the button it open but only first time when i close it and click second time over the same button it not appears. Here is my code :
Script:
$(function () {
$("#dialogPicture").dialog({
autoOpen: false
});
$(".buttonClass").on("click", function () {
// get the div element with the id dialogClass contained at the same scope as button!
var id = ($(this).siblings(".dialogClass").attr("id"));
$("#" + id).dialog({
autoOpen: false
});
$("#" + id).dialog("open").css({
"font-size": "13px"
});
});
});
HTML :
<td>
<?=$row['NOMER']?><input id="btn2" class="buttonClass" type="button" value="ВИЖ" />
<div class="dialogClass" id="dialogPicture_<?=$row['NOMER'];?>" style="display:none;">
<table class="bilet">
<tr>
<h2>
<td colspan="4">
<div align="center"><strong>ПРЕВОЗЕН БИЛЕТ</strong></div>
</td>
</h2>
</tr>
<p>
<tr >
<td colspan="2" align="right">
</table>
</div>
I think the problem is that on each button click you create a new instance of dialog box.
Try initializing all the dialog boxes in the beginning of the script and let the buttons only open them.
Another issue is that the dialog div is no longer on the table after initialization:
<div id="btn<?=$row['NOMER']?>".....
$(function () {
$(".dialogClass").each(function(){
$(this).dialog({ autoOpen: false });
});
$(".buttonClass").on("click", function () {
// get the div element with the id dialogClass contained at the same scope as button!
var btnId = ($(this).attr("id"));
btnId = btnId.replace("btn","");
$("#dialogPicture_" + btnId).dialog("open").css({
"font-size": "13px"
});
});
});
You can try out this.
You should use this Code to destroy the Dialog in Button Section
$("#TestMenuDialog").dialog("close").dialog("destroy").remove();
var NewDialog = $('<div id="TestMenuDialog"><p style="color:Red;style="font-size:7x;font-weight:normal">' + Message + '</p></div>');
NewDialog.dialog({
modal: false,
title: "Test Dialog",
height: 200,
width: 375,
show: 'clip',
hide: 'clip',
buttons: [
{ text: "OK", click: function () { $(this).dialog("close").dialog("destroy").remove(); } }
]
});
check the Jfiddle Link It works fine
http://jsfiddle.net/zpP3c/1/