Save cookie data,string language not in English - php

I find the if i save a cookie by JS Function like below:(w3c school example)
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()+"; path=/project/");
document.cookie = c_name + "=" + c_value;
}
And I use setCookie("hello","你好",30) to save a cookie and use php code to echo it:
<?php
echo $_COOKIE['hello'];
?>
It will output "%u4F60%u597D". not "你好"。
So I exchange the encode way in setCookie function like below:
function encode_setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
//use encodeURIComponent encode string and decodeURIComponent decode it.
var c_value=encodeURIComponent(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()+"; path=/project/");
document.cookie = c_name + "=" + c_value;
}
And use php echo it again.
It will output "你好" as i want read from cookie.
I try this save cookie by PHP code and read it by JS. it also work well.
Or encodeURIComponent/decodeURIComponent is enough for save and read cookie in PHP and JS.
It's about the string save in cookie is not in English problem thanks.
Had more better solution about it?
I find the solution about it.
jQuery cookie

It's an encoding problem. Read this page for an introduction to unicode:
http://www.joelonsoftware.com/articles/Unicode.html

Related

jQuery escape and UTF Contents problems

I'm submitting a form with jQuery .. and in order to manage the post() i have to escape() some text area..
my jQuery script:
var dataString = 'title=' + escape(title) + '&auth=' + auth + '&mainImage=' + mainImage + '&art_text=' + escape(art_text) + '&language=' + language + '&tags=' + tags + '&Status=' + Status + '&postDate=' + datepicker;
$.post("./Scripts/adminDoAddPost.php", dataString, function (data) {
//do something
});
now when i get the data .. i reaches my php script as
while it should be displayed as من نحن
is there a way to decode the whole string?
You can use urldecode and html_entity_decode as
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($_GET['whatever']));
$result = html_entity_decode($str,null,'UTF-8');

Proper Date Formatting for Cookies

So I am trying to do what I think is the impossible. I want to destroy all session cookies on browser or tab close, so I came up with a solution, I am just having small issues with JavaScript formatting. This does have to be time based, as when the next page reloads, it will put the timer for expiration for a year, so that other pages on my site don't delete the cookies as well. So until they navigate away from the site or close the tab, the cookie wont expire.
So The JavaScript starts like this:
var today = new Date();
today.setSeconds(today.getSeconds() + 5);
alert(today);
window.onunload = function(){
document.cookie = 'PHPSESSID=; expires=' + today;
};
So When I run this, it does not recognize the today variable, or I am not formatting today correctly.
All help is appreciated!
The today variable is out of scope. You need to create it in the unload, or pass it in the function().
window.onunload = function(today){
document.cookie = 'PHPSESSID=; expires=' + today.getTime();
};
OR
window.onunload = function(){
var today = new Date();
today.setSeconds(today.getSeconds() + 5);
document.cookie = 'PHPSESSID=; expires=' + today.getTime();
};
So I decided to destroy the session cookies with a small script.
This is what I came up with.
window.onunload = function(){
var today = new Date();
today.setSeconds(today.getSeconds() + 2);
var today1= today.toUTCString()
document.cookie =
'PHPSESSID=<?php echo $sesid; ?>; expires='+ today1 +'; path=/'
alert(document.cookie);
};
So This will set the destroy to 2 seconds, but if you navigate to any other page on my site, it will run this first.
var today2 = new Date();
today2.setSeconds(today2.getSeconds() + 10000);
var today3= today2.toUTCString()
document.cookie =
'PHPSESSID=<?php echo $sesid; ?>; expires='+ today3 +'; path=/';
This allows the session to be destroyed anytime you navigate away from the page.

Put results from .load() php file into Var

I have a page that loads in "get-results.php" which loads the results into a div. This works great. However, I also need to grab a variable from the php file that is being loaded, so that I can re-include it in the .load() url, this way I can have the php file have the previous result for comparison.
$(".results").load("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults);
In the "get-results.php" file, I have set
var theResults = "<?php echo $result; ?>";
Is there a way for me to access "var theResults" from the page that is loading it (outer page, not inner)? How can I get the Var from the file and put it into a Var in the outer file? Any pointers would be great thanks.
var theresults;
$(".results").load("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults,function() {
theresults=$(".results").html();
}));
obviously you have to wait the load is complete to get the new value for theresults.
otherwise, you can use $.get which seems more appropriate here :
$.get("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults,function(data) {
theresults=data;
$(".results").html(data);
}))
edit : if you are indeed sending more than this php var, you should send back a json (with your variables to be sent in an array then you echo json_encode($arrayofvar) then in the js you'll retrieve a json object of your var in the getJSOn callback
No you can not, i.e if you are sending other things with the variable.
If you are sending only that variable ( which I think you are not), then use the above answer.
Else
try this one:
in php file:
var theResults = "<?php echo $result; ?>";
echo "<input type='hidden' value='".$theResults ."' id='theResults'/>";
in javascript file:
$(".results").load("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults,function() {
theresults=$("#theResults").val(); alert(theresults);
}));

Store Form Values In Cookie

I've been trying to teach myself about cookies for the last couple of hours and how I would go about storing values from a few form fields into a cookie.
Im not having much luck, all the examples I've found havent been very helpful.
Should I generate them using PHP or JS?
Any feedback or a kick in the right direction would be highly appreciated!
http://jsfiddle.net/yucM7/
Thanks in advance!
Here's an example.
All you need is jQuery and cookie plugin
Keep in mind, there're couple of changes in html code.
$(document).on('submit', '#myForm', function() {
// serialize our form (get string containing field names and values)
dataString = $(this).serialize();
// set new cookie
$.cookie('formCookie', dataString);
return false;
});
$(document).on('click', '#getCookie', function() {
// get serialized string from cookie
cookieData = $.cookie('formCookie');
// if cookie exists continue
if (cookieData != null) {
// split cookieData string into an array of fields and their values
cookieArray = cookieData.split('&');
// go through each field and split it too to get field name and it's value
$.each(cookieArray, function(k, v) {
field = v.split('=');
// populate field with data
$('#myForm [name="'+field[0]+'"]').val(field[1]);
});
}
return false;
});
You can set cookie using Javascript by following (Refer http://www.w3schools.com/js/js_cookies.asp):
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
Setting cookie using Jquery: $.cookie("example", "foo");
Alternatively you can set your cookie from server by :
<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);

PHP get div position and store in session

I know how to get the position of a div using JS, I'm using Jquery's draggable script, and have an event that when the person stop dragging that it should get the location of the div (top and left):
document.getElementById('object').style.top;
document.getElementById('object').style.left;
now the question is how do I get those coordinates into a php session...?
Maybe the best (easiest) option is to store them in a cookie. The cookie can be read by PHP if you like, but even if you don't, you can still use the cookie again when the page is reloaded in the same browser.
If you want to store the value so it is kept too if the user logs in on another browser (or in a different session), you can store the cookie in a database.
On drop, make an Ajax request to a PHP script that puts them into $_SESSION.
This is the final solution I made thanks to GolezTrol :), I never knew you could make cookies using js
<script type="text/javascript">
function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
</script>
<script type="text/javascript">
$(function() {
$( "#object" ).draggable({ containment: ".body", scroll: false });
$( "#object" ).bind( "dragstop", function(event, ui) {
var l = document.getElementById('object').style.left;
javascript: setCookie('objectl', l, 365);
var t = document.getElementById('object').style.top;
javascript: setCookie('objectt', t, 365);
});
});
</script>

Categories