I am trying to fetch data using data and it is returning json in whole page.
This is my code on Controller to fetch products:
public function products(){
$allProducts = $this->product->has('Sizes')->has('Images')->paginate(9);
$prodCategories = $this->products_categories->get();
return response()->json( $allProducts);
}
And in the ajax, I have the following code:
$( document ).ready(function() {
$.ajax({
url: "{{ route('displayProduct') }}",
dataType: 'json',
method: 'get',
data: { _token:"{{ csrf_token() }}"},
success(response){
let array = response.data;
if(array == 0){
html += '<div class="block"><h2 class="aside-title">Product not found!</h2><p class="errorProduct">Sorry! There is no product in this category. Please search products on other categories</p></div>';
$('#productClick').html(html);
}
$.each( array, function( key, value ) {
let image = jQuery.makeArray(value.images);
html += '<div class="product-layout product-layout-table col-lg-4 col-md-4 col-sm-6 col-xs-6 col-12">';
html += '<div class="product-box clearfix">';
html += '<div class="product-image">';
html += '<a href="/product/'+value.slug+'" class="c-img link-product">';
html += '<img src="'+image[0]['image']+'" class="img-responsive" alt=""></a>';
html += '<a class="smooth quickview iframe-link btn-button quickview quickview_handler visible-lg" href="/quick/'+value.slug+'" title="Quick view" target="_self" data-fancybox-type="iframe">';
html += '<i class="fa fa-search" aria-hidden="true"></i></a></div>';
html += '<div class="product-info"><h4 class="product-name">'+value.title+'</h4>';
html += '<div class="price">'+value.price+' <span class="price-old">$142.00</span></div>';
html += '<div class="product-desc">';
html += '<p>'+value.excerpt+'</p></div></div>';
html += '<div class="button-group"><button class="add-to-cart smooth" onclick="window.location.href=\'cart.html\'">ADD TO CART</button></div></div></div></div></div>';
});
$('#productClick').html(html);
loadIframe();
}
});
});
But, when I load the product I am getting json data on the whole page.
The function loadFrame has following code:
function loadIframe(){
$.getScript( "ajax/test.js" )
}
Related
how to get dynamic data from database in Wordpress ajax if their is already a query is running inside a function in Wordpress
<script type="text/javascript" >
var ajaxurl = "<?php echo admin_url('admin-ajax.php');?>";
jQuery(document).ready(function($) {
var data = {
'action': 'wa_action'
};
$.post(ajaxurl, data, function(result) {
var contactList = JSON.parse(result);
console.log(contactList);
var contents = '<ul id="whatsapps" class="list" >';
contactList.forEach(contact => {
contents += '<li class="child">';
contents += '<a href="https://wa.me/+91'+contact.contact_number+'" target="_blank">';
contents += '<img src="<?php echo PLUGIN_URL."/CustomPlugin/assets/images/whatsapp.png" ?>" class="img-fluid" target="_blank" alt="logo" />';
contents += '<span>'+contact.name + '(' + contact.designation +')' + '</span>';
contents += '</a>';
contents += '</li>';
});
contents += '<li class="parent"><a><img src="<?php echo PLUGIN_URL."/CustomPlugin/assets/images/whatsapp.png" ?>" class="img-fluid" target="_blank" alt="logo"></a></li>';
contents += '</ul>';
$('body').append(contents);
$("#whatsapps .parent").click(function () {
$("#whatsapps").toggleClass("show");
});
});
});
</script>
How to add CSS for positioning here
I have tried to get response using jQuery ajax call but HTML div is not return in ajax response in WordPress.
Following is my HTML
<form action="" name="article_search" id="article_search">
<input type="search" name="search_article_post" class="form-control search_article_post" placeholder="search">
<button type="button" name="search_article" class="project-search-icon search_article">
<img src="<?php echo THEME_DIR; ?>/images/project-search-icon.png">
</button>
</form>
<div class="testimonials-slider"></div> // Ajax output display in this div
Following is my ajax call.
jQuery(document).ready(function(){
jQuery(".search_article").on("click",function(){
var search_by_name = jQuery('.search_article_post').val();
var data = {
action: 'get_articles_details',
search_by_name:search_by_name
};
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
jQuery.ajax({
method: 'post',
url:ajaxurl,
dataType: "html",
data: data,
beforeSend: function (response) {
jQuery("#loading").show();
},
complete: function (response) {
jQuery("#loading").hide();
},
success: function (response) {
jQuery('.testimonials-slider').html(response);
console.log(response)
},
});
return false;
});
});
Following script is written in functions.php in wordpress theme for ajax call
add_action('wp_ajax_get_articles_details' , 'get_articles_details');
add_action('wp_ajax_nopriv_get_articles_details', 'get_articles_details');
function get_articles_details(){
$serch_by_name = isset($_POST['search_by_name']) && !empty($_POST['search_by_name']) ? $_POST['search_by_name'] : '';
$post_arg =array( 'posts_per_page' => -1,
's' => esc_attr( $serch_by_name ),
'post_type' => 'post',
'orderby' =>$serch_by_name,
'order'=>'desc'
);
$get_articles = new WP_Query($post_arg);
global $post;
if($get_articles->have_posts()){
while($get_articles->have_posts()) : $get_articles->the_post();
$html .='<div class="slider-content"><div class="article-content bg-white">';
$html .='<div class="article-img">'.the_post_thumbnail().'</div>';
$html .='</div></div>';
?>
<?php
endwhile;
}
echo $html;
}
I get following output
<img width="481" height="321" src="image_path" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="image_path" sizes="(max-width: 481px) 100vw, 481px" />
<div class="slider-content"><div class="article-content bg-white"><div class="article-img"></div></div>
I mean image is not add in div class article-img it's added before class. so in response of HTML in not getting proper might be missing something.
You need to write
echo json_encode($html);exit;
or
echo $html;exit
I am using bootstrap 3.3 & trying to show ajax content on popover. Here is my jquery code
$('.action_col span').popover({
html: true,
trigger: 'manual',
placement: 'left',
content: function () {
var div_id = 'tmp-id-' + $.now();
var tour_id = $.trim($(this).data('tour_id'));
$.ajax({
type: 'post',
url: site_url + 'controller/tour_details',
dataType: 'html',
data: {
t: tour_id
}
}).done(function (response) {
$('#' + div_id).html(response);
});
return '<div id="' + div_id + '">Loading...</div>';
}
});
tour_details method
echo 'working';
This works fine but when I send any HTML it doesn't work anymore. Like
echo '<div class="popover"> <div class="arrow"></div> <h3 class="popover-title">test</h3> <div class="popover-content"> <p>fgds</p> </div> </div>';
This is not working. Whats wrong here?
My HTML view file
<span data-container="body" data-toggle="popover" data-tour_id="88">[Tour # 48844]</span>
I'm beginner to the JSON with PHP if you can please help me. the code repeat the same output in 3 times. I include the require codes here. The code details are given below.
../Controller/controller.php has the MySQL code to retrieve data from database
donnerdata.js:
function member_note(){
var tableData;
$.post("../Controller/controller.php", {loading_donnerdata: 'table'}, function(e) {
if (e === undefined || e.length === 0 || e === null) {
tableData = 'noData';
$('#orders-datatable').html('').append(tableData);
} else {
$.each(e, function() {
tableData += '<div class="row comment">';
tableData += '<div class="col-sm-10">';
tableData += '<div class="message clearfix">';
tableData += '<div class="note">';
tableData += '<p>';
tableData += 'something';
tableData += '</p>';
tableData += '</div>';
tableData += '</div>';
tableData += '</div>';
});
//Load Json Data to Table
$('#orders-datatable').html('').append(tableData);
}
}, "json");
}
viewStudent.php:
<div class="comments">
<div class="" id="orders-datatable">
<?php require_once '../../Common/all_js_links.php'; ?>
<script src="js/donnerdata.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(function() {
member_note();
});
});
</script>
</div>
</div>
Try this instead of your doubly-nested callback.
$(document).ready(function() {
member_note();
});
I wish to get the file size and show inside my disabled textbox.
I googled and there is very less info about this.
Following bellow is my code
In PHP:
<div class="inputWrapper">
<div class="usetitle">* <?php echo Yii::t('labels', 'gameapk'); ?> :
<div class="div_upload_file_container">
<div style="position:absolute; margin: 0; padding:0; height:27px">
<input style="display:none" id="btnUploadFile" name="btnUploadFile" type="file" />
</div>
<div class="div_upload_file_container_inner">
<div class='word'>
</div>
<div style='clear:both'></div>
</div>
</div>
<div style="height:50px"></div>
<div id="uploaded_file"></div>
</div>
<div class="error2"><div id="err_btnUploadFile"></div></div>
</div>
In JS FILE:
//file
$('#btnUploadFile').uploadify({
'swf': base_url + '/js/plugins/uploadify321/uploadify.swf',
'uploader': "index.php?r=apk/uploadfile",
'folder' : '/temp',
'queueID': 'uploaded_file',
'queueSizeLimit' : 1,
'progressData': 'speed',
'removeCompleted': false,
'width': $('.div_upload_file_container').width(),
'height': 28,
'fileTypeDesc': 'Apk Files (*.apk)',
'fileTypeExts': '*.apk;',
'fileSizeLimit': '100GB',
'uploadLimit' : 1,
'itemTemplate': uploadedFileItem(),
'buttonText' : UPLOAD_FILE,
'onSelect': function (file) {
},
'onUploadStart': function (file) {
},
'onUploadSuccess': function (file, data, response) {
console.log (data);
$('#' + file.id).html(file.name);
var obj = JSON.parse(data);
statusCode = obj.statusCode;
if (statusCode == '200'){
console.log (data);
var today = obj.today;
var tmp_folder_with_date = obj.tmp_folder_with_date;
var filesize = obj.filesize;
var hashfilename_filename = obj.hashfilename_filename;
var full_path = obj.full_path;
file_cnt ++;
var html = '';
html = '<div>';
html += '<div style="float:left">';
html += file_cnt + '.';
html += '</div>';
html += '<div style="float:left; margin: 0 0 0 10px">';
html += file.name;
html += '</div>';
html += '<div style="clear:both"></div>';
html += '</div>';
$('#' + file.id).html(html);
}
var params = {
fileid: file.id,
fileName: obj.fileName,
fullSavePath_original: obj.fullSavePath_original,
fullSavePath_resize: obj.fullSavePath_resize,
fullSavePath_tobesave: obj.fullSavePath_tobesave,
fullSavePath_tobesaveURL: obj.fullSavePath_tobesaveURL,
filesize: obj.fileSize,
fullPath_TempFolder: obj.fullPath_TempFolder,
orientation: obj.orientation,
tobesave_file_width: obj.tobesave_file_width,
tobesave_file_height: obj.tobesave_file_height,
todayTempFolder: obj.todayTempFolder
};
DisplayNewUploadedPhotos(params);
},
'onUploadComplete': function (file) {
},
'onCancel': function (event,ID,fileObj,data) {
},
'onDialogClose': function (queueData) {
},
'onInit': function (instance) {
},
'onQueueComplete': function (queueData) {
},
'onClearQueue': function (queueItemCount) {
}
});
function uploadedFileItem() {
var html = '';
html = '<div id="${fileID}" class="fileUploadedItem">';
html += '<div style="margin:10px 0 0 0"></div>';
html += '<div class="uploadify-progress-bar"></div>';
html += '<div style="margin:10px 0 0 0">';
html += '<center>Cancel</center>';
html += '</div>';
html += '</div>';
return html;
}
obj.filesize is the file size ... Am I correct ? If I was correct , what should I do to get obj.filesize from JS to my textbox ?
I have an idea to do that suddenly:
$("#appsize").attr('value', obj.filesize);
HAHA THANKS =D