Can this JavaScript function be done in PHP?

Discussion in 'PHP' started by Omzy, Dec 22, 2008.

  1. #1
    JavaScript Function:

    function goto(url)
    {
    var selection = document.getElementById('region');
    
    if(selection.value != null)
    {
    	document.location = url + document.getElementById('region').options[document.getElementById('region').selectedIndex].value;
    }
    }
    Code (markup):
    <a href="/directory/'.$index.'/" onclick="goto(this.href); return false">'.$cat[$index][2].'</a>
    Code (markup):
    'region' is a drop down box (HTML SELECT). What the function does is append the current value of 'region' to the end of the URL specified in the 'a href'. It does this onclick (not onpage load).

    I was wondering could the same functionality be achieved using PHP? Perhaps using the header() function?
     
    Omzy, Dec 22, 2008 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    crivion, Dec 22, 2008 IP
  3. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi crivion,

    No that's not what I'm after I'm afraid...
     
    Omzy, Dec 23, 2008 IP
  4. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The simple workaround would be to instead of link use submit button. Visitor would send this form to you and using values from the form PHP would decide where to redirect him. I think there is no other way to do this.
     
    Greg Carnegie, Dec 23, 2008 IP
  5. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    But you see it isn't just that one hyperlink - there are about 50 hyperlinks which are in the menu. It's the way the site works and it works fine using JavaScript but it wont work on some mobile browsers.
     
    Omzy, Dec 23, 2008 IP
  6. hackfanatic

    hackfanatic Peon

    Messages:
    29
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think onclick dosent work for dropdowns on mobile browsers..! not sure though.. you could display a button named go which calls goto()..

    That should work on js supported mobile browsers.
     
    hackfanatic, Dec 23, 2008 IP
  7. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No since javascript is a client side programming it can't be done using PHP you would have to recode it for in a similar way for it to work for mobile
     
    atlantaazfinest, Dec 24, 2008 IP
  8. apsam29

    apsam29 Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This is funny code. Make use of DOMDocument->loadHTML() in PHP scripting.
    Try yourself this code against JavaScript
     
    apsam29, Dec 24, 2008 IP