...

View Full Version : Variable isn't getting properly echoed - odd



fl00d
12-24-2007, 12:36 AM
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



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 :(

Jesuspwnt
12-24-2007, 12:53 AM
<?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($_POST){
$order = htmlentities($_POST['orderby']);
}else{
$order = "default!";
}
echo "Order is: ".$order;
?>

tested and working =]

fl00d
12-24-2007, 12:59 AM
Great thank you! Can you explain though the difference? Obviously I noted you changed

if($_POST){
but I'm not really quite sure what that does.

Jesuspwnt
12-24-2007, 01:05 AM
well in your previous code you were already setting the $ordersubmit by using the htmlentities.
so i replaced that with checking if the page has been viewed via post.
so if somebody has used your form, because its submitted by POST, we check it
IF($_POST)
its the same as isset.
but its shorter.

Enjoy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum