SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Form re-direct to a different URL.

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form re-direct to a different URL.

    Okay apologies this is'nt technically PHP yet I trying to develop a simple select form that will action another url and fulfill the request of the option selected.

    The site is http://www.ding.com/

    The pages that each option will open are;

    a-distirct
    b-district

    So option a on the form would take the user to http://www.ding.com/a-district

    I cant seem to get this right heres the form markup I'm toying with;

    Code:
    <form id="select" action="http://ding.com" method="get">
    	<select name="/">
    		<option value="">Please choose</option>				
    		<option value="a-district">A district</option>	
    		<option value="b-district">B-District</option>	
    </select>
    	<input type="submit" value="Go" />
    </form>
    The resulting url is showing "/?%2F=a-district"......

    Just noticed ding.com is being held - i'm only using this as an example....
    Last edited by stephen_; Dec 13, 2012 at 06:57. Reason: Disclaimer

  2. #2
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,475
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    Hi there,

    You could do this with JavaScript.
    Would using JS be a solution you would consider?
    If so, let me know and I'll provide you with an example.

    Edit: Just to be clear, the reason I ask is because this would disable this functionality for anyone who had JS switched off.
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pullo JS would be perfect. An example would be excellent - thanks!

  4. #4
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,347
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi there,

    You could do this with JavaScript.
    Would using JS be a solution you would consider?
    If so, let me know and I'll provide you with an example.

    Edit: Just to be clear, the reason I ask is because this would disable this functionality for anyone who had JS switched off.
    You could always use JS and a normal form that posts to a php script which in turn redirects to the correct URL.
    In any case, just a <select> without any scripting isn't going to cut it.

    HTML Code:
    <form id="select" action="/choose-district.php" method="get">
    	<select name="disctrict" id="district">
    		<option value="">Please choose</option>				
    		<option value="a">A district</option>	
    		<option value="b">B-District</option>	
            </select>
    	<input type="submit" value="Go" />
    </form>
    <script type="text/javascript">
    document.getElementById('district').onChange = function() {
        var elem = document.getElementById("district");
        var selected = elem.options[elem.selectedIndex].value;
        if (selected != '') {
            window.location = '/' + selected + '-district';
        }
    }
    </script>
    choose-district.php
    PHP Code:
    <?php
    if (isset($_GET['district']) && $_GET['district'] != '')
    {
        
    // district selected
        
    $district $_GET['district'];
        
    header("Location: http://www.example.com/$disctrict-district");
        exit;
    }
    // user didn't select a disctrict, redirect to the homepage
    header('Location: http://ww.example.com/');
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,475
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You could always use JS and a normal form that posts to a php script which in turn redirects to the correct URL.
    How would you best do this Rémon?
    Using cURL?
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  6. #6
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,347
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Pullo View Post
    How would you best do this Rémon?
    Using cURL?
    See my previous post. I've added a lot in an edit.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,539
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    Pullo: His second code block is the PHP page for what he described.

    I think what you're thinking of is an AJAX call, which is essentially a combination of the two methods.

    EDIT: Too many people posting at once ! lol
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,475
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    See my previous post. I've added a lot in an edit.
    The PHP solution is nice.
    Thank you!

    Starlion, nah, I was thinking in too complicated terms, as usual
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay implemented ScallioXTX code yet I'm only getting -district ?

    My code;

    HTML
    Code:
    <script type="text/javascript">
    document.getElementById('district').onChange = function() {
        var elem = document.getElementById("district");
        var selected = elem.options[elem.selectedIndex].value;
        if (selected != '') {
            window.location = '/' + selected + '-district';
        }
    }
    </script>
    
    <form id="select" action="redirect.php" method="get">
    	<select name="district" id="district">
    		<option value="">Please choose</option>				
    		<option value="a">blah</option>	
    		<option value="b">blah</option>		
    	</select>
    	<input type="submit" value="Go" />
    </form>
    redirect.php
    PHP Code:
     <?php
    if (isset($_GET['district']) && $_GET['district'] != '')
    {
        
    // district selected
        
    $district $_GET['district'];
        
    header("Location: http://www.ding.com/$disctrict-district");
        exit;
    }
    // user didn't select a disctrict, redirect to the homepage
    header('Location: http://www.ding.com/'); 
    ?>

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    765
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    There is a typo in this code, see $district vs $disctrict

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay I'm a numpty - thanks all changed the php and select options

    PHP Code:
     <?php
    if (isset($_GET['district']) && $_GET['district'] != '')
    {
        
    // district selected
        
    $district $_GET['district'];
        
    header("Location: http://www.ding.com/$district");
        exit;
    }
    // user didn't select a disctrict, redirect to the homepage
    header('Location: http://www.ding.com/'); 
    ?>
    Select option is now....
    HTML Code:
    <option value="b">blah</option>
    THANKS TO ALL OF YOU!

    PS I've another more complicated one which I'd love to figure out - anyone interested PM me....

  12. #12
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,475
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by stephen_ View Post
    PS I've another more complicated one which I'd love to figure out - anyone interested PM me....
    It's probably best to start a new thread.
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

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
  •