My code,
Using below code ineed to store catlog_id in a session but it is not working
$(document).ready(function() {
$("#catlog").click(function(event) {
var catlog ="<?php $this->session->set_userdata('catlog_id',"+$('.catlog_id').val()+")?>";
});
});
Your JavaScript is executed on the client machine and there is no PHP interpreter there. Any code that you create on the fly through JS must be HTML, CSS, JS. Setting cookies can be done through JS, look for a cookie plugins for jQuery.
A good way to access client data is to use cookies.
You can store with javascript/jquery data in a cookie and then you can it later access with php $_COOKIE
You can use the firefox plugin firebug to track the cookie changes.
Related
I want to find out whether the user have javascript enabled or not and I don't want to use the no script tag in HTML because I don't want the user to download extra scripts of both the JavaScript and the no script tag..
Anyone got an idea?
You can use AJAX to request a php page where you can set a cookie which will be used as flag for JS activation. In js script you can check for existance of cookie if it is set it means user has JS enabled
Unfortunately, this is not something you can do. PHP code typically fully executes before the browser renders any code at all.
The disabling of Javascript in the browser is not reported to PHP in the initial header request. The closest you can do is to use Javascript to make an AJAX call to PHP to detect for the presence of Javascript. If the callback fails, then you can assume it is disabled.
Similar question: Can php detect if javascript is on or not?
test.php
<script>
//// query string
if(/[?&]js=1/g.test(window.location.href) == false)
window.location.href += (window.location.href.indexOf("?") == -1 ? "?" : "&") + "js=1";
//// OR session cookie
if(document.cookie.search(/(^|;)js=1/) == -1) {
document.cookie = "js=1; path=/";
document.location.reload();
}
</script>
<?php
$_GET["js"];
////
$_COOKIE["js"];
?>
There's tons of ways to do this. Set a cookie on the client side with JavaScript, then reload the page, or do the above and add a query string to the URL. PHP can check the cookie or query string and set a session variable saying this user has JS enabled.
I hava a strange problem. I'm using jquery $.post() to send/recieve vars from a PHP script.
JavaScript:
$("#r_submit").click(function()
{
$.post("http://" + server + "/msws/",
{
action: "register",
sub_action: "register_validate"
},
function(json)
{
json = $.parseJSON(json);
alert(json.cell_is_good);
});
});
PHP:
http://textuploader.com/?p=6&id=S7zDD
Problem:
If I run the code on my pc (WAMP) it works fine,
but if I upload it to my server (justhost) then it dosn't keep the session
if I alert the session when I create it, it is there, but when I try to get the session
later, it's gone,
I think it has to do with the fact that the server thinks that the browser was closed, so it destroys the session?
Thank you :)
You are starting your session without a session id. For this to work your PHP has to be configured to use transparent session id, which is deactivated by default. So you will have to start the session in the script that has the AJAX-call, and submit the session_id() in the POST request, and start the session in your PHP something like this:
if (!isset($_POST['SID'])) {
die('{}');
}
session_start($_POST['SID']);
Also I would leave out the host-part of the url you are calling in your $.post to avoid cross-domain ajax call problems (also this most likely will not fix your problem). It will work only on the same server, so you can write the ajax call like this:
$.post("/msws/", //...
check if you have multiple session cookies.
i've the same situation with codeigniter and i found that every new page creates its own session
i've got a slight problem with JS enabled detection.
not too big, because i know i'm on the right track.
but here's my deal:
when i try to set a cookie in JS (jQuery) using this code
$(window).load(function(){
$.cookies.set('c_jsEnabled', 'true');
});
or just in plain JS using this code
function setCookie()
{
document.cookie='c_jsEnabled=true';
}
<body onload="setCookie();">
and then try to detect it in PHP using this code
if($_COOKIE['c_jsEnabled'] == 'true')
{
if(file_exists('./main.php'))
{
require_once ('./main.php');
}
echo getIndex();
}
else
{
if(file_exists('./noJS.php'))
{
require_once ('./noJS.php');
}
echo getIndex();
}
setcookie('c_jsEnabled', '');
it takes 2 page refreshes to actually get the right value into PHP.
my guess is that this bascially means that the PHP script is executed before the JS function is fired.
could this be because all codes shown above are in the same script (index.php)?
This is kind of a problem for me, because i want to prevent people from using my website when they have JS disabled.
is there a way to set the cookie before php tries to get the cookie variable?
PHP is always "fired" before JavaScript because PHP is processed on the server and then sends out the HTML and JavaScript for the browser to process and render. You can never expect JavaScript to execute before PHP for this reason.
In your case, use JavaScript to set the cookie and then do a redirect to refresh the page so PHP can get the cookie value and act accordingly.
You should be setting the cookie directly from the PHP file. That way, you know that it exists, and more importantly, you have control of the cookie. You can set it from the client, but that will always execute after the HTML has been sent to the browser, so the PHP file won't get it until the next request.
PHP only sends the cookie header when content is sent to the browser. Javascript then executes after that, so you would need a second load of the page to detect the cookie.
This can trigger infinite redirection loops (especially if the user has cookies disabled), so be careful.
To disable the site to users without Javascript, consider the following.
<div id="noscript" style="width:100%; height:100%; z-index:999; position:absoloute; top:0px; left:0px; background-color:#CC9900; display:block">
Please Enable Javascript!</div>
<script type="text/javascript">
document.getElementById('noscript').style.display = 'none';
</script>
I find the <noscript> tag is unreliable (there was a bug in iOS causing it to only show when there was Javascript, if I remember correctly).
A second option:
You can have the PHP check for a cookie. If it isn't set, have it redirect (header("Location: aaa.html");) to a page with the Javascript to set the cookie and redirect back. (Alternatively, have the PHP output Javascript to set the cookie reload the page.) You then only have to worry about users who "spoof" the cookie, although you will also lock out users who have cookies disabled.
Nope - PHP will always be called before client-side JavaScript, so with this method you'll always have to refresh the page at least once. You're better to develop your site so that non-JS users have a worse-but-still-acceptable experience, or at worst use the <noscript> HTML tag to serve alternative content to those users.
You can't get a cookie in PHP that's being set by JavaScript before the page renders/executes.
You could set the cookie using PHP, however. That will ensure it's set and available regardless of JavaScript or multiple page refreshes.
I am trying to set session array in jquery which I call inside of javascript function that is called onClick event for link.
But it keeps setting me my last choice that I click.
This is code I used for setting session array(I wanted to add new element to session array everytime when someone clicks on the link):
$_SESSION['Ticket'][]=$IDGame;
You are mixing up server-side and client-side languages. If you want to add something to your $_SESSION variable (server-side), you will need to make an ajax request in javascript (client-side) to the server.
I think this is what you're getting at....
$.isArray($_SESSION['Ticket']) ? $_SESSION['Ticket'].push($IDGame) : $_SESSION['Ticket'] = [$IDGame];
You cannot use PHP code within jQuery (not in this case at least). There is a plugin for jQuery (http://plugins.jquery.com/files/jquery.cookie.js.txt) based on the parameters that are given you can setup a cookie or a session for the current user. For instance:
$('#element').click(function(e) {
e.preventDefault();
$.cookie('Ticket[]', $('#IDGame').val();
});
This code assumed the $IDGame is stored in a (hidden) textfield with ID = IDGame. This is the proper way using jQuery with sessions and cookies. If you want to use PHP Code per sé, than you should consider loading a PHP file with the getJSON function and sending the ID as a parameter to the file and adding a new key to the session in the background.
My code structure is like this:
some php code here...
html
head
script
some ajax code here
/script
after running the ajax code, I want to redirect/refresh the page. How can I do it?
Thanks,
Try window.location.reload() in the ajax request success callback.
you can do it with JavaScript; place in AJax callback function:
window.location.reload(); //for refresh
window.location = "http://www.google.com/"; //redirect
Use JavaScript:http://www.tizag.com/javascriptT/javascriptredirect.php
Using window.location="URL" you can send the client to "URL".
But keep in mind that this will not work for people with JavaScript disabled (but so will you AJAX-code).
The best thing to do is to follow the ajax with a javascript redirect using document.location.href='page.html' as you're already relying on running some code in the browser.
Well it depends on what JavaScript version the browser has, but the latest versions should support:
window.location.reload(false);
Have that run on the callback from your AJAX routine.