Hello Chaps

New to PHP and just finished chapter 3 of Virkram Vaswani's "How to Do everything with PHP & MYSQL.

I am understanding everything so far but am stuck on an earlier segment. "Saving Form Input in Variables'. I can't move on to the next chapter without understanding this.

The example code given does not work, the HTML form works but the PHP side does not!! Maybe I am missing something?

To save a allot of grief could somebody kindly show me how to I can use and save HTML form data input by a user into a PHP variable?

All I would need is a simple code as well as a short explanation?

Many Thanks

    Create two pages.
    1) an html form, called form.php. Note that the form's action attribute is set to "form_handler.php" and the method is set to "post"

    <html>
    <body>
      <form method="post" action="form_handler.php">
        <input type="text" name="my_var" value="">
        <input type="submit" name="submit" value="submit">
      </form>
    </body>
    </html>
    

    2) a php script to handle your form submission, called form_handler.php or whatever you put as the action on your form above.

    if (isset($_POST['submit'])) {
      echo "the value of my_var in your form is " . $_POST['my_var'];
    } else {
      echo "you have not POSTed any data.  Try using the form, dummy!";
    }
    
      sneakyimp;10953511 wrote:

      Create two pages.
      1) an html form, called form.php. Note that the form's action attribute is set to "form_handler.php" and the method is set to "post"

      <html>
      <body>
        <form method="post" action="form_handler.php">
          <input type="text" name="my_var" value="">
          <input type="submit" name="submit" value="submit">
        </form>
      </body>
      </html>
      

      2) a php script to handle your form submission, called form_handler.php or whatever you put as the action on your form above.

      if (isset($_POST['submit'])) {
        echo "the value of my_var in your form is " . $_POST['my_var'];
      } else {
        echo "you have not POSTed any data.  Try using the form, dummy!";
      }
      

      Thanks for that:-)

      I am starting to get very frustrated with this book. Again I am coming accross examples that do not work. Looking at the switch statement, here is the exact example code in the book that does not work.

      switch ($country)

      case 'UK':
      $capital = 'London';
      break;

      case 'US':

          $capital = 'Washington';

      break;

      case 'FR':
      $capital = 'Paris';

      break;

      default:

      $capital ='unknown':
      break;

      This book seems to make big assumptions about it's reader, and expects and assumes the reader should no how to fill in gaps. In an earlier example of the ''if statment'', no variables had been declared in the example code, and the book assumes that you know exactly what to do and what to add.
      Grrr

        Does your book not use $POST and $GET (and all the other superglobal variables)? If not, sounds like you need to throw that book away and get one that's been written more recently (especially if it at least mentions PHP 6, but at the very least PHP 5).

        Also, for info on how to process external data (e.g. data coming from forms, cookies, the query string, etc.), see this page: [man]variables.external[/man].

          ok got it to work.

          does not return error anymore, but now I get a Blank screen when I run it, which is what I am suppose to get

          <?php

          if(!isset($country))$country="na";
          switch ($country){
          case 'UK':
          $capital = 'London';
          break;

          case 'US':
          $capital = 'Washington';
          break;

          case 'FR':
          $capital = 'Paris';
          break;

          default:
          $capital = 'unknown';
          break;
          }
          ?>

          Its just a shame that a code example in a book does not actually output anything and leaves it up to the reader to add code to the example so that he/she can understand it. How about a proper example!!

          When I was studying Pascal, I remember withion the many books, all example code was complete and showed a real world example with an output.

          I need me a knew book😃

          Sorry about the rant! haha

            Thanks

            I really can't continue with my book! Also just tried online PHP tutorials so I can understand this switch statement but getting errors again!!

            <html>
            <body>

            <?php
            switch ($x)
            {
            case 1:
            echo "Number 1";
            break;
            case 2:
            echo "Number 2";
            break;
            case 3:
            echo "Number 3";
            break;
            default:
            echo "No number between 1 and 3";
            }
            ?>

            </body>
            </html>

            Can someone please point me in the direction where I am given proper real world code examples that actually work.

              I don't see anything wrong with the switch() statements you've been posting... it's just that you haven't defined the variables being examined. For example, where does $x get defined? Or $country in your previous example?

              If it's supposed to come from external data, then define the variable before the switch() statement and it should work fine.

                I don't see anything wrong with the switch() statements you've been posting... it's just that you haven't defined the variables being examined. For example, where does $x get defined? Or $country in your previous example?

                You tell me, the book certainly does not!! The book just shows me a bit of switch statement code (literally) that does nothing 😃

                If it's supposed to come from external data, then define the variable before the switch() statement and it should work fine.

                How I am i supposed to know this, should the example not come with external data already which I can copy, and play with? I expect an example code to be complete. it's no good a book explaining how a statement works if there is no real world example to show how the code would be used.

                Notice: Undefined variable: x in C:\wamp\www\new 2.php on line 7

                Notice: Undefined variable: x in C:\wamp\www\new 2.php on line 10

                Notice: Undefined variable: x in C:\wamp\www\new 2.php on line 13
                No number between 1 and 3

                I get the above errors because as you mentioned the variables have not been defined which perhaps could/should have come from a HTML form?

                I want an example that is complete, with a form etc. After all I am a beginner: confused:

                Sorry, I am sounding like a right spoilt brat, just frustrated that I have a book that is not very useful at all.

                Thanks for your help and patients:-)

                  There is a certain 'awakening' that must happen when you program. An experienced programmer knows that you need to define a variable before you start using [man]if[/man], [man]switch[/man], or [man]while[/man] statements on that variable.

                  The 'errors' you are getting are actually notices. This means the php compiler is just letting you know that your code could be improved a bit. It should still run though.

                    PAPPACLART;10953786 wrote:

                    Opps, sorry this is the book I have. I have been throwing darts at it for the past 20 minuets! heheh

                    http://www.everythingphpmysql.com/

                    Just stop. Your book sucks, but you already knew that. I can recommend this one:

                    http://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166/ref=pd_bxgy_b_img_b

                    And in case anyone's wondering, I have no connection to the authors or anything like that... I just learned from an earlier edition a number of years ago.

                    By the way, if you see notices like that in your code, you should learn why you're seeing them and learn how to avoid them. Using uninitialized variables is bad form (illegal in some languages) for a reason - just because PHP will let you get away with it, it's not a good idea.

                      Write a Reply...