jquery click handler language set - php

I have 3 links on page index.php:
<div id="languages">
<a href="index.php?page=test&lang=en" id="flag">
<img src="./images/flags/en.png" class="flag_off" alt="en" title="en">
</a>
<a href="index.php?page=test&lang=sk" id="flag">
<img src="./images/flags/sk.png" class="flag_off" alt="sk" title="sk">
</a>
<a href="index.php?page=test&lang=cz" id="flag">
<img src="./images/flags/cz.png" class="flag_on" alt="cz" title="cz">
</a>
</div>
each of them is passing lang parameter and on same page (index.php). Then I have variable
$_GET['lang']
which will call function for changing language:
$obj->change_language($_GET['lang']);
Q: How can I do this by using jquery get() function without showing parameter in url of index.php page?
(Pass variable $lang through jquery)
I don't like URL with parameters after calling function for example:
index.php?page=test&lang=sk
I suppose using .click() function as handler.
EDIT:
I have something like this:
$(document).ready(function(){
$("#flag").click(function(){
$.get('index.php',
{ lang: "VARIABLE LANG FROM HREF" }
);
});
});
How to parse sk from href index.php?page=test&lang=sk part ?

If your problem is the parameter in the URL, you could try setting it with Ajax (http://api.jquery.com/jQuery.ajax/) and than reloading the page. I don't think you could get around refreshing since your content should be changed to the selected language as well?

$(".flag").click(function(event) {
event.preventDefault();
$.get(this.href, function(data) {
location.reload();
});
});

You don't need jQuery at all do to this. You can do something like
<div id="languages">
<a href="changeLanguage.php?lang=en">
<img src="/flags/en.png" alt="en" title="en">
</a>
<a href="changeLanguage.php?lang=sk">
<img src="/flags/sk.png" alt="sk" title="sk">
</a>
<a href="changeLanguage.php?lang=cz">
<img src="/flags/cz.png" alt="cz" title="cz">
</a>
</div>
Then, in this changeLanguage.php, you do your $obj->change_language($_GET['lang']); (where you should check/cast the value of the lang parameter), then, you can redirect the user to the referer page with something like
$url = (string) $_SERVER['HTTP_REFERER'];
if (0 === strlen($url)) {
// redirect to the home page if there is no referer
$url = '/';
}
header('Location: '.$url);
This way, you don't need any Javascript code to this. I think this is important in some ways:
you only do one HTTP request instead of two with the AJAX solution,
when the user click, the browser reacts immediately: the request is sent right after the click so the "loading icon" of the browser is showing that something is happening
this effect of action->reaction is something important in the User Experience, with the AJAX solution you have to wait for the first request to finish to trigger the "loading icon",
the location.reload() used with the AJAX solution can show up a dialog like Would you like to submit the form again ? if the user has arrived to this page with a POST request, and he might not understand what's the relation between its action (change the language) and this "error" message, which leads to a degraded User Experience too,
if your Javascript is broken is some way, those link might not work at all if your page to change the language only change the language. Let's say the page index.php?page=test&lang=en is doing something like $obj->change_language($_GET['lang']); exit;, because this page's purpose is only to change the language. Then if the Javascript code is not triggered for any reason (the code is broken, handlers are not bound, the user open up the page in a new tab), then the page loaded will be a blank page. And if you handle this case, then the "change language page" would look like a normal page, that's ok but it means that you'll do two HTTP requests with a "complete" response (one for changing the language, one other after the location.reload), and this leads to the first point,
use Javascript only where it's needed :)
What do you think ?

Related

transfer variable between frames

The questions similar to that I found a lot but still can not realize what I need. I have a page.php that loaded in the block din_content with function .load
Page.php
<div id="ess">
<div id="top_ch">
<h1>Ess</h1>
</div>
<div id="ch">
<iframe name="users" width="220" height="510" align="left" src='ess/users.php' id="userch"></iframe>
<iframe name="text" width="450" height="405" src='ess/text.php'></iframe>
</div>
In page I have two Iframes. So I have two problems. First in main page where that iframes loaded I have Jquery function
$(document).on('click','.link_pro',function () {
//some actions
});
How to make that that function also worked in iframe?
Second, In the first Iframe I have two links. So what I need is that depend of what link you click on, the variable 'active' changed and depend on the value of that variable changed the second iframe. In example, you click on the first link and variable 'active' =1 than it transfered to the second iframe (may be even transfer to the parent and try to send to the second iframe trough method Get) where depend of the value displayed a requested result. I try to realize it with jquery and php. But still didn't get the result. The most problem to get a result from clicking on links in the first Iframe.
If you own the iframes sources and they belong to the same domain as the top window, then, for attaching events, you could probably do something like:
jQuery("iframe").load(function(){
jQuery(this).contents().find(".link_pro").on("click", function(){
});
});
The code above will attach the click event to your iframe's content (.link_pro).
Then, for the second question, if you have, for example, in your top window:
var myTopVar; //This belongs to top window scope
From your iframe, you can access:
top.myTopVar;
I think that should be it but, again, both your top window and iframe should share the same domain (document.domain). If not, you will face same origin policy problems.

PHP Referrer of Referrer (Recursively)

I am looking into sending the user back two (or maybe more) pages from where they came. What works so far is
<strong>
<a href="javascript:history.go(-3)">
Click here to go back to the view
</a>
</strong>
However, the [history] page being called does not refresh to show the changes.
So I have the idea of referring the referrer. PHP gives me $_SERVER['REFERER'] to use, which is OK but only up to one level. How do I get the referrer (of referrer...) of $_SERVER['REFERER'] ?
As far as I know, you cannot do that in PHP. The simple answer is no, because PHP is server-side, and gets just the current referrer, while Javascript is client-size, running on the browser, which actually does have 2 or more history steps.
Consider re-thinking why you want this to happen. You can never guarantee that the 2-step back referrer (even the last one) is still in your site.
Pure JS. No PHP.
Modify your existing code like this.
<strong>
<a href="javascript:window.name='autoLoad';history.go(-3)">
Click here to go back to the view
</a>
</code>
Now add this JS to the page, where you are being redirected. say test.html when you click history.go(-3)
function autoLoad(){
if (window.name=='autoLoad') {
location.reload();
window.name='';
}
}
and in the body tag add this
<body onload="autoLoad()">
In server variable $_SERVER['REFERER'] is only the last referrer. If you want to use previous you have to save them to session. The problem is that you can't find out which browser tab initialized the request - so your history would be mixed for all tabs.
Solution would be using JS to force the browser to reload page.
You could do something like this, or use cookies if you like them more, but you don`t need PHP for this.
<input type="hidden" id="reload" value="0">
<script type="text/javascript">
window.onload = function(){
var el = document.getElementById("reload");
if(el.value != "0")
{
window.location.reload();
}
el.value="1";
}
</script>

Hide the fact a page has been sent a form on reload

I have used a form to submit an image id to the another page. I have done this rather than r than via url because it makes the url cleaner. However, when the user now reloads, it asks if they want to submit the form again. The won't know they have submitted a form, as far as they are aware they have simply clicked an image and been brought to a page that displays that image bigger.
Is there any way around this, that saves the image id the hidden form field has sent via the form, yet refreshes the page for the user?
HTML
<a class="gallery-img span4 js-img-submit" href="#">
<img src="img.jpg"/>
<form action="/image" method="post">
<input type="hidden" value="<?=$img['id']; ?>" name="image_id"/>
</form>
</a>
JQUERY
$('.js-img-submit').click(function(){
$(this).find('form').submit();
})
PHP
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : false;
Somehow the parameter needs to be send to the image page.
GET: You dont want that.
POST: You use that, but causes the refresh problem.
SESSION: Will be hidden, but cause troubles, when he opens multiple images, and then refreshing the first page.
So, i would to the following:
Use the hashtag, and load the image via javascript then. finally remove the hashtag:
MainPage:
<a class="gallery-img span4 js-img-submit" href="/image#<?=$img['id']; ?>">
<img src="img.jpg"/>
</a>
ImagePage:
<script language="javascript">
$(document).ready(function(){
var imgid = document.location.hash.slice(1);
document.location.hash = ""; //Remove
loadImage(imgid);
})
</script>
loadImage then needs to be the required javascript function.
The user might notice it for a split second. IF the user is not allowed to see, then your only way would be to use the PHP Session with seperate variables for every opened image-window.
You can do something like this:
$('.js-img-submit').click(function()
{
$.post("image.php", {id: $(this).find('input').val()});
})

Calling JQuery .load after html redirect

I am writing an application that I want to make more user friendly by removing the amount of clicks needed to navigate.
At the moment pages are loaded like so:
<a class='pageloader' name='page1.html'>Page 1</a>
<script>
$('.pageloader').click(function(){
// Load the page that the user clicked on by taking the name property and appending it to includes/
$('.content').load("pages/" + this.name);
});
</script>
Basicly this takes the name of the clicked link and replaces the content div's content with whatever is inside the file that matches the name property.
Now my only problem is that I can't redirect people to pages using HTML because the only page that has proper styling is index.php.
Obviously I am now redirecting people to index.php after an action is finished, but I would like to redirect them to a specific page.
I've thought about calling
$('.content').load('pages/edit-plein.php');
(This code is inside a .php script that writes to a file)
But that only gives me an error since it cannot find the .content div.
Would anyone know a good method to redirect a user to the page I want?
As far as i understand you want to make shure the right content gets loaded (inside that div) when you share the link to a specific subpage on your site, but can only share a link of your index.php because of its styling.
I would suggest you add a variable to your URL, i.e. like
index.php?page=edit-plein
then get that var with PHP and create a JS call to your pageloader, like this:
<?php
if ( $_GET['page'] != '' ) {
echo '<script>$(".content").load("pages/'. $_GET['page'] .'");</script>';
}
?>
This is not a good way for links:
<a class='pageloader' name='page1.html'>Page 1</a>
Maybe you can try this:
Page 1
Page 2
And JS must be like this:
$('.pageloader').click(function(){
$('.content').load("pages/" + $(this).attr('id') + ".html");
});
I hope this solves your problem.

Track dynamic button with href and target_blank

I need to track every time a user clicks on a dynamic button. Also I need to know what product is he clicking. Now I have this button:
<div class="buy">
<a target="_blank" href="<?php echo $this->product['from']; ?>">
<img src="http://example.com/data/images/buy.jpg" alt="buy">
</a>
</div>
Href sends the user to another site to buy the product. How can i track this on PHP?
If the href is linking to another page that is not on your own server, you can go with an ajax-solution, like #coder1984 proposed, or you can create a proxy php script. That means:
the user clicks the link to the proxy, sending in the product URL
like: href="myproxy.php?url=<?php echo $this->product['from']; ?>"
the proxy gets the URL, checks it and updates a database, the session, a text file
afterwards it redirects the user to the actual URL
Client-side dynamic user actions can be determined using Javascript/Jquery. Here's how its done in Jquery,
$(document).ready(function() {
$('.buy').click(function(){
alert('This is clicked');
});
});
To track in PHP, you can pass the value(buttons clicked) to PHP using Jquery AJAX. Here's the manual.

Categories