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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Reading input from form elements

    I am a complete newb to PHP and frankly programming in general.

    I am working through a book I purchased called PHP 5/MySql Programming for the "Absolute Beginner"

    That's not really important other then just to say I'm beginning to get perturbed and believe this book is full of coding errors.

    The exercise I am working on now is supposed to take information you enter into a form, and provide you with a PHP output. (I've got my Apache/PHP installed, and it's worked fine on the other couple of exercises)

    I will now post the code for the two files that you are supposed to create. (Note that I am actually posting you the exact code from the author, not my own which neither worked).

    As it stands now, when I submit the form borderMaker.html it sends me back the second file, borderMaker.php. All that is shown on the browser is the < h1 > Your Output at the beginning of the second file. None of the PHP script appears.


    Form file "borderMaker.html"
    Code:
    <html>
    <head>
    <title>Border Maker</title>
    </head>
    <body>
    <center>
    <h1>Border Maker</h1>
    <h3>Demonstrates how to read HTML form elements</h3>
    
    <form method = "post"
          action = "borderMaker.php">
    
    
    <h3>Text to modify</h3>
    <textarea name = "basicText"
              rows = "10"
              cols = "40">
    Four score and seven years ago our fathers brought forth on this
    continent a new nation, conceived in liberty and dedicated to the
    proposition that all men are created equal. Now we are engaged in a
    great civil war, testing whether that nation or any nation so
    conceived and so dedicated can long endure.
    </textarea>
    
    <table border = 2>
    <tr>
      <td><h3>Border style</h3></td>
      <td colspan = 2><h3>Border Size</h3></td>
    </tr>
    
    <tr>
    <td>
    <select name = borderStyle>
      <option value = "ridge">ridge</option>
      <option value = "groove">groove</option>
      <option value = "double">double</option>
      <option value = "inset">inset</option>
      <option value = "outset">outset</option>
    </select>
    </td>
    <td>
    
    
    <select size = 5
            name = borderSize>
      <option value = "1">1</option>
      <option value = "2">2</option>
      <option value = "3">3</option>
      <option value = "5">5</option>
      <option value = "10">10</option>
    </select>
    </td>
    
    <td>
    <input type = "radio"
           name = "sizeType"
           value = "px">pixels<br>
    <input type = "radio"
           name = "sizeType"
           value = "pt">points<br>
    <input type = "radio"
           name = "sizeType"
           value = "cm">centimeters<br>
    <input type = "radio"
           name = "sizeType"
           value = "in">inches<br>
    </td>
    </tr>
    </table>
    
    <input type = "submit"
           value = "show me">
    
            
    </form>
    
    </center>
    </body>
    </html>
    PHP Output File "borderMaker.php"
    Code:
    <html>
    <head>
    <title>Your Output</title>
    </head>
    <body>
    <h1>Your Output</h1>
    <center>
    <?
    $theStyle = <<<HERE
    "border-width:$borderSize$sizeType;
    border-style:$borderStyle;
    border-color:green"
    HERE;
    
    print "<div style = $theStyle>";
    print $basicText;
    print "</span>";
    
    ?>
    </center>
    
    </body>
    </html>
    On an earlier exercise, there was a problem that involved the "Register Globals" option in the PHP.ini file. Not sure if that has anything to do with why this exercise won't work or not.

    It's frustrating that even that author's code will not work, and I would greatly appreciate someone with experience to take a look and see if they can figure out why I might be having troubles. Thanks again.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    The problem is with register globals, however, don't turn them on to solve this. Learn how form data should be accessed in your code.

    When the form method is POST, the input fields will be present in the $_POST array. Use the following code to display the entire contents of the $_POST array to see what it contains -
    PHP Code:
    echo "<pre>";
    print_r($_POST);
    echo 
    "</pre>"
    Register globals were turned off by default in php 4.2 some time in the year 2002. No new code, tutorials, or books should have been written after that point in time that relied on them being on. Register globals have been completely eliminated in PHP6. The reason they were turned off and are being eliminated is because they were a huge blunder and a security problem. By "magically" populating same name program/GET/POST/COOKIE/SESSION variables, you don't know where an actual value came from and this allowed program and session variables (which you would hope a visitor could not change) to be modified by simply putting parameters on the end of the url. There have even been several recent posts here where values in session variables have been overwritten by simply having a form field with the same name, causing unexplained operation of the code.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I went in and added the code you posted to my script as the following:

    "borderMaker.php"

    Code:
    <?
    $theStyle = <<<HERE
    "border-width:$borderSize$sizeType;
    border-style:$borderStyle;
    border-color:green"
    HERE;
    
    print "<div style = $theStyle>";
    print $basicText;
    print "</span>";
    echo "<pre";
    print_r($_POST);
    echo "</pre>";
    
    ?>
    When I submit the form, I now get this output.
    Your Output
    Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. [borderStyle] => double [borderSize] => 10 [sizeType] => pt )
    Is this the $_POST array?
    [borderStyle] => double [borderSize] => 10 [sizeType] => pt )
    How can I go about using this to make the border function work?
    Thanks. Sorry for my stoopid questions.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    Yep. The corresponding post variables are -
    $_POST['borderStyle']
    $_POST['borderSize']
    $_POST['sizeType']
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    Clueless_Coder (10-28-2007)

  • #5
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    How would I work that into my code to make it work?

  • #6
    Regular Coder
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    924
    Thanks
    10
    Thanked 56 Times in 55 Posts
    Code:
    <?
    $borderSize = $_POST['borderStyle'];
    $sizeType  = $_POST['borderStyle'];
    $borderStyle = $_POST['borderSize'];
    
    $theStyle = "border-width:$borderSize$sizeType; border-style:$borderStyle; border-color:green"
    
    print "<div style = $theStyle>";
    print $basicText;
    print "</span>";
    echo "<pre";
    print_r($_POST);
    echo "</pre>";
    
    ?>
    Last edited by jlhaslip; 10-28-2007 at 11:14 PM. Reason: add code tags

  • #7
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    jihaslip,

    When I run the following code in my borderMaker.php file I still get the same thing.

    Using this,
    Code:
    <html>
    <head>
    <title>Your Output</title>
    </head>
    <body>
    <h1>Your Output</h1>
    <center>
    <?
    $borderSize = $_POST['borderStyle'];
    $sizeType  = $_POST['borderStyle'];
    $borderStyle = $_POST['borderSize'];
    
    $theStyle = "border-width:$borderSize$sizeType; border-style:$borderStyle; border-color:green";
    
    print "<div style = $theStyle>";
    print $basicText;
    print "</span>";
    echo "<pre";
    print_r($_POST);
    echo "</pre>";
    ?>
    </center>
    
    </body>
    </html>
    Still gets me this output
    Your Output
    Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. [borderStyle] => ridge [borderSize] => 3 [sizeType] => cm )


  •  

    Posting Permissions

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