i have a link that looks like this
is it possible to read variables from this url likeCode:href="#/work/companyName1"
is it possible to somehow add a ?id=someVariableCode:if(#/work="companyName1" ){$link = "http://www.companyName1.com";}
Printable View
i have a link that looks like this
is it possible to read variables from this url likeCode:href="#/work/companyName1"
is it possible to somehow add a ?id=someVariableCode:if(#/work="companyName1" ){$link = "http://www.companyName1.com";}
I am confused to about your question. Do you mean something like this:
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 &PHP Code:if(#/work="companyName1" ){$link = "http://www.companyName1.com/file.php?id=someVariable";}
If that is not what you mean please give some more examples of what you think it should look like.
What's the relationship between the variable and the link or how is the link determined?
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
but i would like to pass some sort of variable from page to page and th only way i know how is to use theCode:href="#/work/page1"
Code:somelink.php?var=123otherVar=456
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?
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:
would scroll to:Quote:
Code:href="#/work/page1"
if found, to the top of the page if not. If you add a query string:HTML Code:<a name="/work/page1">Optional Text</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.PHP Code:<?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>
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:
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:PHP Code:<?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>
at the top of the page.Quote:
Fred's Friend
thank you for replying. i also need to mention that i found this piece of code and implemented on the site
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 asCode:$.ajax({
beforeSend: function () {}, data: {id:id,gateway:'detail'}, success: on_query_success, error: function () {}, url: 'switch.php'
});
but these variables do not registerCode:<?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>";
?>
Code:<li>
<a href="#/work/coke?id=1" rel="coke">coke</a>
</li>
<li>
<a href="#/work/pepsi?id=2" rel="pepsi">pepsi</a>
</li>
Like I said, to even have a chance of working, they would need to be like:
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:Code:<li>
<a href="?id=1#/work/coke" rel="coke">coke</a>
</li>
<li>
<a href="?id=2#/work/pepsi" rel="pepsi">pepsi</a>
</li>
Not:Code:href="index.php?id=2#/work/pepsi"
as you are more or less currently doing it.Code:href="index.php#/work/pepsi?id=2"
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:
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.Quote:
PHP Code:if($id="1" ){$link = "http://coke.com"; $title="Coke"}
else if($id="2" ){$link = "http://pepsi.com"; $title="Pepsi"}
would i need this on top?Quote:
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.
Code:$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?
since the url is unique, can i write a condition based on the changing url? how would one do that??
In a PHP code block, yes. And with error checking via isset() if your server requires that. Most are not that strict.
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:
and answers your original question.Quote:
PHP Code:<?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>
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.
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/...-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
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:
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.PHP Code:<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;
?>
But I would strongly advise you to use a GET request with a valid query string for this, it would be so much simpler.
thank you for all the help, will go over all the notes this evening.