#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Function returns a form, not working


    Hey Devs,

    I'm working through a tutorial book by Apress published in 2009 called, "PHP for the Absolute Beginner". Which I got because they walk you through creating a PHP blog. There's been a couple of issues but this one I am really stuck on.

    The following codes (from Apress) are supposed to call a function that creates a "create user" form conditionally.

    functions.inc.php

    PHP Code:
    function createUserForm() {     
    return <<<
    FORM 
         
    <form action="/simple_blog/inc/update.inc.php" method="post">       
    <
    fieldset>         
              <
    legend>Create a New Administrator</legend>         
                    <
    label>Username             
                    
    <input type="text" name="username" maxlength="75" />         
                    </
    label>         
                    <
    label>Password             
                    
    <input type="password" name="password" />         
                    </
    label>         
          <
    input type="submit" name="submit" value="Create" />         
          <
    input type="submit" name="submit" value="Cancel" />         
          <
    input type="hidden" name="action" value="createuser" />     
    </
    fieldset
    </
    form
    FORM; } 
    this is a snippet from admin.php which is included in the document.

    PHP Code:
    // check if we're creating a new user
     
    if($page == 'createuser')
    {
         
    $create createUserForm();

    and also from that document in the HTML section

    Code:
    <?php      
    if($page == 'delete'):
    {
    echo $confirm;     
    } 	 
    elseif($page == 'createuser'):     
    {     	
    echo $create; 	
    }     
    else:  
    ?>
    (you can download the exercise files here. (chapter 11) Apress Files)

    Even while using the actual Apress Exercise documents, this fails to pull up an administrator "add user" form. Also I should add that I am positive that my MySql connection is correct and that my database reflects the tutorials.

    Any help would be very much appriciated.

    Dan
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Originally Posted by danbeaulieu
    Even while using the actual Apress Exercise documents, this fails to pull up an administrator "add user" form.
    Then what does it do?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0
    pulls up a blank page with just the header on it saying "simple blog application" thats it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    What does your address end like? I'm guessing it should not look like '.../file.php' but more like '.../file.php?page=createuser'

    Also, as long as I am correct, you do not need your final 'else:' unless there is more you are executing which we don't see.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by Triple_Nothing
    What does your address end like? I'm guessing it should not look like '.../file.php' but more like '.../file.php?page=createuser'

    Also, as long as I am correct, you do not need your final 'else:' unless there is more you are executing which we don't see.
    The tutorial utilizes .htaccess but I think the URL would be simple_blog/admin.php?page=createuser

    regarding the "else" heres the entire admin.php page

    PHP Code:
    <?php      error_reporting(E_ALL);     ini_set('display_errors'2);      /*      * Include the necessary files      */     include_once 'inc/functions.inc.php';     include_once 'inc/db.inc.php';      // Open a database connection     $db = new PDO(DB_INFO, DB_USER, DB_PASS);      $page = isset($_GET['page']) ? htmlentities(strip_tags($_GET['page'])) : 'blog';      if(isset($_POST['action']) && $_POST['action'] == 'delete')     {         if($_POST['submit'] == 'Yes')         {             $url = htmlentities(strip_tags($_POST['url']));             if(deleteEntry($db, $url))             {                 header("Location: /simple_blog/");                 exit;             }             else             {                 exit("Error deleting the entry!");             }         }         else         {             header("Location: /simple_blog/blog/$_POST[url]");         }     }      if(isset($_GET['url']))     {         $url = htmlentities(strip_tags($_GET['url']));          // Check if the entry should be deleted         if($page == 'delete')         {             $confirm = confirmDelete($db, $url);         }          // Set the legend of the form         $legend = "Edit This Entry";          $e = retrieveEntries($db, $page, $url);         $id = $e['id'];         $title = $e['title'];         $img = $e['image'];         $entry = $e['entry'];     }     else     {         // Set the legend         $legend = "New Entry Submission";          // Set the variables to null if not editing         $id = NULL;         $title = NULL;         $img = NULL;         $entry = NULL;     } ?> <!DOCTYPE html     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>     <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />     <link rel="stylesheet" href="/simple_blog/css/default.css" type="text/css" />     <title> Simple Blog </title> </head>  <body>     <h1> Simple Blog Application </h1>  <?php      if($page == 'delete'):     {         echo $confirm;     }      elseif($page == 'createuser'):     {         echo $create;     }     else:  ?>     <form method="post"          action="/simple_blog/inc/update.inc.php"          enctype="multipart/form-data">         <fieldset>             <legend><?php echo $legend ?></legend>             <label>Title                  <input type="text" name="title" maxlength="150"                     value="<?php echo $title ?>" />             </label>             <label>Image                  <input type="file" name="image" />             </label>             <label>Entry                  <textarea name="entry" cols="45"                      rows="10"><?php echo $entry ?></textarea>             </label>             <input type="hidden" name="id"                 value="<?php echo $id ?>" />             <input type="hidden" name="page"                 value="<?php echo $page ?>" />             <input type="submit" name="submit" value="Save Entry" />             <input type="submit" name="submit" value="Cancel" />         </fieldset>     </form> <?php endif; ?> </body>  </html>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    How come you have colons ( : ) directly after your if/elseif lines? This seems to be the only area you did that. I don't know the outcome of this, but see it nowhere on the net, so this is maybe voiding your statements?

    Ok, and your final } else:, change that to } else { and your elseif; change to }
    Last edited by Triple_Nothing; December 19th, 2012 at 10:05 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by Triple_Nothing
    How come you have colons ( : ) directly after your if/elseif lines? This seems to be the only area you did that. I don't know the outcome of this, but see it nowhere on the net, so this is maybe voiding your statements?

    Ok, and your final } else:, change that to } else { and your elseif; change to }
    i thought it was unusual myself but thats the way the tutorial is written, here's the entire statement.

    PHP Code:
    <?php      
    if($page == 'delete'):     {         
       echo 
    $confirm;     
    }     
      elseif(
    $page == 'createuser'):     {         
           echo 
    $create;     
    }     
           else:  
    ?>    

     <form method="post"          
    action="/simple_blog/inc/update.inc.php"          
    enctype="multipart/form-data">         
    <fieldset>             
    <legend><?php echo $legend ?></legend>             
    <label>Title                  
    <input type="text" name="title" maxlength="150"                     
    value="<?php echo $title ?>" />             
    </label>             
    <label>Image                  
    <input type="file" name="image" />             
    </label>             
    <label>Entry                  
    <textarea name="entry" cols="45"                      
    rows="10"><?php echo $entry ?></textarea>             </label>             
    <input type="hidden" name="id"                 
    value="<?php echo $id ?>" />             
    <input type="hidden" name="page"                 
    value="<?php echo $page ?>" />             
    <input type="submit" name="submit" value="Save Entry" />             
    <input type="submit" name="submit" value="Cancel" />         
    </fieldset>     
    </form> 
    <?php endif; ?>
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    I'd make those minor changes I mentioned before and see what happens.

IMN logo majestic logo threadwatch logo seochat tools logo