Why won't this simple script work?

Discussion in 'PHP' started by keithhulse, Jun 27, 2008.

  1. #1
    Hi, I am totally new to PHP and have been working with Larry Ullman's "PHP for the World Wide Web". Creating the below form:

    <HTML>
    <HEAD>
    <TITLE>HTML Form</TITLE>
    </HEAD>
    <BODY>
    <FORM ACTION="HandleForm.php" METHOD=POST>
    First Name <INPUT TYPE=TEXT NAME="FirstName" SIZE=20><BR>
    Last Name <INPUT TYPE=TEXT NAME="LastName" SIZE=40><BR>
    E-mail Address <INPUT TYPE=TEXT NAME="Email" SIZE=60><BR>
    Comments <TEXTAREA NAME="Comments" ROWS=5 COLS=40></TEXTAREA><BR>
    <INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
    </FORM>
    </BODY>
    </HTML>

    and handler

    <HTML>
    <HEAD>
    <TITLE>Form Results/Using Strings</TITLE>
    </HEAD>
    <BODY>
    <?php
    /* This page receives and handles the data generated by "form.html". */
    $FirstName = trim($FirstName);
    $LastName = trim($LastName);
    $Email = trim($Email);
    $Comments = trim($Comments);
    $Name = $FirstName . " " . $LastName;
    print ("Your name is $Name.<BR />");
    print ("Your E-mail address is $Email.<BR />");
    print ("This is what you had to say:<BR /> $Comments<BR />");
    $Name = urlencode($Name);
    print ("<P>Click <A HREF=\"welcome.php?Name=$Name\"> here</A> to see your personalized greeting!");
    ?>
    </BODY>
    </HTML>

    results in the text being produced but not the variables.

    Using an example from w3schools.com the form:

    <html>
    <body>

    <form action="welcome2.php" method="post"><br />
    First Name: <input type="text" name="firstname" /><br />
    Last Name: <input type="text" name="lastname" /><br />
    Comments: <textarea name="comments" cols=20" rows="10">Enter you comments here</textarea><br />
    <input type="submit" />
    <input type="reset" />
    </form>

    </body>
    </html>

    and handler:

    <html>
    <body>

    Welcome <?php echo $_POST["firstname"]; ?> <?php echo $_POST["lastname"];?><br />
    You said: <?php echo $_POST["comments"]; ?><br />


    </body>
    </html>

    produces what you would expect - it works.

    Could someone tell me why this is happening before I travel any further in Ullman's book.

    Many thanks,
    Keith Hulse
     
    keithhulse, Jun 27, 2008 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1
    for attributes of an element
    (for the input-element : type, cols, rows)
    enclose the value of the attribute in quotes.

    <INPUT TYPE=SUBMIT...>

    <input type="submit"..>

    2
    if you post a page,
    the server stores all the input in the $_POST array for your handler form
    so in your handler page
    you can retrieve the data as $_POST['variable'] from the server,

    $_POST['Comments']
     
    juust, Jun 27, 2008 IP
  3. keithhulse

    keithhulse Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Juust, thanks for the reply. I made the changes you suggested to the first form but with no success. The first form and handler were taken directly from Ullman's book - why doesn't the first handler work?

    Many thanks,
    Keith Hulse
     
    keithhulse, Jun 27, 2008 IP
  4. robokoder

    robokoder Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think you missed the beginining of the handler

    try:

    <?php

    $FirstName = $_REQUEST['FirstName'];
    $LastName = $_REQUEST['LastName'];

    ?>

    <HTML>
    <HEAD>
    <TITLE>Form Results/Using Strings</TITLE>
    </HEAD>
    <BODY>
    <?php
    /* This page receives and handles the data generated by "form.html". */
    $FirstName = trim($FirstName);
    $LastName = trim($LastName);
    $Email = trim($Email);
    $Comments = trim($Comments);
    $Name = $FirstName . " " . $LastName;
    print ("Your name is $Name.<BR />");
    print ("Your E-mail address is $Email.<BR />");
    print ("This is what you had to say:<BR /> $Comments<BR />");
    $Name = urlencode($Name);
    print ("<P>Click <A HREF=\"welcome.php?Name=$Name\"> here</A> to see your personalized greeting!");
    ?>
    </BODY>
    </HTML>


    Now the variables are linked to the values submitted by the form- you see?
    With the W3Schools example, they just used the $_POST['XXX'] directly which is why it works

    Note that forms can go via POST or GET (PHP sees as $_POST['XXX'] or $_GET['XXX'] respectively), but you can use $_REQUEST['XXX'] which will check both
     
    robokoder, Jun 27, 2008 IP
  5. SLoB

    SLoB Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    don't forget to run those user inputs through an xss/regex function to remove any nasties #1 rule do not trust user input ;)
     
    SLoB, Jun 27, 2008 IP
  6. keithhulse

    keithhulse Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi, thanks robokoder, that did the trick. I copied the code straight from the book so its knocked my confidence in the book but your explanation made sense.

    Many thanks,
    Keith Hulse
     
    keithhulse, Jun 27, 2008 IP