PHP doesn't allow me to manage forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Village

    PHP doesn't allow me to manage forms

    I have installed PHP a number of times following tutorials from online
    and from a book. I have been able to process simple functions such at
    date() but when it comes to simply sending information from a HTML form
    to a PHP page to display it doesn't work.

    I know it isn't my programming as i have copied it word for word from a
    book, and even used one of their examples from the site.

    The code I have used in the PHP page is:

    <?php
    /* This page receives and handles the data generated by "form.htm". */
    print "Your first name is $FirstName.<BR> \n";
    print "Your last name is $LastName.<BR>\ n";
    print "Your E-mail address is $email.<BR>\n";
    print "This is what you had to say:<BR>\n$Comm ents<BR>\n";
    ?>

    and all of those variables relate to a text field in the form on the
    HTML page (form.htm).

    Any suggestions on how to sort this out?

    Thank you very much

  • Erwin Moller

    #2
    Re: PHP doesn't allow me to manage forms

    Village wrote:
    [color=blue]
    > I have installed PHP a number of times following tutorials from online
    > and from a book. I have been able to process simple functions such at
    > date() but when it comes to simply sending information from a HTML form
    > to a PHP page to display it doesn't work.
    >
    > I know it isn't my programming as i have copied it word for word from a
    > book, and even used one of their examples from the site.
    >
    > The code I have used in the PHP page is:
    >
    > <?php
    > /* This page receives and handles the data generated by "form.htm". */
    > print "Your first name is $FirstName.<BR> \n";
    > print "Your last name is $LastName.<BR>\ n";
    > print "Your E-mail address is $email.<BR>\n";
    > print "This is what you had to say:<BR>\n$Comm ents<BR>\n";
    > ?>
    >
    > and all of those variables relate to a text field in the form on the
    > HTML page (form.htm).
    >
    > Any suggestions on how to sort this out?
    >
    > Thank you very much[/color]

    Hi,

    Well, you didn't show us all your code, so it is hard to say what went
    wrong.

    Try this:
    send.html

    *************** *************** *********
    <html>
    <body>
    <form action="receive .php" method="Post">
    firstname:<inpu t type="text" name="firstname ">
    <br>
    LastName<input type="text" name="LastName" >
    <br>
    email:<input type="text" name="email">
    <br>
    Comments:<input type="text" name="Comments" >
    <br>
    <input type="submit" value="Post me please">
    </form>
    </body>
    </html>

    *************** *************** *********

    and the file receive.php:

    *************** *************** *********
    <?
    $firstname=$_PO ST["firstname"];
    $LastName=$_POS T["LastName"];
    $email=$_POST["email"];
    $Comments=$_POS T["Comments"];
    ?>
    <html>
    <body>
    I received:
    <br>
    Firstname: <? echo $firstname; ?><br>
    LastName: <? echo $LastName; ?><br>
    email: <? echo $email; ?><br>
    Comments: <? echo $Comments; ?><br>
    </body>
    </html>

    *************** *************** *********


    That code should always work.

    Regards,
    Erwin Moller

    Comment

    • Peter Fox

      #3
      Re: PHP doesn't allow me to manage forms

      Following on from Village's message. . .[color=blue]
      >I have installed PHP a number of times following tutorials from online
      >and from a book. I have been able to process simple functions such at
      >date() but when it comes to simply sending information from a HTML form
      >to a PHP page to display it doesn't work.
      >
      >I know it isn't my programming as i have copied it word for word from a
      >book, and even used one of their examples from the site.[/color]
      The book was taking some deprecated shortcuts.
      (See 'register globals' in the manual)

      try
      print_r($_POST) ;
      or
      print_r($_REQUE ST);
      to see what has been passed to PHP, then you can pick the bits you want
      out of the array, *validate them*, *validate them again* then use them.

      --
      PETER FOX Not the same since the bolt company screwed up
      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
      2 Tees Close, Witham, Essex.
      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

      Comment

      • Village

        #4
        Re: PHP doesn't allow me to manage forms

        Brilliant, that works fine. Thanks. I will look up about the Register
        Globals thing to see what it is.

        Comment

        • Tim Van Wassenhove

          #5
          Re: PHP doesn't allow me to manage forms

          On 2006-01-06, Erwin Moller <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote:[color=blue]
          > $firstname=$_PO ST["firstname"];
          > $LastName=$_POS T["LastName"];
          > $email=$_POST["email"];
          > $Comments=$_POS T["Comments"];[/color]

          I've got the feeling that tutorials should spend more attention to cleaning
          input and output so the student can't shoot himself in the foot.

          $html = array();
          $html['firstname'] = htmlentities($_ POST['firstname'], 'UTF-8');
          ....


          echo "Your firstname is: {$html['firstname']} <br/>";
          ...


          --
          Met vriendelijke groeten,
          Tim Van Wassenhove <http://timvw.madoka.be >

          Comment

          • Erwin Moller

            #6
            Re: PHP doesn't allow me to manage forms

            Tim Van Wassenhove wrote:
            [color=blue]
            > On 2006-01-06, Erwin Moller
            > <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote:[color=green]
            >> $firstname=$_PO ST["firstname"];
            >> $LastName=$_POS T["LastName"];
            >> $email=$_POST["email"];
            >> $Comments=$_POS T["Comments"];[/color]
            >
            > I've got the feeling that tutorials should spend more attention to
            > cleaning input and output so the student can't shoot himself in the foot.
            >
            > $html = array();
            > $html['firstname'] = htmlentities($_ POST['firstname'], 'UTF-8');
            > ...
            >
            >
            > echo "Your firstname is: {$html['firstname']} <br/>";
            > ..
            >
            >[/color]

            Hi/hoi Tim,

            I agree with that.
            I know I had my fair share of 'obscure bugs' when I started with PHP, and I
            guess 90% had to do with characters: encoding/in-out database/show them in
            html, show them in a textfield of a form, etc. etc.
            You know what I am talking about. :P

            'obscure bugs' in a ironic way because it was of course me who scewed up by
            not paying attention to them from the start.
            They are excactly the kind of things that make a habit of returning later in
            the project, and always in a worse form, and of course close to deadline.
            ;-)

            But I didn't want to confuse the OP with that because he clearly had
            problems with the concept of forms and superglobals $_POST and $_GET.

            btw, I didn't know that UTF-8 directive for htmlentities.
            Now I do. :-)

            Regards,
            Erwin Moller

            Comment

            • Darkstar 3D

              #7
              Re: PHP doesn't allow me to manage forms

              Sounds to me like that book sucks. Especially if it didn't discuss
              register globals before taking the short way out.

              Comment

              • Michael Trausch

                #8
                Re: PHP doesn't allow me to manage forms

                Village wrote:[color=blue]
                >
                > Brilliant, that works fine. Thanks. I will look up about the Register
                > Globals thing to see what it is.
                >[/color]

                A good bit of advice: Don't use, nor rely on, register globals. That
                functionality is going away in the future, according to the PHP web
                site, I think it's slated for removal entirely in PHP 6.

                Use the Superglobals instead. Some of the common ones are $_GET,
                $_POST, and $_COOKIE. There are others, also. It's definitely
                preferable to use them, however, because with register globals (just as
                an example), let's say you have a POST form with a hidden value UID and
                the user wanted to attempt to override it with a GET request appending
                UID to the URL. The opposite could also be true, but not as likely.
                The idea is that by using the superglobals, everything is separated into
                different places, and it makes life somewhat more secure, easier, and
                more intuitive to deal with.

                You should get a book on PHP 5. :)

                Later,
                Mike

                Comment

                Working...