How to use cookie in ajax file - php

I have code that uses ajax in order to pull and print data from DB.
ajax run PHP file that does that. This file read data from the cookie and use it, but because I'm using ajax and run file "away" from the website I don't think it can call to cookies...
how can I read cookie inside the ajax file?
AJAX FILE
switch ($_COOKIE['user_lang'])
{
case "ES":
$langDir = SPANISH
break;
case "EN":
$langDir = ENGLISH
break;
}

You can try it .
function readCookie(cname) {
var name = cname+"=";
var cookies = decodeURIComponent(document.cookie).split(';');
var cval = [];
for(var i=0; i < cookies.length; i++) {
if (cookies[i].trim().indexOf(name) == 0) {
cval = cookies[i].trim().split("=");
}
}
return (cval.length > 0) ? cval[1] : "";
}
switch (readCookie(['user_lang']))
{
case "ES":
$langDir = SPANISH
break;
case "EN":
$langDir = ENGLISH
break;
}

Related

How to use SQL-View with PQGrid in combination of saving to db

PQGrid: How I can save changes from grid to DB when I have a View as data source
I have read all examples from the homepage/forum etc. Furthermore I tried to transfer from jqGrid / other Gridtools to PQGrid. No chance.
The code below shows the PopUp-Editing opportunity and when I change any value, e.g. Prio = Priority, the values in the grid are edited, but when I reload the data, I got the original value from database.
Likewise I have created a PHP-file (Vormerkungen_Grid_Speichern.php) for parameter 'editurl' by a template, but nothing happens. He don't call this special file.
<?php
// FILENAME: Vormerkungen_Grid_Speichern.php
// Datei dient nur zum Speichen von Veränderungen im Grid
require ('./system_connector_mysql.php');
$artikelnr = $_POST['ArtikelNr'];
$aufgabe = $_POST['Aufgabe'];
$prio = $_POST['Prio'];
echo $_POST['ArtikelNr'];
echo $_POST['Aufgabe'];
echo $_POST['Prio'];
switch ($_POST["oper"]) {
case "add":
// do mysql insert statement here
break;
case "edit":
$query = "UPDATE tab_vormerkungen SET Aufgabe = '$aufgabe', Prio=$prio WHERE ArtikelNr=$artikelnr";
mysql_query($query) OR ('Vormerkungen_Speichern.php - Zeile 13 |' . mysql_error());
$datei = fopen('Vormerkungen_Speichern.txt', 'w');
fwrite($datei, $query);
fclose($datei);
break;
case "del":
// do mysql delete statement here
break;
}
?>
// FILENAME: Vormerkungen.php
function editRow() {
var rowIndx = getRowIndx();
if (rowIndx != null) {
var row = $grid.pqGrid('getRowData', { rowIndx: rowIndx });
var aufgabeAlt = row.Aufgabe;
var $frm = $("form#crud-form");
$frm.find("input[name='artikelnr']").val(row.ArtikelNr);
$frm.find("input[name='aufgabe']").val(row.Aufgabe);
$frm.find("input[name='prio']").val(row.Prio);
$frm.find("input[name='VFG']").val(row.VFG);
$("#popup-dialog-crud").dialog({ title: "Vormerkung " + row.ArtikelNr + " bearbeiten", buttons: {
Update: function () {
//update row.
row = [];
row.ArtikelNr = $frm.find("input[name='artikelnr']").val();
row.Aufgabe = $frm.find("input[name='aufgabe']").val();
row.Prio = $frm.find("input[name='prio']").val();
// Umsetzung im Grid
$grid.pqGrid('updateRow', { rowIndx: rowIndx, row: row, checkEditable: false });
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
}).dialog("open");
}
}
Thank you #Tony Tmov (Sarcasm). PQGrid has a better license for me, that is the reason why I have choose PQGrid.
I think my biggest problem is the data source. If I didn't have a view as a data source, I wouldn't have a problem with storing it in the database.
But I also have no other choice with the data source.

php and js-ajax acting funny (explanation inside)

I am having trouble with this ajax. The return is funny. The switch seems to always evaluate as though #about is true. And include the rest of the switch statement in the #page variable. For instance my PHP code prints this (as well as the #about that it is supposed to) the code at the bottom should clarify what I mean.
To recap it echoes everything past the first $page ='
on my page I see this below what it is supposed to echo
';
break;
case '#register' :
$page = 'k';
break;
case '#contact' :
$page = 'a';
break;
case '#a' :
$page = ' b';
break;
case '#b' :
$page = '<p> c</p>';
break;
default;
echo "def";
}
echo $page;
Not to mention it wont even work for #contact, or #a, #b...etc. I am not sure why. Regardless of the url passed it appears as though #about is called with what it returns (even though it returns about and more).
I would really appreciate some help! Thank you
Here is my code:
the js =]
$(document).ready(function () {
//highlight the selected link
$('a[href=' + document.location.hash + ']').addClass('selected');
//Seearch for link with REL set to ajax
$('a[rel=ajax]').click(function () {
//grab the full url
var hash = this.href;
//remove the # value
hash = hash.replace(/^.*#/, '');
//clear the selected class and add the class class to the selected link
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
//hide the content
$('#content').hide();
console.log(this.href);
//run the ajax
getPage();
//cancel the anchor tag behaviour
return false;
});
});
function pageload(hash) {
//if hash value exists, run the ajax
if (hash) getPage();
console.log("k");
}
function getPage() {
//generate the parameter for the php script
var data = 'page=' + encodeURIComponent(document.location.hash);
$.ajax({
url: "loader.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
//add the content retrieved from ajax and put it in the #content div
$('#content').html(html);
//display the body with fadeIn transition
$('#content').fadeIn('slow');
}
});
}
the php =]
switch($_GET['page']) {
case '#about' :
$page = ' HTML stack overflow formats it, anyways';
break;
case '#register' :
$page = 'k';
break;
case '#contact' :
$page = 'a';
break;
case '#a' :
$page = ' b';
break;
case '#b' :
$page = '<p> c</p>';
break;
default;
echo "def";
}
echo $page;
thank you again!
Also in the php file, try putting this on top of the script:
header("Content-Type: text/html");

need help to send variable from javascript to PHP

i try to send some data from html to process page. i want to do this use javascript.
the javascript is:
$('#search').click(function(){
var param = serialize({
action:"searchmodelqp",
jhead:"aaData",
month:$("#search_month").val(),
year:$("#search_year").val(),
export:"excel"
});
$('#link2excel' ).replaceWith("<div id='link2excel'><a href='shows_merchan.php?" + param + "' target='_blank'>Export result as Excel file</a></div>");
});
and i send it to this:
if(getVar('export')=='excel'){
$expexcel = 'excel';
} else {
$expexcel=0;
}
switch(getVar('action')){
case 'searchmodelqp':
modelqp(getVar('jhead'),getVar('month'),getVar('year'),getVar('export'));
break;
}
function modelqp($jsonhead,$month, $year,$export){
$Month = mysql_real_escape_string($month);
$Year = mysql_real_escape_string($year);
switch($jsonhead){
case 'aaData':
//i put mysql query here
break;
}
if($export==0) {
//do something
} else {
//do something
}
how do i do to make it clear?because i have no result for this(script do nothing).
change your data as below
data:action="searchmodelqp&jhead="aaData"&month=$("#search_month").val()&year=$("#search_year").val()&export="excel"
i change at this part:
var param = jQuery.param({action:"searchmodelqp",jhead:"aaData",month:$("#search_month").val(),year:$("#search_year").val(),export:"excel"});

element in form is empty in IE

The following code is working in FF but not in IE. There is no value of document.myform["parameters"].value in IE.
The code:
function form_elements()
{
if(document.myform.elements["parameters"].value == '')
{
myform_values = ......
}
}
<form method="post" name="myform">
<div id="parameters_info">
<select id="parameters" name="parameters" onfocus="e =form_elements();" >
<?php
foreach($params_name as $name)
{
if($name == $param_posted)
{
echo "<option selected>$name</option>";
}
else
{
echo "<option>$name</option>";
}
}
?>
</select>
</div>
</form>
I tried also document.myform["parameters"].value but the value is empty.
My options are like:
<option>1234</option>
<option>234a</option>
And i want that the value of the option will be in the function.
10x,
Ronny
You need to give a value to each option. Also, you cannot get the selected option using the value of the select tag. You need to use:
var selectedOption = document.myform.parameters.options[document.myform.parameters.options.selectedIndex].value;
<option>1234</option>
1234 - is a text of an option.
<option value="1234">1234</option>
Now 1234 - is a text and value.
That should be:
document.forms["myform"].elements["parameters"].options[document.forms["myform"].elements["parameters"].selectedIndex].text
...or you can use a function like this, which returns an array of values from any field type:
function getFieldValueArray(fld, frm) {
// fld may be a field object or a field name
// frm may be an index or a name, defaults to document.forms[0] when needed and not supplied
//function returns an array of values
var form = frm ? document.forms[frm] : document.forms[0];
var field = (typeof(fld) == "object") ? fld : form.elements[fld];
var valueArray = new Array();
var multinode = (field.length>1) ? true : false;
var type = field.type;
if (!type) {
type = field[0].type;
}
switch(type) {
case "radio": //fall-through intentional
case "checkbox":
if (multinode) {
for (i=0; i<field.length; i++) {
if (field[i].checked) {
valueArray.push(field[i].value);
}
}
}
else {
if (field.checked) {
valueArray.push(field.value);
}
}
break;
case "select-one": //fall-through intentional
case "select-multiple":
var options = field.options;
for (i=0; i<options.length; i++) {
if (options[i].selected) {
valueArray.push(options[i].value ? options[i].value : options[i].text);
}
}
break;
case "text": //fall-through intentional
case "file":
valueArray.push(field.value);
break;
case "hidden":
valueArray = field.value.split(";");
break;
case "textarea":
valueArray = field.value.split("\n");
default:
alert("Field not found -- atGetField(" + fld.toString() + ")");
}
return valueArray;
}
The function comes in rather handy when you can't be certain what type the field may be (for instance, if the field is an editable type under some circumstances and hidden under others), but once written it's an easy fallback, particularly for selects, radios and checkboxes.

Slidesshow in javascript

I am doing a slidesshow image in javascript:
function ImageLoop( images, speed, width, height ,inc){
this.img = images;
this.speed = speed;
this.width = width;
this.height = height;
this.interval = false;
this.play = false;
if (inc > this.img.length-1){
this.index = this.img.length-1;
} else {
this.index = inc;
}
this.returned = false;
function preloadImages(){
for (i in this.img){
var _i = new Image();
i.src = this.img[i];
}
}
preloadImages();
thisObj = this;
function onInterval( ){
thisObj.next();
}
function set(index) {
var im = document.getElementById("imageloop");
if (index > this.img.length-1){
var idx = this.img.length-1;
} else {
var idx = index;
}
im.src = this.img[idx];
}
function next( ){
if (this.play){
if (next == this.img.length-1){
this.returned = false;
}
if ((this.index == this.img.length-1) && !this.returned){
this.returned = true;
return;
}
var next = (this.index < this.img.length-1) ? ++this.index : 0;
this.set(next);
}
}
function start( ){
this.play = true;
if (!this.interval){
this.interval = setInterval(onInterval, this.speed);
}
}
function stop( ){
this.play = false;
if (this.interval){
clearInterval(this.interval);
this.interval = false;
}
}
function changeSpeed( adjust ){
this.speed += adjust;
clearInterval(this.interval);
this.interval = setInterval(onInterval, this.speed);
}
function serendre( place ) {
switch (place){
case "beginning":
this.stop();
this.set(0);
break;
case "left":
this.stop();
var next = (this.index > 0) ? --this.index : this.img.length-1;
this.set(next);
break;
case "right":
this.stop();
var next = (this.index < this.img.length-1) ? ++this.index : 0;
this.set(next);
break;
case "end":
this.stop();
this.set(this.img.length-1);
break;
}
}
// Setup
var d = document.getElementById("il");
var image = "<img src='" + this.img[this.index] + "' id='imageloop' width='" + this.width + "' height='" + this.height + "' />";
d.innerHTML = image;
// Add Functions
this.preloadImages = preloadImages;
this.set = set;
this.next = next;
this.start = start;
this.stop = stop;
this.changeSpeed = changeSpeed;
this.serendre = serendre;
}
In IE is working fine but on other I have problem. Like when I set the index to a other number and start it in Ie working but on other it start from index 0. I don't know what is the problem of it. Can someone help me with that ?
I do not know your goal in what you are doing. But if you just need that functionality (image slideshow), consider using existing javascript library and solution.
For jQuery it can be:
http://sorgalla.com/projects/jcarousel/
http://gmarwaha.com/jquery/jcarousellite/
http://thomlx.free.fr/jquery/jquery_carousel.htm
I really like Lytebox. It has the slideshow feature you want (as well as others) and is very easy to use. It also works in IE6, etc.
The best approach to creating JS code that is compatible with all somewhat modern browsers (starting with IE6 and Firefox2) is to use a javascript library that takes care of those things for you. jQuery is an excellent library that has many plugins, including slideshows etc... You can find more information at link text and a bunch of great examples of UI features such as slideshows at link text
all the best,
yuval

Categories