www.webdeveloper.com
Results 1 to 6 of 6

Thread: Elementary if then else trouble

  1. #1
    Join Date
    Jun 2008
    Location
    Omaha
    Posts
    15

    Question Elementary if then else trouble

    Hello,

    Since I'm starting with PHP, I just can't figure out why this sequence does not work. It brings me a clean page with nothing in it.
    I ran the script with echo on the "POST[uid']" and "POST['edupdel']" and both are received from the calling script.
    All scripts
    PHP Code:
    <?php
    ob_start
    ();
    session_start();
    if (isset(
    $_POST['uid']))
        { 
            
    $uid $_POST['uid'];
        }
    else
        {
            die(
    "UID NOT FOUND");
        }
        
    if    (!isset(
    $_POST['edupdel']))
        {
            die(
    "Edupdel not found");
        }
    else
        {
            
    $opt =    $_POST['edupdel'];
        }
        
    if    (
    $opt == "listone")
        {
            
    header('Location:selectone_user.php?uid=$uid');
            exit;
        }

    if    (
    $opt == "noselect")
        {
            
    header("Location:invalid_option.php");
            exit;
        }

    if    (
    $opt == "update")
        {
            
    header("Location:update_user.php");
            exit;
        }
    if    (
    $opt == "listall")
        {
            
    header("Location:listall_user.php");
            exit;
        }
        
    if    (
    $opt == "delete")
        {
            
    header("Location:delete_user.php");
            exit;
        }
        
    if    (
    $opt == "add")
        {
            
    header("Location:add_user.php");
            exit;
        }    
    ob_end_flush(); 
    ?>
    What is wrong with this code??? (or witth me )


    Thank You

  2. #2
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Per the HTTP specification, the Location header expects a fully qualified url. (See here) Try using a full url for example:

    PHP Code:
    header("Location: http://www.example.com/add_user.php"); 
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    At least for debugging, try these additions:
    Code:
     <?php
    ini_set('display_errors', true);
    error_reporting(E_ALL);
    ob_start();
    session_start();
    if (isset($_POST['uid']))
        { 
            $uid = $_POST['uid'];
        }
    
    // ...etc....
    
    if    ($opt == "add")
        {
            header("Location:add_user.php");
            exit;
        }    
    ob_end_flush();
    echo "<p>DEBUG: Should we have gotten here?</p>"; 
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jun 2008
    Location
    Omaha
    Posts
    15
    Quote Originally Posted by Derokorian View Post
    Per the HTTP specification, the Location header expects a fully qualified url. (See here) Try using a full url for example:

    PHP Code:
    header("Location: http://www.example.com/add_user.php"); 
    Thank You for the help. In the last minutes before leaving work I found the answer:

    PHP Code:
    if    ($opt == "add")
        {
            echo 
    "<script> 
            window.location.href='add_user.php?uid=
    $uid'</script>";
        } 
    Note that since I'm working on localhost I do not need at this point the .. www...
    Nevertheless your example will be used as a guide when I go into production.

    Now, independently of my approach, I would really appreciate if you could lead me to the way where your example would work for me. I think that solving the issue the way I did does not make the one that is not working go away. I'd like to have both options. So, if you are up to it, with some infinite patience, it would interesting to find out why the Header("Location: does not work for me.

    Once again, Thank You.
    Have a nice weekend.
    Last edited by oldbag; 12-07-2012 at 09:18 AM.

  5. #5
    Join Date
    Jun 2008
    Location
    Omaha
    Posts
    15
    NoDog,

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    This line reminds me of General Relativity and Quantum Mechanics when taken looking at Black Holes for the quantum perspective. The answers do not fit the way the World seems to work.

    Sorry, I digress...

    Going back to what is important. Thank You for your help. I tested your example and it would only show me the dreaded white (blank page). Not even the last echo shows up.

    Anyway, even though I solved the issue using a bit of Javascript :

    PHP Code:
    if    ($opt == "add")
        {    
            echo 
    "<script> 
            window.location.href=add_user.php?uid=
    $uid'</script>";

             exit;
        } 
    Now, independently of my approach, I would really appreciate if you could lead me to the way where your example would work for me. I think that solving the issue the way I did does not make the one that is not working go away. I'd like to have both options. So, if you are up to it, with some infinite patience, it would interesting to find out why the Header("Location: does not work for me.

    Once again thank You and have a nice weekend.
    Last edited by oldbag; 12-07-2012 at 09:19 AM.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    If you get a blank page, first do a "view source" just in case something was returned but is interpreted by the browser as being all mark-up with no content.

    Otherwise, if it's truly blank, that is usually a parse/compile error. When that happens, if your web server's PHP configuration has display_errors turned off, then nothing will be displayed. Therefore, in your development environment, it's useful to turn it on; or else put a tail on your php-errors.log file to see if it tells you what happened. (It's also useful to use a code editor that auto-detects parse errors, to at least let you find those before you actually try running it.)

    However, my editor did not detect any parse errors when I copy/pasted your original code. It's possible it's "working" up to the point where the browser receives the Location HTTP header, but is unable to process it. I don't think it's required, but I always include a space after the "Location:". Also, if testing locally, you can still use a full URL:
    PHP Code:
    header("Location: http://localhost/file.php"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles