SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why doesn't this echo print?

    Hi -

    I'm out of ideas for testing this and could probably flip out any minute. Could someone please help in figuring out why the marked echo statement won't print?

    PHP Code:
    <?php 

    // start PHP session
    session_start();
    echo 
    $_POST['entry'];
    if (isset(
    $_POST['entry']))
      {
        
    $id $_POST['entry'];
      }else{
    $id $_GET['entry'];}

    if (empty(
    $id)){
      
    header ('Location: index.php');
      }

    // check for posted form
      
    if (isset($_POST['login'])) 
    {
    //see if the code the user typed matched the generated code
    if (!empty($_SESSION['code']) && strtoupper($_POST['code']) == $_SESSION['code'])
        {
        unset(
    $_SESSION['code']);
        }else{
            if(
    count($_POST))
            {
            
    $error_list = array();
            
    $error_list[] = 'You entered an incorrect code.';
                          
            
    $name trim($_POST['name']);
            
    $email trim($_POST['email']);
            
    $cbody trim($_POST['body']);
            
    $parentid $_POST['parent_id'];
            
    $comsubject $_POST['subject'];
                                                                    if(empty(
    $name))
                {
                
    $error_list[] = 'You did not supply your name.';
                }
            if(empty(
    $email))
                {
                
    $error_list[] = 'You did not supply your email address';
                }
            if(empty(
    $cbody))
                {
                
    $error_list[] = 'You did not supply a comment';
                }
            if(
    count($error_list) > 0)
                {
                
    $_SESSION['name'] = $name;
                
    $_SESSION['email'] = $email;
                
    $_SESSION['body'] = $cbody;
                                      
                
    $status "<h2>Please <a href=\"blog_view.php?entry=$id#comment\">go back</a> and correct the following errors.</h2";
                
    $status .= "<div id=\"errorlist\"><ul>\n";
                                        
                foreach(
    $error_list as $error)
                {
                
    $status .= "<li>$error</li>\n";
                }
                
    $status .= "</ul>\n";
                echo 
    $status;
                }else
                {
    //THIS ECHO WON'T PRINT
                    
    echo "<p>How are you?</p>";
                    
    $sql "INSERT INTO blog ";
                    
    $sql .= "SET parent_id='$parentid', subject='$comsubject', author='$name', body='$cbody', category_id='', status='hide', email='$email'";
                    echo 
    $sql;
                    if(
    mysql_query($sql)) 
                    {
                                    unset(
    $parentid$comsubject$name$cbody$email); ?>
                    <div id="commentapprove">
                    <p>Thank you for your comment. It has been sent to the site administrator for approval.</p>
                    <ul>
                    <li>Return to the <a href="index.php">Resource Page.</a></li>
                    </ul>
                    </div>                                     
                <?php }
                    }
        }
      }
    }
    ?>
    The IF (count($_POST)) works, since if I submit my form without the fields, it generates the list and gives me the link to fix it. However, the ELSE statement doesn't kick in for whatever reason.

    To test, I've taken this:

    PHP Code:
    <?php

    $error_list 
    = array();
    $name trim($_POST['name']);
    $email trim($_POST['email']);
    $cbody trim($_POST['body']);
    $parentid $_POST['parent_id'];
    $comsubject $_POST['subject'];
                                                                                        
    if(empty(
    $name))
    {
        
    $error_list[] = 'You did not supply your name.';
    }
    if(empty(
    $email))
    {
        
    $error_list[] = 'You did not supply your email address';
    }
    if(empty(
    $cbody))
    {
        
    $error_list[] = 'You did not supply a comment';
    }
                                    

    if(
    count($error_list) < 0)
    {
         
    $_SESSION['name'] = $name;
         
    $_SESSION['email'] = $email;
         
    $_SESSION['body'] = $cbody;
                                      
        
    $status "<h2>Please <a href=\"blog_view.php?entry=$id#comment\">go back</a> and correct the following errors.</h2";
        
    $status .= "<div id=\"errorlist\"><ul>\n";                                    
        foreach(
    $error_list as $error)
        {
            
    $status .= "<li>$error</li>\n";
        }
        
    $status .= "</ul>\n";
        echo 
    $status;
        }
    //THIS ELSE WORKS
            
    else
        {
            echo 
    "How are you?";
            
    $sql "INSERT INTO blog ";
            
    $sql .= "SET parent_id='$parentid', subject='$comsubject', author='$name', body='$cbody', category_id='', status='hide', email='$email'";
            if(isset(
    $sql)){ echo "<br />hi"; }
        }

    ?>
    in it's own file (had to change to if(count($error_list) < 0) so the errors wouldn't be picked up) and it works.

    What the hell is the difference?

    thanks
    No, I REALLY dislike having to use Joomla.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the simple answer would be:

    count($error_list) is greater than 0.

    use var_dump() to see the contents of a variable. in your case, you would be intrested in $error_list

  3. #3
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I added this...

    PHP Code:
    if(count($_POST))
    {
    $error_list = array();
    $error_list[] = 'You entered an incorrect code.';
                          
    $name trim($_POST['name']);
    $email trim($_POST['email']);
    $cbody trim($_POST['body']);
    $parentid $_POST['parent_id'];
    $comsubject $_POST['subject'];
                                                                                        
    if(empty(
    $name))
    {
    $error_list[] = 'You did not supply your name.';
    }
    if(empty(
    $email))
    {
    $error_list[] = 'You did not supply your email address';
    }
    if(empty(
    $cbody))
    {
    $error_list[] = 'You did not supply a comment';
    }
    var_dump($error_list); 
    nothing comes up.
    No, I REALLY dislike having to use Joomla.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats because count($_POST) is not boolean true, so that code is never evaluated. post something.

  5. #5
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I submit the form and leave out a field...

    Code:
    array(1) { [0]=>  string(30) "You entered an incorrect code." }
    No, I REALLY dislike having to use Joomla.

  6. #6
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher View Post
    thats because count($_POST) is not boolean true, so that code is never evaluated. post something.
    sorry, that's a snippet from the first code block from above with the var_dump added.

    I did post something.
    No, I REALLY dislike having to use Joomla.

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crowden View Post
    When I submit the form and leave out a field...

    Code:
    array(1) { [0]=>  string(30) "You entered an incorrect code." }


    as you can see, your array has 1 element. so count() will always be at least 1.

  8. #8
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's only when I purposely leave out a field so the $error_list array gets populated.

    the array is empty when all fields are filled and posted correctly.

    if it's not, how can I see what's populating it, cause var_dump isn't giving me anything when I post the form correctly.
    No, I REALLY dislike having to use Joomla.

  9. #9
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, I rearrange some conditionals...

    this

    PHP Code:
    $error_list = array();
    if (isset(
    $_POST['login'])) 
    {
    //see if the code the user typed matched the generated code
    if (!empty($_SESSION['code']) && strtoupper($_POST['code']) == $_SESSION['code'])
    {
    unset(
    $_SESSION['code']);
    }else{
         
    $error_list[] = 'You entered an incorrect code.';}
         if(
    count($_POST))

    instead of this...

    PHP Code:
    if (isset($_POST['login'])) 
    {
    //see if the code the user typed matched the generated code
    if (!empty($_SESSION['code']) && strtoupper($_POST['code']) == $_SESSION['code'])
    {
        unset(
    $_SESSION['code']);
    }else{
    $error_list = array();
    $error_list[] = 'You entered an incorrect code.';
    if(
    count($_POST))

    and I get a response. thanks for looking at it and the advice as always.
    No, I REALLY dislike having to use Joomla.


Bookmarks

Posting Permissions

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