Reloading page custom fetched data - php

Basically I'm trying to load a tab with the following unique_id data from a button. The issue is I want the tab to load with the corresponding variable without reloading the page. I've looked on here for solutions regarding ajax URL rewrite but nothing is working. I dont care about adding more files here and there I just need a solution to this minor problem. Thanks
<a href="?user_id='. $row['unique_id'] .'">
... blah .. stuff.. etc
</a>

Ajax loading into a tab in 2 flavors, vanilla and jquery
function loadTabOG() {
let url = 'someURLThatWillReturnHTML.php';
fetch(url)
.then(response => response.text())
.then(html => {
document.getElementById('tab-2').innerHTML += html
})
// the snippet won't allow us to do an ajax request, so we'll simulate the response here.
document.getElementById('tab-2').innerHTML += "<p>Here is some new html from regular javascript</p>";
}
function loadTabJQ() {
let url = 'someURLThatWillReturnHTML.php';
$.ajax({
url: url,
}).done(function(html) {
$('#tab-2').append(html)
});
// the snippet won't allow us to do an ajax request, so we'll simulate the response here.
$('#tab-2').append("<p>Here is some new html from jquery</p>")
}
.tab {
border: 1px solid #ccc;
border-radius: 8px;
padding: 15px;
margin: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='tab' id='tab-1'>
<h3>Tab 1</h3>
<ul>
<li><a href='javascript:void(0)' onclick='loadTabOG()'>Load tab 2 using regular javascript</a>
</li>
<li> <a href='javascript:void(0)' onclick='loadTabJQ()'>Load tab 2 using jquery</a>
</li>
</ul>
</div>
<div class='tab' id='tab-2'>
<h3>Tab 2</h3>
<div>

Related

Bad redirect when translating with php using a button: <a href="?lang=en">

I have managed to translate an example web page using an array, the problem I have found is that when pressing the button (it is used to translate) it directs you to the index, and what I want is to translate the page where you are you find.
I leave you the code in addition to some image:
<div class = "menu">
<ul>
<li>
<? php echo $ lang ['home']?>
</li>
<li>
<? php echo $ lang ['about']?>
</li>
<? php
include ('menu.php');
?>
<li class = "right">
<? php echo $ lang ['Spanish']?>
</li>
<li class = "right">
<? php echo $ lang ['catalan']?>
</li>
<li class = "right">
<? php echo $ lang ['english']?>
</li>
</ul>
https://ibb.co/w62zkDM
I don't know how to do it any other way, I don't know if what I'm trying to do can be done, I have the translations in a single folder.
sorry for my english i'm using google translate.
I have also used:
but it redirects me to index anyway.
Sorry for my english i´m using google translate.
Your requirement is to translate the language of a page. Iam not sure how much the below solution using JS will helps.
<html>
<head>
<style>
.goog-te-banner-frame.skiptranslate {
display: none !important;
}
.goog-gt-tt{
display:none;
width:0px;
height:0px;
}
a{
cursor:pointer;
}
#button{
background:#3399ff;
padding:5px 10px;
box-shadow:0px 6px 16px -6px rgba(1,1,1,0.5);
border:0;
outline:none;
margin:0px 10px;
}
</style>
</head>
<body>
<a onclick="changeLanguageByButtonClick('de')" id='button'>German</a>
<a onclick="changeLanguageByButtonClick('es')" id='button'>Spanish</a>
<a onclick="changeLanguageByButtonClick('en')" id='button'>English</a>
<!--PAGELOADER-->
<div id="google_translate_element" style="display:none"></div>
<h1>Google Translator:Default Language English</h1>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: "en"}, 'google_translate_element');
}
function changeLanguageByButtonClick(language) {
var selectField = document.querySelector("#google_translate_element select");
for(var i=0; i < selectField.children.length; i++){
var option = selectField.children[i];
// find desired langauge and change the former language of the hidden selection-field
if(option.value==language){
selectField.selectedIndex = i;
// trigger change event afterwards to make google-lib translate this side
selectField.dispatchEvent(new Event('change'));
break;
}
}
}
</script>
</body>
</html>

PHP / AJAX not getting return

I am using AJAX to receive data from my database on to my main PHP page.
I have a piece of code that worked, but on with PHP.
When I have just tried to put it in to AJAX (receiving format), the code that I return is not being shown.
I know my AJAX method works as I'm using it to get some other database values.
It's just the Get online users individually won't work.
When I load the page, the code shows what's inside my div id - Loading Info... and then goes blank, so I know it's trying to update it but it's not getting it correctly.
Picture showing that nothing is displayed
My PHP request code is :
//Get online users individually and echo if they're online or not in a div class
$user_grab = mysqli_query($con, "SELECT * FROM users");
while($users_ = mysqli_fetch_array($user_grab)) {
$last_online = strtotime($users_['lastonline']);
if(time() - $last_online < 30) {
$client_is_online = '
<div class="chat-list-item -available" style="background: rgba(255,255,255,0.1); padding: 5px;">
<img class="chat-list-avatar" src="'.$users_['profile_picture'].'" style="width: 40px; height: 40px; padding: 7px; border-radius: 20px;" /><i class="fa fa-circle chat-list-status"> </i>
<div class="chat-list-user">'.$users_['username'].' (<font size="2">'.get_users_level_all($users_['userLevel']).'</font>)</div>
<div class="chat-list-excerpt">Online</div>
</div>
';
} else {
$client_is_online = '
<div class="chat-list-item -offline" style="background: rgba(255,255,255,0.1); padding: 5px;">
<img class="chat-list-avatar" src="'.$users_['profile_picture'].'" style="width: 40px; height: 40px; padding: 7px; border-radius: 20px;" /><i class="fa fa-circle chat-list-status"> </i>
<div class="chat-list-user">'.$users_['username'].' (<font size="2">'.get_users_level_all($users_['userLevel']).'</font>)</div>
<div class="chat-list-excerpt">Offline</div>
</div>
';
}
}
//I then echo it back to my home PHP page so it can read the values
//Ignore my other code definitions below as I know they work
//$client_is_online is the only one which doesn't
echo $totalUsers.",".$totalOnline.",".$freemode.",".$bypasses.",".$client_is_online;
My AJAX recieve code is :
<script>
function fetchOnline() {
$.ajax({
url: "includes/get_dash_settings.php",
context: document.body,
success: function(value){
var data = value.split(",");
$('#totalUsers').html(data[0]);
$('#totalOnline').html(data[1]);
$('#freeModeStatus').html(data[2]);
$('#bypassesStatus').html(data[3]);
$('#isOnline').html(data[4]);
},
complete:function(){
setTimeout(fetchOnline,5000);
}
})
}
$(document).ready(function() { setInterval(fetchOnline,5000); });
</script>
I then try storing the returned data in-side my div id :
<div class="sidebar-tab-content" id="staff">
<div class="chat-list sidebar-content-section" id="isOnline">
Loading Info...
</div>
</div>
Return the json data like this
1st : your overwriting the variable . you need to concatenate all user like this
$client_is_online=""; //declare empty string before while loop start
//while loop start here
$client_is_online .= 'html here';
// while end here
2nd : Return the json data like this
$response = array ('totalUsers'=> $totalUsers, 'totalOnline'=> $totalOnline,'freemode'=>$freemode,'bypasses'=>$bypasses,'client_is_online'=>$client_is_online);
header('Content-Type: application/json');
echo json_encode($response);
3rd : Don't forgot to add dataType in ajax
dataType: "json",
4rd : success function should be changed like this
ajax :
success: function(value){
var data = JSON.parse(value);
$('#totalUsers').html(data['totalUsers']);
$('#totalOnline').html(data['totalOnline']);
$('#freeModeStatus').html(data['freemode']);
$('#bypassesStatus').html(data['bypasses']);
$('#isOnline').html(data['client_is_online']);
},

Can't bind event to dynamically created element

I have following html structure in editstaff.php:
<div id="result" style="display:none">This is result div</div>
<form id="adres" onsubmit="return submitForm();">
<input type="hidden" name="type" value="adrs" />
<input type="submit" value="Address" /></form>
And in inline-style:
#result{
position: absolute;
border: 5px solid gray;
padding: 10px;
background: white;
width: 270px;
height: 190px;
}
What this html page do, clicking on "Address" button of "adres" form, some post data is send to another php page which shows information based on sent data on a pop-up like div (which style was display:none) fadeIn from display:none. The javascript/jquery codes for this purpose are as follows:
<script>
function submitForm(){
var data=$("#adres").serializeArray();
/* alert(data); */
data.push(
{
name:'sname',value:$("#title").val()
}
);
$.post("geteditdata.php",data,
function(data){
$("#result").html(data);
positionPopup();
$("#result").fadeIn(1000);
}
)
return false;
}
function positionPopup(){
$("#result").css({
left: ($(window).width() - $('#result').width()) / 2,
top: ($(window).width() - $('#result').width()) / 2,
position:'absolute'
});
}
$("#divclose").live('click',function(){
$("#result").fadeOut(500);
});
</script>
i.e the div in editstaff.php with fetched data will pop-up like following structure:
<div id="result">
some_value
Close
</div>
All things is going on okay upto this stage. But when I am clicking on "Close" link on pop-up div the div is not closing(fadeOut) with $("#divclose").click(function()
Why this is not happening in this case?
can anybody give a solution for me?
I am giving a demo page where you can see the problem in practical.
Please visit http://raddacentre.org/editstaff.php and
write 'Afsar' in "Search by name" field and
then press "show".
After page loads, please press "Address" button which will be at the bottom of the page.
Then a pop-up div will be shown where there will be a link named "Close".
Press that button and please check why this div is not fade in there?
As I used jquery version 1.3, I used $("#divclose").live method instead of $(selector).on method.
Any help will be appreciated.
in geteditdata.php there is only this code:
<?php
echo $_POST['sname'];
?>
<br /><br />Close Here
You are using really old jQuery 1.2.3 (it's not jq 1.3), I'm not sure if .live exist in that version. So two things:
Use new jQuery (I suggest 1.9)
Use .on instead of .live (read about it, syntax is a little bit different)
Code should look like that:
$("body").on('click', "#divclose", function(){
$("#result").fadeOut(500);
});
Instead of "body" you can use other container that exist when event is being binded.

How to keep my text on the same line with AJAX call?

I'm trying to keep my ajax call on the same line, as detailed here: http://external.sidewaykill.com/versound/motd.php.
It won't, so what can I do?
$(document).ready(function() {
$.ajaxSetup ({
cache: false
});
var ajax_load = "<img src='http://static.sidewaykill.com/img/ajax-loader2.gif' alt='loading...' />";
// load() functions
var loadUrl = "playercount.php?id=2";
$("#servercount").html(ajax_load).load(loadUrl);
});
This has nothing to do with PHP but everything to do with the absolutely awful markup of your HTML and the lack of CSS. You're going to want to wrap all of this "free-text" inside of a div or two, and then make sure you're putting the wrapper div inside of the infobar div
Sample CSS:
#infobar p{
float: left;
margin-left: 5px;
}
HTML:
<div id="infobar">
<div style="float: left;">
<p>Welcome! Our server count is:</p>
<p>1 / 10</p>
<p>We hope you enjoy your stay! We are currently playing on gm_construct.</p>
</div>
</div>
its because the ajax call returning a div element,
as this :
<div id="count"> 0 / 10</div>
Since div is a block element it will always render in a new line. there are two solutions for this
one would be to return a <span> element instead of the div
second will be if u are unable to change the return value you can fix it via css
#servercount #count { display : inline }
My suggestion is to go with the first solutions

Dynamic Sliding Panel Using JQuery, based on values from a mySQL database

I am in the process of developing an academic toolkit for my university. The problem statement is, the user will be given a list of courses. When one clicks on that particular name of the course, he should get a dynamic slide panel showing the course objective and other details of that course. All these values will be present in a mySQL database. The slide panel is a must requirement here. So please help me how to get the data dynamically in the slide panel from the mySQL database. Thanks in advance... :)
http://api.jquery.com/jQuery.ajax/'>jQuery's $.ajax is your friend!
Something like the following should work (it is untested and not optimized). Hopefully it will lead you in the right direction. You should also add a loading message, error handling, and data caching.
<style>
.course{
border:solid 1px #000;
margin-bottom:10px;
}
.title{
display:block;
border-bottom:solid 1px #000;
background:#eee;
font-weight:bold;
}
.details{
display:none;
}
</style>
<div class='course'>
<a class='title' href='/classDetails.php?classID=54321'>Composition 101</a>
<div class='details'></div>
</div>
<div class='course'>
<a class='title' href='/classDetails.php?classID=54322'>Composition 201</a>
<div class='details'></div>
</div>
<div class='course'>
<a class='title' href='/classDetails.php?classID=54323'>Composition 301</a>
<div class='details'></div>
</div>
<script>
$(function(){
$(".course").each(function(){
var self = $(this);
$(".title",self).click(function(){
$.ajax({
"url":this.href,
"success":function(data){
// extract the content you need from the HTML page.
var content= $(data).find("#content").html()
// insert into the details div and then show it.
self.find(".details").html(content).slideDown(1000);
}
});
// prevent default action...
return false;
});
});
});
</script>
Also note that there are some abstractions of $.ajax to make calls like this easier (such as $("#myElement").load(url), $.post and $.get). You can find more information about these methods at http://api.jquery.com/category/ajax/

Categories