Calling a php file with jquery - session var is not read - php

this is the code that I have to call my index.php file.
<script type="text/javascript">
$('button').click(function(){
$.ajax({url: 'permiteCookie.php'});
document.getElementById('div_cookies').style.display = 'none';
return false;
})
</script>
<?php
session_start();
if (!isset($_SESSION['permiteCookie'])){
?>
<div class="div_cookies" style="display: block;">
<div>
<div>
<div class="cookies_botoes">
<div class="cookies_bt2">
<button id="button" name="button" type="button">Close</button>
<a id="button" name="button" href="javascript:void(null);">Close</a></div>
</div>
</div>
</div>
</div>
<?php
}
?>
and this is the code that I have in the file permiteCookie.php
<?php
session_start();
$_SESSION['permiteCookie']=1;
?>
Problem: I cant access to the $_SESSION['permiteCookie'] in the index.php file.
What am I doing wrong?

The script is running before the DOM is ready. You'll need to wrap it in a $(document).ready() handler:
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
$.ajax({url: 'permiteCookie.php'});
document.getElementById('div_cookies').style.display = 'none';
return false;
});
});
</script>
And as long as you're using jQuery, why use the plain DOM to hide the div?
$('#div_cookies').hide();

if I'm not mistaken, because session_start() affects the page headers, it needs to be at the top of the page, preferably the first line of code.
i.e. add this segment to the very top of index.php :
<?php
session_start();
if (!isset($_SESSION['permiteCookie'])){
?>

Related

how to load others comments with out page refresshing

Loading comments others automatically like facebook
**php code updated **
<section class="panel-body">
<div class="container">
<div class="comment">
<div class="commenter" name="comments">
<img width="80px" height="80px" src="uploads/<?php echo $row['photo']; ?>">
</div>
<div class="comment-text-area">
<textarea class="textinput"id="comment" name="comments" placeholder="Comment Here......"></textarea>
<button type="submit" class="butn"name="compost">post comment</button>
</div>
</div>
</div>
</div>
</fotm>
<div id="comments">
<?php include'cajax.php'; ?>
</div>
</section>
</section>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js">
</script>
<script type="text/javascript">
$(document).ready( function(){
$('#comments').load('cajax.php');
refresh();
});
function refresh()
{
setTimeout( function() {
$('#comments').load('cajax.php');
refresh();
}, 1000);
}
</script>
how to load comments automatically without page refreshing I know its
possible with ajax but how to implement code please help us
first create another php file named comments.php or whatever you want and cut and paste the code that fetch the comment from DB in that file
And in your main file where you want to show comments create a div , id="comments" or whatever you want .. and inside that div include the new php file like this
<div id="comments">
<?php include'comments.php'; ?>
</div>
now above that code paste the following javascript code:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js">
</script>
<script type="text/javascript">
$(document).ready( function(){
$('#div_name').load('filename.php');
refresh();
});
function refresh()
{
setTimeout( function() {
$('#div_name').load('filename.php');
refresh();
}, 1000);
}
</script>
Replace the div_name with the name of your div and filename.php .. with your file name

Change Visited Link

I have this code:
<div class="menuList">
<li><img src="/images/icon/arena.png" alt="">Arena<span class="green"> (+)</span>
</li>
</div>
And i want to remove <span class="green"> (+)</span> after users click that link.
Anyone can help me (php code)?
As already mentioned, PHP is not the ideal language to do this in. But, if you need to use PHP, here is how you could do it.
Set a session variable on the /arena/ page, like this;
<?php
session_start();
$_SESSSION['visited'] = 1;
?>
Then, use PHP to check for the session variable in your HTML code like this:
<div class="menuList">
<li><a href="/arena/"><img src="/images/icon/arena.png" alt="">Arena
<?PHP
If(isset($_SESSION['visited'])){
echo '<span class="green"> (+)</span>';
}
?>
</a>
</li>
</div>
You will need to add session_start() to the top of the page wherever you are accessing session variables, before you output anything to the page (e.g. DOCTYPE declaration.)
in Jquery you may do this like
<script>
$(function(){
$('.menuList').find('a').click(function(){
$(this).children('.green').remove();
});
});
</script>
You can accomplish this by adding this javascript to your page:
<script>
window.onload = function() {
var a = document.querySelector('.menuList a');
a.onclick = function() {
var span = a.querySelector('.green');
a.removeChild(span);
}
}
</script>
OR if you're using jQuery:
<script>
$(document).ready(function(){
$('.menuList').find('a').click(function(){
$(this).find('.green').remove();
});
});
</script>

Refresh part on my Page

I'm trying to refresh this part of the page
<div class="refresh">
<?php
include("message.php");
?>
</div>
with this code :
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#refresh').empty();
$('#refresh').load('message.php');
}, 500);
</script>
Could you help me?
change this
<div class="refresh">
To
<div id="refresh">
because you are using id selector in jquery to load the content.
if you are using $('#refresh') then "#" represents the id, so change this
<div class="refresh">
to
<div id="refresh">
or change this
$('#refresh')
to
$('.refresh').

How to refresh different div in same page

I am using php and mysql database for my website.
In my website I have four different menu. In that each menu have single div which has to refresh after clicking to it.
Here refresh mean it call php file which has functionality like insert,delete and display the specific contain from database.
My code is refreshing whole page instate of div refresh.
Here is my sample code:
My mainpage.php
<div>
<ul id="headerMenu" class="menu">
<li rel="menu1"><a href="" >About</a></li>
<li rel="menu2">Display</li>
<li rel="menu3">Settings</li>
</ul>
</div>
<div id="menu1" class="menu_content">
<div class="right-pane">
<div class="details">
<?php include('about.php'); ?>
</div>
<div id="menu2" class="menu_content">
<div class="right-pane">
<div class="details">
<?php include('display.php'); ?>
</div>
<div id="menu3" class="menu_content">
<div class="right-pane">
<div class="details">
<?php include('Settings.php'); ?>
</div>
My display.php
<?php
/* fetched latest data from database is done */
/* if i use header it going to main page about div */
?>
Now I want to display data in my display div.
Can any one please tell me how to achieve this?
Thank you.
Now Try this code.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$( "button[class='disp']" ).click(function() {
$(".details").load('profile.php');
});
});
</script>
</head>
<body>
<div>
<ul id="headerMenu" class="menu">
<li rel="menu1"><button>About</button></li>
<li rel="menu2"><button class="disp">Display</button></li>
<li rel="menu3"><button>Settings</button></li>
</ul>
</div>
<div id="menu1" class="menu_content">
<div class="right-pane">
<div class="about">
<?php //include('about.php'); ?>
</div>
</div>
<div id="menu2" class="menu_content">
<div class="right-pane">
<div class="details">
<?php include('profile.php'); ?>
</div>
</div>
</div>
<div id="menu3" class="menu_content">
<div class="right-pane">
<div class="Settings">
<?php //include('Settings.php'); ?>
</div>
</div>
</div>
</div>
</body>
</html>
Just add profile.php like that.
<?php echo rand(1, 9999); ?>
Do not forget to add JQuery. http://jquery.com/download/
Hope it will help you.
Thanks
You can do that easily with ajax.
Example for one DIV:
<html>
<body>
<script type="text/javascript">
function Ajax(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();// For Firefox, Opera 8.0+, Safari and Chrome
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // For Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("No AJAX!!!");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
setTimeout('Ajax()',10000);
}
}
xmlHttp.open("GET","http://www.examplepage.com/the_page_that_contains_the_div_content",true);
xmlHttp.send(null);
}
window.onload=function(){
setTimeout('Ajax()',10000);
}
</script>
<div id="ReloadThis">Default text</div>
</body>
</html>
try
$('.menu_content').on('click',function(e){
e.stopPropagation();
var $menu_to_load=$(this).children('.right-pane .details')
$menu_to_load.load( $menu_to_load.attr('id'));
});
/*make sure your div with class "detail" have id of the page you want to call for them */
Put all your functionality in php together print it in json format json_encode(outputdatafromphp); get that value from your ajax function in javascript and make your menu div tag as clickable inside the success function
`
$.ajax({
url:url,
type:'post',
dataType:'json',
success:function(out){
$('#menu1').click(out,function(){
$.each(out,function(ie,value){
alert(value.data);
}
}
$('#menu2').click(out,function(){
}
});
What lbu said.
Use $.ajax to do a call to a php program that will return the data you want. for this example, hold it in var ajax_data
On .success event of ajax call, stuff the contents into the correct div like this:
<!-- html definition of display div -->
<div id="displaystuff"></div>
...
// script:
$('#displaystuf').html(ajax_data)
PHP is on the server side, so it can only generates "pages",.. If you want to modify the content of a "page" without refresh the page itself, you have to use javascript to make ajax request. So you can insert a php response in the HTMLElement you want.
It's easyer with jQuery.
http://api.jquery.com/jquery.ajax/

AJAX load PHP file

I am a newbie on AJAX, I have a link that loads table.php. Then it writes the code to the index.php. In that code, I have another link to
show the info.php. Is it possible to do this?
<!--This is index.php-->
<div id="link">my Info</div><!--it works here-->
<div id="link">My Table</div>
<div id="table"></div>
<div id="info"></div>
<!--My javascript-->
<script type="text/javascript">
$(document).ready(function() {
$('#link a').click(function(){
var page = $(this).attr('href');
if(page =='table')
$('#table').load('table.php');
else if(page =='info')
$('#info').load('info.php');
return false;
})
});
</script>
<!--This is table.php-->
<div id="link">my Info</div><!--it doesn't works here-->
<!--This is info.php-->
<h1>My info</h1>
Your three <div> (as pointed out by #scragar) have the same id link, most probably causing the issue. Make it a class like that :
<div class="link">
And in your JS :
$('.link a')
EDIT : As noted by dbf, you must as well declare your handler with live() or on() instead of click() :
$('.link a').live('click', function(){ ... });
in order for it to be binded after table.php is loaded in the page. ( http://api.jquery.com/live/ )

Categories