Jump to content

[SOLVED] Default variable value not being echoed


CMC

Recommended Posts

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

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.

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.