View Full Version : pull variable from href
ggalan
09-22-2010, 03:22 AM
i have a link that looks like this
href="#/work/companyName1"
is it possible to read variables from this url like
if(#/work="companyName1" ){$link = "http://www.companyName1.com";}
is it possible to somehow add a ?id=someVariable
fastsol1
09-22-2010, 04:07 AM
I am confused to about your question. Do you mean something like this:
if(#/work="companyName1" ){$link = "http://www.companyName1.com/file.php?id=someVariable";}
Then yes but the link has to have a file name after the domain name to reference to then you can add several ?id=someVariable separated by &
If that is not what you mean please give some more examples of what you think it should look like.
bluewalrus
09-22-2010, 04:14 AM
What's the relationship between the variable and the link or how is the link determined?
ggalan
09-22-2010, 04:23 AM
my href links do not have the ".php" suffix because the site does not have html container , rather it is a wide scrolling page using javascript so the href is simply
href="#/work/page1"
but i would like to pass some sort of variable from page to page and th only way i know how is to use the
somelink.php?var=123otherVar=456
ggalan
09-22-2010, 05:20 AM
if its impossible to read href without a ".php" since i cannot add "?var=123"
can i read the entire url and work from there?
jscheuer1
09-22-2010, 05:39 AM
When an href starts with a hash (#), unless the server is parsing that into something else as the page is served, it's not a link to another page, rather a link to a named anchor on the same page.
So:
href="#/work/page1"
would scroll to:
<a name="/work/page1">Optional Text</a>
if found, to the top of the page if not. If you add a query string:
<?php
if(isset($_GET['bob'])){
echo $_GET['bob'];
}
?>
<a href="#/work/page1?bob=Fred's Friend">GoTo /work/page1?bob=Fred's Friend</a>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<a name="/work/page1?bob=Fred's Friend">/work/page1?bob=Fred's Friend</a>
It will go to the named anchor at the bottom, and the query string will appear in the address bar, but the server side code at the top will not echo anything.
If you want some other type of behavior, you either need javascript or some other type of syntax. Since to be valid the query string must always precede the hash in a URL, you can do:
<?php
if(isset($_GET['bob'])){
echo $_GET['bob'];
}
?>
<a href="?bob=Fred's Friend#/work/page1">GoTo /work/page1</a>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<a name="/work/page1">/work/page1</a>
Now the query will appear in the address bar, and the page will scroll to the named anchor (which is different here, it has no query in it), and the server side code will echo:
Fred's Friend
at the top of the page.
ggalan
09-22-2010, 02:55 PM
thank you for replying. i also need to mention that i found this piece of code and implemented on the site
$.ajax({
beforeSend: function () {}, data: {id:id,gateway:'detail'}, success: on_query_success, error: function () {}, url: 'switch.php'
});
the file switch.php is called when one of the anchor buttons are pressed and its in this file that i want to write a condition so that it loads dynamic markup such as
<?php
if($id=="1" ){$link = "http://coke.com"; $title="Coke"}
else if($id=="2" ){$link = "http://pepsi.com"; $title="Pepsi"}
echo "<div id='divOne'><h1>$title</h1><h2 class='link'>\n";
echo "</div>\n";
echo "<div id='someClasss'>\n";
echo "<div class='someClass'><img src='images/client1/a.jpg' alt='' /></div>\n";
echo "<div class='someClass'><img src='images/client1/b.jpg' alt='' /></div>\n";
echo "<div class='someClass'><img src='images/client1/c.jpg' alt='' /></div>\n";
echo "<div class='someClass'><img src='images/client1/d.jpg' alt='' /></div>\n";
echo "</div>";
?>
but these variables do not register
<li>
<a href="#/work/coke?id=1" rel="coke">coke</a>
</li>
<li>
<a href="#/work/pepsi?id=2" rel="pepsi">pepsi</a>
</li>
jscheuer1
09-22-2010, 04:38 PM
Like I said, to even have a chance of working, they would need to be like:
<li>
<a href="?id=1#/work/coke" rel="coke">coke</a>
</li>
<li>
<a href="?id=2#/work/pepsi" rel="pepsi">pepsi</a>
</li>
Otherwise they are not valid query strings, and will not register as anything other than as part of the hash string. The hash always comes after the query. If this were a full URL, to be a valid query, it would be something like:
href="index.php?id=2#/work/pepsi"
Not:
href="index.php#/work/pepsi?id=2"
as you are more or less currently doing it.
This should be independent of any javascript and will work just fine, unless the javascript cancels or in some other way renders moot the default action of these links.
Also, your PHP code here:
if($id="1" ){$link = "http://coke.com"; $title="Coke"}
else if($id="2" ){$link = "http://pepsi.com"; $title="Pepsi"}
will not pick up the query $_GET data anyway, unless your server is really lax about how it does this, or you have a more formal method for getting the $_GET data into the $id variable higher up on the page that you are not showing in your post.
ggalan
09-22-2010, 04:48 PM
Also, your PHP code here:
if($id="1" ){$link = "http://coke.com"; $title="Coke";}
else if($id="2" ){$link = "http://pepsi.com"; $title="Pepsi";}
will not pick up the query $_GET data anyway, unless your server is really lax about how it does this, or you have a more formal method for getting the $_GET data into these variables higher up on the page that you are not showing in your post.
would i need this on top?
$id = $_GET['id'];
i tried placing the "?id=1" before the hash and now the whole thing doesnt work. any suggestions on how to make that "switch.php" page dynamic?
ggalan
09-22-2010, 04:50 PM
since the url is unique, can i write a condition based on the changing url? how would one do that??
jscheuer1
09-22-2010, 05:42 PM
would i need this on top?
$id = $_GET['id'];
In a PHP code block, yes. And with error checking via isset() if your server requires that. Most are not that strict.
i tried placing the "?id=1" before the hash and now the whole thing doesnt work. any suggestions on how to make that "switch.php" page dynamic?
since the url is unique, can i write a condition based on the changing url? how would one do that??
To diagnose the javascript part of it, I would need a link to the live page. However, even at that it would be time consuming to mock up because of the AJAX involved. To fix up the PHP part, I would need to see all of the PHP code employed.
And I only have a vague idea of you objective here. It would help if you could be more clear on that.
You might not need so much code. I get the feeling that you might be over complicating things. Like if you want to use AJAX, let it do most of the work and use less PHP and normal HTML. Or perhaps consider doing it all via PHP.
One thing I did say is that if the link doesn't fire (often the case where javascript is involved), it won't matter if the query string is valid or not, at least insofar as what would normally happen in PHP and HTML on the page.
This from my earlier post however does work:
<?php
if(isset($_GET['bob'])){
echo $_GET['bob'];
}
?>
<a href="?bob=Fred's Friend#/work/page1">GoTo /work/page1</a>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<a name="/work/page1">/work/page1</a>
and answers your original question.
I might add that if the javascript code relied upon an invalid query string in order to parse the link, of course it will no longer work if you change it to a valid one.
Another thing I didn't mention before because it's unclear to me what your $.ajax() function is doing is that it appears perhaps to be a POST request. If so, the query string (GET data), even if valid (perhaps especially if valid), might not get passed to the requested page.
ggalan
09-22-2010, 05:46 PM
thank you for all the communication, after all of this i think what i need is something like this
http://www.stoimen.com/blog/2009/04/15/read-the-anchor-part-of-the-url-with-php/
however this posts method doesnt seem to work, i will post files and if you have a moment, would be greatly appreciated
jscheuer1
09-23-2010, 07:44 PM
I have serious doubts about that method's applicability in this and pretty much any situation. You cannot call it via AJAX to get the value because the javascript on it cannot be used under an AJAX request, it requires a reload of the page so sort of defeats the purpose of doing anything unobtrusively, because it's using the hash, many requests to it will not be passed to the server anyway, and owing to its reliance upon cookies adds layers of uncertainty as to whether or not it will perform as expected.
That said, it doesn't work even as stated on the page because the code is written using invalid characters and is poorly thought out as well. If it would be of any help, here's a working version:
<script type="text/javascript">
(function(){
var h = location.hash;
h = h.substring(h.indexOf('#') + 1);
document.cookie = 'anchor=' + h;
<?php
$c = isset($_COOKIE['anchor'])? $_COOKIE['anchor'] : '';
$c = "'" . $c . "'";
?>
if(h !== <?php echo $c; ?>){
location.reload();
}
})();
</script>
<?php
$c = isset($_COOKIE['anchor'])? $_COOKIE['anchor'] : '';
echo $c;
?>
Perhaps if you ran it in a hidden iframe . . . But then there would be caching issues added to those already mentioned. These could perhaps be worked around, and then timing would become critical as to when this value became accessible to the rest of your code, still not solving many of the other issues.
But I would strongly advise you to use a GET request with a valid query string for this, it would be so much simpler.
ggalan
09-23-2010, 10:16 PM
thank you for all the help, will go over all the notes this evening.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.