4 Replies Latest reply: May 2, 2013 11:05 PM by osgood_ RSS

    A simple PHP Question

    Christ-Guard Community Member

      Hey guys,

         I am learning some basic PHP to make a simple form, and I have a quick question. I have these two test PHP files, Form.php and acion.php. What you enter in on the form will then be posted via $_POST to action.php that gets displayed. here are the test sites:

       

      http://christianstest.info/phptest/Form.php

      http://christianstest.info/phptest/action.php

       

      And the code for the two, verry simple:

       

      Form.php

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>PHP Form Test</title>
      </head> 
      <body>
      <form action="action.php" method="post">
      <p>Your name: <input type="text" name="name" /></p>
      <p>Your age: <input type="text" name="age" /></p>
      <p><input type="submit" /></p> </form> 
      </body>
      </html>

       

      and action.php


      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Untitled Document</title>
      </head> 
      <body>
      Hi <?php echo htmlspecialchars($_POST['name']); ?>. You are <?php echo (int)$_POST['age']; ?> years old.
      </body>
      </html>

       

       

      Now what I want to do is have action.php store the data entered from Form.php so that someone could link directly to action.php and see whatever the last user entered into Form.php. As of right now, if you go directly to an uncached action.php it displays nothing for name and age respectivly. how can I get this data to stick? If that is in fact not a simple question, can you point me to a tutorial? Thanks!

        • 1. Re: A simple PHP Question
          osgood_ MVP

          form.php

           

          <?php session_start(); ?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

          <html xmlns="http://www.w3.org/1999/xhtml">

          <head>

          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

          <title>PHP Form Test</title>

          </head>

          <body>

          <form action="action.php" method="post">

          <p>Your name: <input type="text" name="name" /></p>

          <p>Your age: <input type="text" name="age" /></p>

          <p><input name="submit" type="submit" /></p>

          </form>

          </body>

          </html>

           

           

           

           

          action.php

           

          <?php session_start(); ?>

          <?php if (array_key_exists('submit', $_POST)) { ?>

          <?php

          $_SESSION['name'] = $_POST['name'];

          $_SESSION['age'] = $_POST['age'];

          ?>

          <?php } ?>

           

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

          <html xmlns="http://www.w3.org/1999/xhtml">

          <head>

          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

          <title>Untitled Document</title>

          </head>

          <body>

          Hi <?php echo $_SESSION['name']; ?>.

          You are <?php echo $_SESSION['age']; ?> years old.

          </body>

          </html>

          • 2. Re: A simple PHP Question
            Christ-Guard Community Member

            Thanks I will try this first thing tomorow.

            • 3. Re: A simple PHP Question
              Christ-Guard Community Member

              Hmmm... still did not work. If I enter in "Christian" and "25" then action.php shows "Hi Christian, you are 25 years old", but then when another computer goes directly to action.php without ever touching Form.php it has no data so it just says "Hi . You are years old". I am wanting the data to save to either action.php or written to another server base file that is then read from action.php

              • 4. Re: A simple PHP Question
                osgood_ MVP

                Christ-Guard wrote:

                 

                but then when another computer goes directly to action.php without ever touching Form.php it has no data so it just says "Hi . You are years old". I am wanting the data to save to either action.php or written to another server base file that is then read from action.php

                 

                 

                 

                I guess you may be able to write the information to a database and retrieve the last database entry. Never done it before so without much experimenting I can't really help you.