Thread: Code showing

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

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7

    Code showing


    Can someone tell me why my code shows when you load it in a browser? The form shows but there is code showing where you put the year and it also shows the case code as well as the code that follows.

    PHP Code:
     <!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">
    <head>
    <title>Your Chinese Zodiac Sign</title>
    <link rel="stylesheet" type="text/css" href="ChineseZodiac.css" /> 
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>

    <h1>Your Chinese Zodiac Sign</h1>
    <h2>using an <span class="code">if...else</code> statement</h2>


    <?php
    function validateInput($data$fieldName) {
         global 
    $errorCount;
         if (empty(
    $data)) {
              echo 
    "\"$fieldName\" is a required field.<br />\n";
              ++
    $errorCount;
              
    $retval "";
         } else { 
    // Only clean up the input if it isn't empty
              
    $retval trim($data);
              
    $retval stripslashes($retval);
         }
         return(
    $retval);
    }

    function 
    displayForm($Year) {
    ?>
    <form action = "BirthYear_ifelse.php" method = "post">
    <p>Your Birth Year: <input type="text" name="Year" value="<?php echo $Year?>" /></p>
    <p><input type="reset" value="Clear Form" />&nbsp; &nbsp;<input type="submit" name="Submit" value="Show Me My Sign" /></p>
    </form>
    <?php
    }


    function 
    StatisticsForYear($Year) {
        
    $retval 0;
          
        
    $counter_file "./Statistics/BirthYear_" $Year "_Count.txt";
            
        if (
    file_exists($counter_file)) {
            
    $Hits file_get_contents($counter_file);
            ++
    $Hits;
        }
        else{
            
    $Hits 1;
        }
        
    file_put_contents($counter_file$Hits);
        
    $retval $Hits;
        return(
    $retval);
    }     


         

    function 
    displayResults($Year) {
         
    $CZIndex = ($Year+8) % 12;
         if (
    $CZIndex == 0) {
              echo 
    "<p>You were born under the sign of the Rat.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/rat.jpg alt='Rat' title='Rat' width='300%' height='300%'/></p>\n";
         } 
         else if (
    $CZIndex == 1) {
              echo 
    "<p>You were born under the sign of the Ox.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/ox.jpg' alt='Ox' title='Ox' /></p>\n";
         } 
         else if (
    $CZIndex == 2) {
              echo 
    "<p>You were born under the sign of the Tiger.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/tiger.jpg' alt='Tiger' title='Tiger' /></p>\n";
         } 
         else if (
    $CZIndex == 3) {
              echo 
    "<p>You were born under the sign of the Rabbit.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/rabbit.jpg' alt='Rabbit' title='Rabbit' /></p>\n";
         } 
         else if (
    $CZIndex == 4) {
              echo 
    "<p>You were born under the sign of the Dragon.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/dragon.jpg' alt='Dragon' title='Dragon' /></p>\n";
         } 
         else if (
    $CZIndex == 5) {
              echo 
    "<p>You were born under the sign of the Snake.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/snake.jpg' alt='Snake' title='Snake' /></p>\n";
         } 
         else if (
    $CZIndex == 6) {
              echo 
    "<p>You were born under the sign of the Horse.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/horse.jpg' alt='Horse' title='Horse' /></p>\n";
         } 
         else if (
    $CZIndex == 7) {
              echo 
    "<p>You were born under the sign of the Goat.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/ram.jpg' alt='Goat' title='Goat' /></p>\n";
         } 
         else if (
    $CZIndex == 8) {
              echo 
    "<p>You were born under the sign of the Monkey.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/monkey.jpg' alt='Monkey' title='Monkey' /></p>\n";
         } 
         else if (
    $CZIndex == 9) {
              echo 
    "<p>You were born under the sign of the Rooster.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/rooster.jpg' alt='Rooster' title='Rooster' /></p>\n";
         } 
         else if (
    $CZIndex == 10) {
              echo 
    "<p>You were born under the sign of the Dog.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/dog.jpg' alt='Dog' title='Dog' /></p>\n";
         } 
         else if (
    $CZIndex == 11) {
              echo 
    "<p>You were born under the sign of the Pig.</p>\n";
              echo 
    "<p><img src='images/Zodiac_Lg/pig.jpg' alt='Pig' title='Pig' /></p>\n";
         } 
         
    $YearCount StatisticsForYear($Year);
         echo 
    "<p>You are person $YearCount to enter the year $Year.</p>\n";
         echo 
    "<p style = 'text-align:center'><a href='index.php?page=control_structures'>Back</a></p>\n";
    }

    $ShowForm TRUE;
    $errorCount 0;
    $Year date("Y");
    if (isset(
    $_POST['Submit'])) {
         
    $Year validateInput($_POST['Year'],"Birth Year");
         if (
    $errorCount==0)
              
    $ShowForm FALSE;
         else
              
    $ShowForm TRUE;
    }
    if (
    $ShowForm == TRUE) {
         if (
    $errorCount>0// if there were errors
              
    echo "<p>Please re-enter the form information below.</p>\n";
         
    displayForm($Year);

    else {
         
    displayResults($Year);
    }

    ?>


    </body>
    </html>
    Thanks
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    You actually see the PHP code?
    a) You're viewing the file directly, not through a web server.
    b) PHP isn't installed.
    c) Your file doesn't have the right extension.
    d) Something else.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by maineman
    PHP Code:
    <h2>using an <span class="code">if...else</spanstatement</h2
    Your code has a closing </code> tag, but it should be </span> like I've shown.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    thanks. ive got it sorted out except for this code:
    PHP Code:
    function StatisticsForYear($Year) {
        
    $retval 0;
          
        
    $counter_file "./Statistics/BirthYear_" $Year "_Count.txt";
            
        if (
    file_exists($counter_file)) {
            
    $Hits file_get_contents($counter_file);
            ++
    $Hits;
        }
        else{
            
    $Hits 1;
        }
        
    file_put_contents($counter_file$Hits);
        
    $retval $Hits;
        return(
    $retval);

    I get:

    Warning: file_put_contents(./Statistics/BirthYear_2012_Count.txt) [function.file-put-contents]: failed to open stream: No such file or directory in F:\EasyPHP-5.3.2i\www\BirthYear_ifelse.php on line 41
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by maineman
    I get:

    Warning: file_put_contents(./Statistics/BirthYear_2012_Count.txt) [function.file-put-contents]: failed to open stream: No such file or directory in F:\EasyPHP-5.3.2i\www\BirthYear_ifelse.php on line 41
    Does your webserver have sufficient privileges to create a new file if it doesn't exist?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    yes it does. i created the folders and now it works. thanks. i really hated to create folders for that. is there another way?

IMN logo majestic logo threadwatch logo seochat tools logo