CMC Posted December 23, 2007 Share Posted December 23, 2007 Hi, I'm working on a script and everything is running fine except for this problematic part. Really, it is quite simple code but for some reason that I don't know the variable isn't getting echoed right away. Basically, I have a select list and a button. If the button is pressed, I want it to assign the value of the select to a variable. If the button isn't pressed, the variable has a default value. Pretty simple, except the variable won't echo out unless the button is pressed... here is the code <?php echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">"; echo "<select name=\"orderby\">"; echo "<option value=\"shop_name\">Shop Name</option>"; echo "<option value=\"shop_location\">Location</option>"; echo "</select>"; echo "<input type=\"submit\" name=\"orderSubmit\" value=\"Sort\">"; echo "</form>"; //if button is set, set order $orderSubmit = htmlentities($_POST['orderSubmit']); if(isset($orderSubmit)){ $order = htmlentities($_POST['orderby']); }else{ $order = "default!"; } echo "Order is: ".$order; ?> When the button isn't pressed the default value is echoing out blank, when I told it it should be "default!". Any idea's? It looks like it should work to me... but it doesn't Quote Link to comment https://forums.phpfreaks.com/topic/82980-solved-default-variable-value-not-being-echoed/ Share on other sites More sharing options...
corillo181 Posted December 23, 2007 Share Posted December 23, 2007 mm for wha ti can see the reason for why could be happening is that you using a if to check if orderSubmit isset, you are giving the variable ordersubmit a set when you put it outside the if statement.. mm how can i explain this better.. the if see that ordersumit has a value becuase you gave it htmlentities($_POST['orderSubmit']) so it is set. so when it check if(isset(orderSubmit)) the result is yes.. and the becomes order = htmlentities($_post[]) which is blank becuase nothing is posted yet. there for your get a blank order.. the best way to do it would be if(!empty(orderSubmit)) that way it checks that it is not empty. Quote Link to comment https://forums.phpfreaks.com/topic/82980-solved-default-variable-value-not-being-echoed/#findComment-422039 Share on other sites More sharing options...
corillo181 Posted December 24, 2007 Share Posted December 24, 2007 i just wanted to give you some insight , the easy way would be to change if(isset()) to if(!empty()) Quote Link to comment https://forums.phpfreaks.com/topic/82980-solved-default-variable-value-not-being-echoed/#findComment-422043 Share on other sites More sharing options...
cooldude832 Posted December 24, 2007 Share Posted December 24, 2007 lets see what $_POST is showing us !empty should work <?php echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">"; echo "<select name=\"orderby\">"; echo "<option value=\"shop_name\">Shop Name</option>"; echo "<option value=\"shop_location\">Location</option>"; echo "</select>"; echo "<input type=\"submit\" name=\"orderSubmit\" value=\"Sort\">"; echo "</form>"; //if button is set, set order if(!empty($_POST)){ print_r($_POST); } $orderSubmit = htmlentities($_POST['orderSubmit']); if(isset($orderSubmit)){ $order = htmlentities($_POST['orderby']); }else{ $order = "default!"; } echo "Order is: ".$order; ?> Edit: I know the issue, I don't belive submit buttons are carried across $_POST on processing. you will need to do something like a hidden var <?php echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">"; echo "<input type=\"hidden\" name=\"submitted\" value=\"yes\" />"; echo "<select name=\"orderby\">"; echo "<option value=\"shop_name\">Shop Name</option>"; echo "<option value=\"shop_location\">Location</option>"; echo "</select>"; echo "<input type=\"submit\" name=\"orderSubmit\" value=\"Sort\">"; echo "</form>"; //if button is set, set order if($_POST['submitted'] == "yes'){ $order = htmlentities($_POST['orderby']); } else{ $order = "default!"; } echo "Order is: ".$order; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82980-solved-default-variable-value-not-being-echoed/#findComment-422052 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.