Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: pull variable from href

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default pull variable from href

    i have a link that looks like this
    Code:
    href="#/work/companyName1"
    is it possible to read variables from this url like
    Code:
    if(#/work="companyName1" ){$link = "http://www.companyName1.com";}
    is it possible to somehow add a ?id=someVariable

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    I am confused to about your question. Do you mean something like this:
    PHP Code:
    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.

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    What's the relationship between the variable and the link or how is the link determined?
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    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
    Code:
    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
    Code:
    somelink.php?var=123otherVar=456

  5. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    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?

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    href="#/work/page1"
    would scroll to:

    HTML Code:
    <a name="/work/page1">Optional Text</a>
    if found, to the top of the page if not. If you add a query string:

    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>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</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 Code:
    <?php
    if(isset($_GET['bob'])){
        echo 
    $_GET['bob'];
    }
    ?>
    <a href="?bob=Fred's Friend#/work/page1">GoTo /work/page1</a>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    thank you for replying. i also need to mention that i found this piece of code and implemented on the site
    Code:
    $.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

    Code:
    <?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
    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>
    Last edited by ggalan; 09-23-2010 at 04:13 AM.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Like I said, to even have a chance of working, they would need to be 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>
    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:
    href="index.php?id=2#/work/pepsi"
    Not:

    Code:
    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:

    PHP Code:
    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.
    Last edited by jscheuer1; 09-22-2010 at 04:48 PM. Reason: English usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    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?
    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?

  10. #10
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    since the url is unique, can i write a condition based on the changing url? how would one do that??

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •