Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    Variable isn't getting properly echoed - odd

    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 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
    Last edited by fl00d; 12-24-2007 at 12:40 AM.

  • #2
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    PHP 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
    if($_POST){
      
    $order htmlentities($_POST['orderby']);
    }else{
      
    $order "default!";
    }
    echo 
    "Order is: ".$order
    ?>
    tested and working =]

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    Great thank you! Can you explain though the difference? Obviously I noted you changed
    PHP Code:
    if($_POST){ 
    but I'm not really quite sure what that does.

  • #4
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •