Storing the value of a textbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manton
Forum Newbie
Posts: 10
Joined: Fri Apr 27, 2007 8:27 am
Location: Athens

Storing the value of a textbox

Post by manton »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,
I have a form with a textbox and A submit button. I want the value of the textbox to be stored in a variable so as to pass through to other pages.

I have reached this point:
page 1

Code: Select all

<?php

session_start();

if (isset($_POST['txtusername']))
   $_SESSION['test'] = $_POST['txtusername'];

?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="page2.php">
  <input type="submit" name="Submit" value="Submit" /></a>
  <input type="text" name="txtusername" />
</form>
</body>
</html>
page2

Code: Select all

<?php

session_start();
echo $_SESSION['test'];

?> 
<?php echo $_SESSION['test']; ?>
Unfortunately, the opening page is blank...I don't know what to do...please, help me


Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

You have set "action='page2.php'" in your <form>.

Either you can alter the "page2.php" to save the post-info, or change
the action to "page1.php" and add:

Code: Select all

header("location:page2.php");
Regards
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

First of all, the isset function on page1 doesn't really do anything as it will never be set on this page. You need to move that function over to page2 . I tested this on my system and it worked.

page1:

Code: Select all

<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="page2.php">
  <input type="submit" name="Submit" value="Submit" /></a>
  <input type="text" name="txtusername" />
</form>
</body>
</html>
page2.php

Code: Select all

<?php

session_start();
if (isset($_POST['txtusername'])){
   $_SESSION['test'] = $_POST['txtusername'];
}

echo $_SESSION['test'];

?>
<?php //echo $_SESSION['test']; ?>
I'm not sure why you ended php (?>) and then started again to echo the variable again. I commented that line out but if you want it take out the two forward slashes.

Wayne
Post Reply