Jump to content

[SOLVED] Default variable value not being echoed


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.