Ajax query not showing mysql results in my div - html

I have a list that I show with an ajax call, each element has an onclick that performs a query, the model receives the correct query but when it refreshes the list it remains blank, it is as if the information from the controller did not arrive because I have made a var_dump to the foreach and mark array[0] when the query does get results and the div remains blank
this is my code:
<?php
foreach ($direc as $direccion) :
?>
<li class="list-group-item">
<div class="row align-items-center no-gutters">
<div class="col mr-2">
<option id="hijo" value="<?= $direccion['first_name'] ?> <?= $direccion['last_name'] ?>" href="javascript:;"
onclick="realizaProceso($(this).val());return false;">
<h4 class="mb-0">
<strong><?= $direccion['first_name'] ?> <?= $direccion['last_name'] ?>, Zona--><?= $direccion['nombre_zona'] ?></strong>
</h4>
</option>
</div>
<div class="col-auto">
</div>
<script>
function realizaProceso(padre) {
var parametros = {
"padre": padre
};
$.ajax({
data: parametros, //datos que se envian a traves de ajax
url: "<?php echo base_url() ?>Administracion/paneladminreddos", //archivo que recibe la peticion
type: 'post', //método de envio
beforeSend: function() {
$("#content").html("Procesando, espere por favor...");
},
success: function(response) { //una vez que el archivo recibe el request lo procesa y lo devuelve
$("#content").html(response);
}
});
}
</script>
There is no error message just that the div does not refresh again.

I would rewrite the HTML portion to correct the markup errors previously identified and bind the event handler after the list has been rendered.
<?php
foreach( $direc as $direccion ) {
?>
<li class="list-group-item">
<div class="row align-items-center no-gutters">
<div class="col mr-2">
<!--
Change the `option` to another element such as `i` as used here.
Change the ID to a `data-id` so that duplicate IDs are not observered in the DOM.
Remove inline event handler and assign external event listener later.
Assign `data-set` attributes so that the event listener can obtain required information when the `i` element is clicked.
-->
<i data-id='hijo' data-zona='<?= $direccion['nombre_zona']; ?>' data-value='<?= $direccion['first_name'];?> <?= $direccion['last_name'];?>' >
<h4 class="mb-0">
<strong><?=$direccion['first_name']; ?> <?=$direccion['last_name']; ?>, Zona--><?=$direccion['nombre_zona']; ?></strong>
</h4>
</i>
</div>
<div class="col-auto">
</div>
</div>
</li>
<?php
}//end foreach loop
?>
And the external event listener:
<script>
document.querySelectorAll('i[ data-id="hijo" ]').forEach( i=>i.addEventListener('click',function(e){
e.preventDefault();
e.stopPropagation();
<?php
printf(
'const url="%s/Administracion/paneladminreddos";',
base_url()
);
?>
// Use `this` to access (dataset) properties of the `i` element.
// Construct the payload for the POST request.
let data={
'padre':this.dataset.value,
'zona':this.dataset.zona
};
$.ajax({
data:data,
url:url,
type:'post',
beforeSend:()=>$('#content').html('Procesando, espere por favor...'),
success:(r)=>$('#content').html(r),
error:(e)=>console.log(e)
});
}));
</script>
An example
document.querySelectorAll('[data-id="hijo"]').forEach(n => n.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
const url = location.href;
let data = {
'padre': this.dataset.value,
'zona': this.dataset.zona
};
$.ajax({
data: data,
url: url,
type: 'post',
beforeSend: () => $('#content').html('Procesando, espere por favor...'),
success: (r) => $('#content').html(r),
error: (e) => console.log(e.statusText)
});
}));
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="list-group-item">
<div class="row align-items-center no-gutters">
<div class="col mr-2">
<i data-id='hijo' data-zona='International' data-value='John Smith'>
<h4 class="mb-0">
<strong>John Smith, Zona -->International</strong>
</h4>
</i>
</div>
<div class="col-auto"></div>
</div>
</li>
<li class="list-group-item">
<div class="row align-items-center no-gutters">
<div class="col mr-2">
<i data-id='hijo' data-zona='Spy' data-value='Mr Bean'>
<h4 class="mb-0">
<strong>Mr Bean, Zona -->Spy</strong>
</h4>
</i>
</div>
<div class="col-auto"></div>
</div>
</li>
<li class="list-group-item">
<div class="row align-items-center no-gutters">
<div class="col mr-2">
<i data-id='hijo' data-zona='Superhero' data-value='Batman'>
<h4 class="mb-0">
<strong>Batman, Zona -->Superhero</strong>
</h4>
</i>
</div>
<div class="col-auto"></div>
</div>
</li>
</ul>

Foreach function will be loop, repeat tag. So if you set id for that, i think you can set by numerical order or ID of that.
<option id="<?= $direccion['id'] ?>" value="<?= $direccion['first_name'] ?> <?= $direccion['last_name'] ?>"

Related

Delete an item(Object) from an array using ng-if

I am trying to remove item using ng-if and ng-else-if.
"ng-if="navItem.gbn.indexOf('12345') !== -1" is true then array item only show contains('12345') items.
but the item still remain in array so I think ng-else-if is not working.
I don't know how to delete it.
I have the following Angular code:
<div ng-controller="PIDAEController">
<div class="container-fluid" style="padding-top:30px;">
<div class="row">
<div class="col-lg-2 col-md-3 col-xs-6 tubmbimg" ng-repeat="navItem in navItems | Fpage: curPage * pageSize | limitTo:pageSize" ng-if="navItem.gbn.indexOf('12345') !== -1">
<a class="thumbnail border" href="#/item/{{navItem.site}}">
<div class="b-marjin">
<span align="center" class="label label-primary full-width" >{{ navItem.gbn }}</span>
</div>
<div>
<img style="border: 1px solid black;" class="img-responsive full-width" src="{{navItem.pimg}}" alt="">
</div>
<div align="center" class="under-block">
<span title="{{ navItem.pname }}" align="center">{{ navItem.pname2}}</span>
</div>
</a>
</div>
<div ng-else-if="navItems.splice(navItem, 1)"></div>
</div>
</div>
</div>
angular.forEach(item, function (value, key) {
var keepGoing = true;
if(keepGoing) {
if(value.gbn.indexOf("11600") != -1) {
$scope.initialized = true;
$scope.navItems = item;
$scope.curPage=0;
$scope.pageSize=20;
$scope.numberOfPage = Math.ceil(item.length / $scope.pageSize);
keepGoing = false;
}
else {
$scope.navItems.splice(key, 1);
}
}
});
ng-else-if does NOT EXIST. its neither angular or angularJS.
In the line
<div ng-else-if="navItems.splice(navItem, 1)"></div>
splice requires navItem to be an index/int, not an object.
As an alternative to ng-else-if you can simply do:
ng-if="navItem.gbn.indexOf('12345') == -1
Also, splice would return the removed array item when found, not sure if "ng-if" handles that as being TRUE.

How could I populate href of a list starting by a json?

I've got 3 videos in a folder and a json file with them, I would make a list near a player, and by clicking the list item I would play that specific video on that player. I've already made html and the function to get the elements to make the list, now I have to make the function to play each video
<section class="container">
<div class="card col-sm-6">
<div class="card-body container">
<h5 class="card-title">Video</h5>
<p class="card-text">Seleziona un video in elenco per visualizzarlo</p>
<div class="row">
<div class="col-sm-6">
<ul id="listvideo" class="list-group list-group-flush border rounded">
</ul>
</div>
<div class="col-sm-6">
<video id="myVideo" src="files/assets/video/by_night.mp4" width="220"></video>
<button id="play" onclick="play()">Play</button>
<button id="stop" onclick="stop()">Stop</button>
<!-- <span id="duration"></span> -->
</div>
</div>
</div>
</div>
</section>
<script>
var myVideo = document.getElementById("myVideo");
function play() {
myVideo.play();
}
function stop(){
myVideo.pause();
}
$(document)
.ready(function() {
$.ajax({
url: 'files/assets/media.json',
method: 'GET',
dataType: "json",
success:function(data){
$.each(data.video,function(i,el){
var titolo = el.title;
console.log(titolo);
$('#listvideo').append('<li class="list-group-item"><a class="choose" href="'+el+'">'+titolo+'</a></li>');
});
}
});
$('.choose').on('click', function(e){
e.preventDefault();
var change = $(this).attr('href');
});
});
</script>

Scraping using codeigniter and i need title and custom link

<div id="archive_content_block">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="archive_cat_caption">
<h4>অর্থনীতি</h4>
</div>
<ul>
<li><i class="fa fa-square-o"></i>
<a href="http://67.227.189.112/~rtvnews24/economy/2126/বাধ্যতামূলক-করারোপের-প্রস্তাব-অর্থমন্ত্রীর">
<font style="color:rgb(33, 33, 33)">বাধ্যতামূলক করারোপের প্রস্তাব অর্থমন্ত্রীর</font>
</a>
</li>
</ul>
</div>
<div class="col-md-6 col-sm-6">
<div class="archive_cat_caption">
<h4>খেলাধুলা</h4>
</div>
<ul>
<li><i class="fa fa-square-o"></i> <font style="color:rgb(33, 33, 33)">অস্ট্রেলিয়া রেকর্ড</font></li>
</ul>
</div>
</div>
</div>
[this is my scraping html page code]
here i have a function which for get scraping title and link. but not work
public function get_dom()
{
$this->load->library('scraping');
$date = date('Y/m/d'); // custom date
$url = "http://67.227.189.112/~rtvnews24/archive/$date"; //my link
$html = file_get_html($url);
$row = $html->find('div.archive_content_block',0); // select content
foreach($row->find('div.archive_cat_caption', 0) as $title) {
echo $title->find('h4', 0)->plaintext.'</br>'; //title
foreach($row->find('ul',0) as $link) {
echo $link->find('li',0)->find('a', 0)->href.'</br>'; //link
}
}
}
here i am try to get title and link. please help me. Thanks in advance

Reload a div tag instead of a page reload using angularjs

I have the below div tag which contains a dropdown and a carousel. Whenever I select from the dropdown I call a function in an AngularJS controller which calls golang at the backend using http.golang to generate the data to be displayed in the carousel and sends a response which is displayed in the carousel but creates a page refresh. I just want this div to be loaded again and not the entire page to load. How can I do this using AngularJS?
HTML:
<div class="col-xs-11" style="width:750pt; height:180pt; margin-top:30px; ">
<div class="row " style="margin-left:75pt;">
<div class="dropdown">
<label class="text-center" style="margin-top:10pt;font-size:14pt;"> Deals on</label>
<button data-toggle="dropdown" class=" btn dropdown-toggle dropdown-menu-hover-color text-center" style="padding:0px 0px;background-color:white;" href="#"><label style="font-size:14pt; color: #191970;" >{{currentCategory}}</label> <b class="caret"></b></button>
<ul class="dropdown-menu submenu-hover-color dropdown-menu-scroll" style="margin:0;">
<li>{{page.category_name}}</li>
</ul>
<div style="display: inline;" ng-hide="deals"> <label class="text-center" style="margin-top:10pt;font-size:14pt;color: #191970;;">No deals found, please select a different category. </label></div>
</div>
</div>
<div id ="carousel_category" class="carousel slide " ng-show ="deals">
<div class="carousel-inner text-center" style="margin-left:75px;">
<div class="item active">
<div class="carousel-width" ng-repeat="(key, value) in categoryList.slice(0, 5) track by $index">
<img ng-src= {{value.imageUrl}} alt="No Image"/>
<h6>{{value.productName}}</h6>
<b style="color:#990000;">{{value.price}}</b> <b style="color:green;">{{value.discount}}</b>
</div>
</div>
<div class="item" ng-repeat="page in category_products_pages">
<div class="carousel-width" ng-repeat="image in page">
<img ng-src= {{image.imageUrl}} alt="No Image" />
<h6>{{image.productName}}</h6>
<b style="color:#990000;">{{image.price}}</b> <b style="color:green;">{{image.discount}}</b>
</div>
</div>
</div> <!--End of carousel-inner-->
<a class="left carousel-control pull-left" href="#carousel_category" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control pull-right" href="#carousel_category" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div><!--End of carousel_cartegory-->
</div>
Angularjs :(Below code is inside angularjs controller)
$scope.changeCategory = function(category_id) {
$http({
method : 'POST',
url : '/changeCategory',
data : {"category_id" : category_id},
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
// set the headers so angular passing info as form data (not request payload)
}).success(function(data, status, headers, config) {
$scope.deals = true;
$scope.products = JSON.parse(JSON.stringify(data));
$scope.currentCategory = $scope.products.categoryInfo.category_name;
$scope.dropdownCategoryList = $scope.products.dropdownCategoryList;
$scope.categoryList = $scope.products.categoryList;
if($scope.categoryList == undefined) {
$scope.deals = false;
return;
}
currList = $scope.categoryList;
for(var i=0;i<currList.length;i++){
product_type = currList[i].product_type;
if (product_type != "Price_With_Discount") {
currList[i].price = "";
currList[i].discount = "Save Upto "+currList[i].discount + "%";
} else {
currList[i].price = "$" + currList[i].price;
currList[i].discount = "(Save "+currList[i].discount+"%)"
}
}
//category_data
var category_products_page = [];
var category_products_pages = [];
var count_1 = 5;
$scope.category_products_pages = category_products_pages;
for(var j=5;j<$scope.categoryList.length;j++){
if(count_1 > 1){
category_products_page.push($scope.categoryList[j]);
count_1--;
if($scope.categoryList.length === j+1){
category_products_pages.push(category_products_page);
//return;
break;
}
}else{
category_products_page.push($scope.categoryList[j]);
category_products_pages.push(category_products_page);
category_products_page = [];
count_1 = 5;
}
}
}).error(function(data, status, headers, config) {
$scope.status = status;
$window.alert("error")
});
}
});

How to reload the same page on browser refresh in angularjs

I've a nav bar and when I click any tab in the nav bar, it takes me to that tab.
<section id="tabs">
<div class="mob-nav">
<div class="nav nav-tabs nav-fill nav-tabs-scroll" id="nav-tab" role="tablist">
<!-- N add new class nav-tabs-scroll -->
<a class="nav-item nav-link" style="padding-top: 10px;"
ng-class="{ active: $index == 0 }"
id="nav-{{menuTab.menuURL}}-tab"
data-toggle="{{menuTab.dataToggle}}"
data-target="#{{menuTab.menuURL}}"
href="#{{menuTab.menuURL}}" role="tab"
aria-controls="nav-{{menuTab.menuURL}}"
ng-repeat="menuTab in menuList">
</a>
</div>
</div>
</section>
<!-- Tab Details -->
<section id="tabs">
<div class="">
<div class="">
<div class="mb-d-150">
<div class="tab-content py-3 px-sm-0 pl-0 pr-0"
id="nav-tabContent">
<!-- N removed py-3 class -->
<div class="tab-pane fade show"
ng-class="{ active: $index == 0 }"
id="{{menuTab.menuURL}}" role="tabpanel"
data-target="#{{menuTab.menuURL}}"
aria-labelledby="nav-{{menuTab.menuURL}}-tab"
ng-include="menuTab.colDef"
ng-repeat="menuTab in menuList">
</div>
<div class="tab-pane fade" id="changepass" role="tabpanel"
aria-labelledby="nav-about-tab"
ng-include="changePasswordTemplate">
</div>
</div>
</div>
</div>
</div>
</section>
Here's an example of menuList.
[{
menuID: "USER LANDING PAGE"
caption: "Dashboard"
parent: "root"
menuURL: "exampleModal"
cssClass: "fas fa-cog fa-lg"
cssParent: "nav navbar-nav"
aClass: "customerLandingPageTemplate"
SlNum: 98
colDef: "/js/templates/user-landing-page.html"
menuList: []
dataToggle: "modal"
},{
menuID: "USER QUERIES"
caption: "USER QUERIES"
parent: "root"
menuURL: "user-queries"
cssClass: "fas fa-comment-alt fa-lg"
cssParent: "nav navbar-nav"
aClass: "userQueriesTemplate"
SlNum: 100
colDef: "/js/templates/user-queries.html"
menuList: []
dataToggle: "tab"
}]
Here's the angularjs part which gives me the menuList:
GetData.async(CONFIG.urlMaker('ws/menulist?userid=' + userid)).then(function (data) {
$scope.menuList = data;
console.log($scope.menuList)
});
When I refresh browser, it always takes me back to the USER LANDING PAGE no matter where I refresh. But on browser refresh, I need to reload the page where I was before refreshing.
You could use the session storage for saving the last state.
Internal code snippet does not allow to use session storage.
A working example on JSBin.
angular.module('app', ['ui.router'])
.config(($stateProvider) => {
const helloState = {
name: 'hello',
url: '/hello',
template: '<h3>hello world!</h3>'
}
const aboutState = {
name: 'about',
url: '/about',
template: '<h3>Its the UI-Router hello world app!</h3>'
}
$stateProvider.state(helloState);
$stateProvider.state(aboutState);
})
.run(($state, $transitions) => {
$transitions.onSuccess({}, transition => {
sessionStorage.setItem('lastState', transition.to().name);
});
const lastState = sessionStorage.getItem('lastState');
if (lastState) {
$state.go(lastState);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="//unpkg.com/#uirouter/angularjs/release/angular-ui-router.min.js"></script>
<body ng-app="app">
<a ui-sref="hello" ui-sref-active="active">Hello</a>
<a ui-sref="about" ui-sref-active="active">About</a>
<ui-view></ui-view>
</body>