Results 1 to 6 of 6

Thread: Changing the post on a form based on input submission

  1. #1
    Join Date
    Jul 2007
    Location
    north shore
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing the post on a form based on input submission

    Hi, I have a problem I am struggling with... I have a simple html form. The purpose is to have the user select one of two regions, either east or west (they are now radio-button inputs).

    I want the user to be redirected to the appropriate page based on their region. I want the forms post value to change to one page if west or a different page if east.

    This is what I have on the page of the form, but its not adequate. Either there's a better way to do this altogether or I am missing something really fundamental. It always posts to self.

    Code:
    if ( isset($_POST['region']) ) {
    	$region = $_POST['region'];
    	}
    if ( $region == $region_west  ) {
    	$post_to = $region_url_west;
    	}
    else if ($region == $region_east  ) {
    	$post_to = $region_url_east;
    	}
    else {
    	$post_to = $current_page;
    	}
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Where are the values of $region_url_west and $region_url_east coming from? How are you using $post_to in your code?

    You have only posted a part of code and the problem is actually in the part of code which you have not posted....

    Pls post the code for the form as well as the parts of code which will explain those variables.

  3. #3
    Join Date
    Jul 2007
    Location
    north shore
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    $region_url_west and $region_url_east are statically assigned variables. I guess the problem is that the post is defined before the user submits the form. But its the form that re-directs the user or should define the post... What's the best way to do this?

    Code:
    <?php
    
    $region_url_west = "region-west.php";
    $region_url_east = "region-east.php";
    $region_west = "West Coast";
    $region_east = "East Coast";
    
    if ( isset($_POST['region']) ) {
    	$region = $_POST['region'];
    	}
    if ( $region == $region_west  ) {
    	$post_to = $region_url_west;
    	}
    else if ($region == $region_east  ) {
    	$post_to = $region_url_east;
    	}
    else {
    	$post_to = $current_page;
    	}
    }
    
    ?>
       
    <form action="<?php echo $post_to; ?>" method="post"> 
      <div id="regionselection">            
       <ul>
         <li><a href="#"><?php echo $region; ?></a>
         <ul>
            <li><label><input type="radio" name="region" value="<?php echo $region_west; ?>" onclick="this.form.submit()" class="regionradio" />West Coast Factory</label></li>
            <li><label><input type="radio" name="region" value="<?php echo $region_east; ?>" onclick="this.form.submit()" class="regionradio" />East Coast Factory</label></li>
         </ul>
         </li>
         </ul>
     </div>
    </form>

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    There is an error in your code, your if statement ends prematurely, after checking whether the region post variable is set you've ended it straight after, meaning the other conditions won't be executed.

    You write:

    PHP Code:
    if ( isset($_POST['region']) ) {
        
    $region $_POST['region'];
        } 
    See? The curly brace is there and it shouldn't be, meaning that if the variable is set nothing will really happen because it's been ended, so if you take that out you should get it working:

    This also means that there is a redundant curly brace at the end, which should throw up an error - maybe you have error checking turned off?

    PHP Code:
    if ( isset($_POST['region']) ) {
        
    $region $_POST['region'];

        if ( 
    $region == $region_west  ) {
            
    $post_to $region_url_west;
        }
        else if (
    $region == $region_east  ) {
            
    $post_to $region_url_east;
        }
        else {
            
    $post_to $current_page;
        }

    Also indented it to make it clearer that the first if statement encompasses them all.

    Finally... I must say I don't like how this has been coded, I will wait to hear if the above fixes your problem but I can think of a better way of doing what you want if it doesn't work out for you.

    Good luck with this any way

  5. #5
    Join Date
    Jul 2007
    Location
    north shore
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'll try it out.

    If you like, how would you have done it? Totally curious and open to suggestions.

  6. #6
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Server side (PHP) solution (require the appropriate PHP code):
    Code:
        if ( $region == $region_west  ) {
            require $region_url_west;
        }
        else if ($region == $region_east  ) {
            require $region_url_east;
        }
        else {
            # local page processing
        }
    Client side (Javascript) solution (change the form action):
    Code:
    <li><label><input type="radio" name="region" value="<?php echo $region_west; ?>" onclick="this.form.action='<?php echo $region_url_west; ?>';this.form.submit()" class="regionradio" />West Coast Factory</label></li>
    <li><label><input type="radio" name="region" value="<?php echo $region_east; ?>" onclick="this.form.action='<?php echo $region_url_east; ?>';this.form.submit()" class="regionradio" />East Coast Factory</label></li>
    I would either have the factory buttons be submit buttons, or have a distinct submit button. Using a radio button to submit a form violates the users' interface expectations.

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
  •