SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to redirect to selected option

    I need a php redirect scrip to go to a particular state from a drop-down select
    I want to use an array.
    <?php
    $states_arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>......);
    ?>

    Should I use this in the htmlpart?


    <select name="states">
    <option value="0">Choose a state</option>
    <?php echo showOptionsDrop($states_arr); ?>
    </select>

    <input type="submit" value="Find">
    </form>
    or please advise..

    Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (isset($_GET['states']) && isset($states_arr[$_GET['states']]) {
        
    header('Location: ' $states_arr[$_GET['states']]);
        exit;


  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    crmalibu,

    I forgot to mention it will be on a parsed html page.

    and I am not sure how to impliment what you suggest here?

    Thanks
    Last edited by lukkas; Jul 13, 2009 at 13:08. Reason: spelling

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Devon, UK
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forst let's build your HTML for the drop-down box (presuming $arrStates contains your list of states)
    PHP Code:
    $htmStates='';
    foreach (
    $arrStates as $abbrev => $state) {
      
    $htmStates.='<option value="'.$abbrev.'"'.($abbrev==$strState ' selected' '').'>'.$state.'</option>';

    That will build all your HTML into the variable $htmStates. This also presumes that you've used $_POST to read the state from your form beforehand and uses it as a default value should the form have to be presented to the user again - it remembers what was chosen.

    Let's say your form looks like this:
    Code:
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    State: <select name="state"><option value="XX">-- Choose --</option><?php echo $htmStates; ?></select><br>
    <input type="submit" name="substates" value="Submit State">
    Now, to detect the submit button and that the state is valid we can use this:
    PHP Code:
    if (isset($_POST['substate'])&&$_POST['state']!='XX') {
      
    header('Location: '.$_POST['state'].'.html.');
      exit;

    Let's say you've chosen Alabama - it'll try and redirect to AL.html

    Note the exit after the header() function - this is to make sure the script terminates after redirecting.
    Last edited by Yesideez; Jul 13, 2009 at 13:49. Reason: Variable typo would have caused some confusion

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also don't use PHP_SELF, use $_SERVER['SCRIPT_NAME'] instead. PHP_SELF is vulnerable to attacks when not used correctly. With a little help of js you can also make your select to submit automatically and redirect by adding <select name="states" onchange="this.form.submit();">. Disadvantage in this is that if the user clicked wrong state then he would be immediately redirected to wrong page.

  6. #6
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Tendolla,

    Yeah, I prefer a way that is not code heavy and now not as vulnerable.

    Forgive me for asking but why can't it just be a short jump script with something I had above

  7. #7
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It can be no problems. I just brought up a another way for submit. The actual problem you had was with redirect right? That happens just the way crmalibu and Yesideez has shown above. Depending on the method you are using in your form (GET/POST).


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
  •