Skip navigation
Currently Being Moderated

A simple PHP Question

May 1, 2013 8:53 PM

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!

 
Replies
  • Currently Being Moderated
    May 2, 2013 12:26 AM   in reply to Christ-Guard

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    May 2, 2013 11:05 PM   in reply to Christ-Guard

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points