#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    England, UK
    Posts
    83
    Rep Power
    15
    Hello,
    I have a piece of javascript here, can anyone 'translate' it into PHP? I would if I could but i don't understand this javascript.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    function addvar(href, variable, value)
    {
    if(href.indexOf('?') != -1)
    {
    return href + '&' + variable + '=' + value;
    }
    else
    {
    return href + '?' + variable + '=' + value;
    }
    }

    for(i = 0; i < document.anchors.length; i++)
    {
    with(document.anchors[i])
    {
    href = addvar(href, 'SID', '<? echo $SID; ?>');
    }
    }
    for(i = 0; i < document.forms.length; i++)
    {
    with(document.forms[i])
    {
    action = addvar(action, 'SID', '<? echo $SID; ?>');
    }
    }
    [/code]

    Thank you

    Girish Gupta
    webmaster@musicgoeson.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Den Laeghe Landen
    Posts
    107
    Rep Power
    15
    [QUOTE]Originally posted by girish_gupta:
    [B]Hello,
    I have a piece of javascript here, can anyone 'translate' it into PHP? I would if I could but i don't understand this javascript.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    function addvar(href, variable, value)
    {
    if(href.indexOf('?') != -1)
    {
    return href + '&' + variable + '=' + value;
    }
    else
    {
    return href + '?' + variable + '=' + value;
    }
    }

    for(i = 0; i < document.anchors.length; i++)
    {
    with(document.anchors[i])
    {
    href = addvar(href, 'SID', '<? echo $SID; ?>');
    }
    }

    for(i = 0; i < document.forms.length; i++)
    {
    with(document.forms[i])
    {
    action = addvar(action, 'SID', '<? echo $SID; ?>');
    }
    }
    [/code]
    the function above, searches all links and actions in a page, and adds an extra variable to the URL-string, in order to pass a session-ID to the next page.

    I don't know the new session-possibilities in PHP4, but in PHP3, you can add a variable to each link while building the page.

    What i would do:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    // at the top of the page
    $SID = some_function_or_var_that_passes_SID();

    function addvar($href, $var, $value){
    $querystring = strstr ($href, '?');
    if($querystring){
    return "$href&$var=$value";
    }
    else{
    return "$href?$var=$value";
    }
    }

    //and at any place in your code, where an url or form action is printed:

    echo "blabla, <A HREF="" . addvar("foo_link.php", "SID", $SID) . "" ALT="foo">click here</A>n";
    [/code]


    ------------------
    --
    ...tiktak...tiktak...
    --
    ...Tommyknockers, Knocking On Your Door...
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    England, UK
    Posts
    83
    Rep Power
    15
    I only want the link to have SID=*** if it has to (if the user doesn't have cookies enabled).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Den Laeghe Landen
    Posts
    107
    Rep Power
    15
    okay, in that case the only thing you have to change is:
    * let PHP generate your page, which is almost completely static, except for 1 thing: at the top do something like this:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    echo "<HTML>n"; // and all the other header-stuff
    echo "<SCRIPT LANGUAGE="Javascript"">n";
    echo " var SID=" . $SID . ";n"; // $SID should be available somewhere in your PHP-code
    // and below the rest of the html/javascript-code that you showed in the upper message
    [/code]

    the function-cals to addvar now become:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    function addSID(href)
    {
    if(href.indexOf('?') != -1)
    {
    return href + '&SID=' + SID;
    }
    else
    {
    return href + '?SID=' + SID;
    }
    }

    for(i = 0; i < document.anchors.length; i++)
    {
    with(document.anchors[i])
    {
    href = addSID(href);
    }
    }

    for(i = 0; i < document.forms.length; i++)
    {
    with(document.forms[i])
    {
    action = addSID(action);
    }
    }
    [/code]

Similar Threads

  1. PHP scripts to run in Javascript continuously
    By linhardt in forum JavaScript Development
    Replies: 0
    Last Post: February 13th, 2004, 02:51 PM
  2. pass php value of a varible to javascript variable
    By fluke_tony in forum JavaScript Development
    Replies: 1
    Last Post: February 13th, 2004, 03:34 AM
  3. javascript inside php tags
    By andy123 in forum JavaScript Development
    Replies: 5
    Last Post: February 5th, 2004, 04:34 PM
  4. Javascript in PHP
    By tintintin in forum JavaScript Development
    Replies: 5
    Last Post: January 28th, 2004, 05:40 AM
  5. Passing string from PHP to Javascript
    By ka8oad in forum JavaScript Development
    Replies: 4
    Last Post: January 17th, 2004, 03:12 PM

IMN logo majestic logo threadwatch logo seochat tools logo