View Full Version : Changing the post on a form based on input submission
digitaldiva
04-27-2009, 08:44 PM
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.
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;
}
}
borris83
04-28-2009, 03:53 AM
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.
digitaldiva
04-28-2009, 12:58 PM
$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?
<?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>
Schmoopy
04-28-2009, 08:30 PM
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:
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?
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 :)
digitaldiva
04-29-2009, 02:42 AM
I'll try it out.
If you like, how would you have done it? Totally curious and open to suggestions.
bkatzung
04-29-2009, 03:42 AM
Server side (PHP) solution (require the appropriate PHP 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):
<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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.