Hi All,
I have an assignment to complete and I just can't get it and I was hoping someone could help me out. What I need to do is:
Call the session_start function. - <?php
-
session_start();
-
?>
Detect whether or not the name session variable has a value; if so, display name and age as text; if not, display the name and age input fields. - First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" name="submit" value="Submit" />
-
-
<?php
-
if (isset($_POST['submit'])) {
-
$_session['fname'] = $_POST['fname'];
-
$_session['age'] = $_POST['age'];
-
}
-
?>
How can have the text fields replaced with the users Name and age after they type it in and submit the form? So, when they go back to the that page, they will already see their name and age printed out in place of the text fields. I also upload an image of what it is suppose to look like.
Thanks in advance for your help,
T
14 1569
Your fields and button must be placed inside the form tag. (<form method="post"> fields here!.. </form>)
Try to run this codes. . . - <?php
-
session_start();
-
ob_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>Sample</title>
-
</head>
-
-
<body>
-
-
<form method="post">
-
First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" name="submit" value="Submit" />
-
</form>
-
-
</body>
-
</html>
-
<?php
-
if(isset($_POST['submit']))
-
{
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
//header('Location: page_to_go.php');
-
echo $_SESSION['fname'].' '.$_SESSION['age'];
-
}
-
?>
Hi Exequiel,
Thank you for the reply. I tried the code you suggested and it still doesn't work. Here's what I have: - <?php
-
session_start();
-
ob_start();
-
?>
-
-
<?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
-
<!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 name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
-
<link rel="stylesheet" type="text/css" href="css/styles.css" />
-
<script type="text/javascript" src="js/catalog.js"></script>
-
<title>Scion tC Catalog</title>
-
</head>
-
<body>
-
<a name="Top" id="Top"></a>
-
-
<form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php"
-
-
method="post">First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input
-
-
type="submit" name="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />
-
<br />
-
<table class="table1">
-
<tr>
-
<th>Select</th>
-
</tr>
-
<tr>
-
<td class="radio button"><input type="radio" name="scion" id="r1" value="You selected the 2010 Scion tC RS" /><br
-
-
/></td>
-
</tr>
-
<tr>
-
<td class="radio button"><input type="radio" name="scion" id="r2" value="You selected the 2012 Scion tC RS" /><br
-
-
/></td>
-
</tr>
-
<tr>
-
<td class="radio button"><input type="radio" name="scion" id="r3" value="You selected the 2013 Scion tC RS" /><br
-
-
/></td>
-
</tr>
-
<tr>
-
<td class="radio button"><input type="radio" name="scion" id="r4" value="You selected the 2014 Scion tC" /><br
-
-
/></td>
-
</tr>
-
</table></form>
-
-
</body>
-
</html>
-
-
<?php
-
if(isset($_POST['submit']))
-
{
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
//header('Location: catalog.php');
-
echo $_SESSION['fname'].' '.$_SESSION['age'];
-
}
-
?>
You need to first check whether or not the name variable in the session is set and then choose what to output depending on if it's set or not.
If there's a session started, I need to have their name (fname) and age (age) displayed where the input text fields should be. If the session is not started, then the whole form should display with text fields for the user's input. Would it look something like this: - <?php
-
if(isset($_POST['submit']))
-
{
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
//header('Location: catalog.php');
-
echo $_SESSION['fname'].' '.$_SESSION['age'];
-
}
-
if (isset($_POST['fname']) && ($_POST['age']))
-
$session = $_POST['fname'] && $_POST['age'];
-
session_start();
-
$_SESSION['fname'] && $_SESSION['age'] = $session;
-
?>
I'm sorry if this isn't close. I'm not a very good programmer and I'm still trying to learn.
Thanks,
T
It needs to look more like this - <?php
-
if(isset($_POST['submit'])) {
-
set your session variables
-
output your values
-
} else {
-
output your form
-
}
-
?>
I tried this: - <form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post">
-
<?php
-
if(isset($_POST['submit'])) {
-
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
-
if(isset($_SESSION['fname'])) {
-
//header('Location: catalog.php');
-
echo $_SESSION['fname'].' '.$_SESSION['age'];}
-
else {
-
echo 'First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> '; }
-
}
-
?>
-
<input type="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />
It's not displaying the text fields or the output. Am I getting close?
T
That structure doesn't follow my example.
Ok, so what am I doing wrong? Please remember I trying to learn this. I very green when it comes to programming. Should it look like this: - <?php
-
if(isset($_POST['submit'])) {
-
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
-
if(isset($_SESSION['fname'])) {
-
//header('Location: catalog.php');
-
echo $_SESSION['fname'].' '.$_SESSION['age'];}
-
else {
-
echo '<form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post">
-
-
First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" value="Submit" />
-
-
<input type="button" onclick="formReset()" value="Reset form" />'; }
-
}
-
?>
Compare your structure to my structure.
To repeat, my structure is this: - <?php
-
if(isset($_POST['submit'])) {
-
set your session variables
-
output your values
-
} else {
-
output your form
-
}
-
?>
Your structure is this: - <?php
-
if(isset($_POST['submit'])) {
-
set your session variables
-
-
if(isset($_SESSION['fname'])) {
-
output your values
-
} else {
-
output your form
-
}
-
}
-
?>
As your can see, your structure is not the same as mine.
So from what you said above, it should look like this? - <?php
-
if(isset($_POST['submit'])) {
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
//header('Location: catalog.php');
-
echo $_SESSION['fname'].' '.$_SESSION['age'];}
-
else {
-
<form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post"> First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />';
-
}
-
?>
That looks about right.
But I see another error, you didn't give your submit button a name. So it's not going to be set when it goes to check it.
Ok, I just tried the code above and it still doesn't show the name and age in replace of the text fields after the I go back to my catalog page. This is what I have: - <?php
-
if(isset($_POST['submit'])) {
-
$_SESSION['fname']=$_POST['fname'];
-
$_SESSION['age']=$_POST['age'];
-
echo $_SESSION['fname'].' '.$_SESSION['age'];}
-
else {
-
echo '<form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post">';
-
echo 'Name: <input type="text" name="fname" />';
-
echo 'Age: <input type="text" name="age" />';
-
echo '<input type="submit" name="submit" value="Submit" />';
-
echo '<input type="button" onclick="formReset()" value="Reset form" />';
-
}
-
?>
I really do appreciate your help with this.
T
Works fine for me.
Assuming of course that everything else is correct. I am assuming all the code before and after this block of code is correct and that this code is in fact in the thankyou.php page. By which I mean you're submitting the page to itself.
And if by going back, you mean clicking the back button on your browser, that doesn't refresh the page.
@LugNut29
Its not still working because of the action on your form, action="thankyou.php". Your code that you replied to me is right, only the thankyou.php is the problem. If you try to remove the action="thankyou.php" on your form the code is working fine. but if you really want to display the session on thankyou.php try my code. I've created thankyou.php. can you post your thakyou.php ? -
<?php
-
session_start();
-
ob_start();
-
?>
-
-
<?php
-
echo $_SESSION['fname'].' '.$_SESSION['age'];
-
?>
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: chints |
last post by:
Hi,
I have a very unique problem, i am submitting from a pdf
form, when submitted goes to a asp page, which saves the
data and redirects to a different page after saving the
data. I am writing a...
|
by: shank |
last post by:
I need advice on how to get started with this project. I want a page where a
user can manually enter an order. Our users know our order# system and it's
much easier to manually enter items as...
|
by: Matt |
last post by:
I want to know how online order processing implemented? For example, the
online shopping mall such as Amazon receives order, it will communicate with
credit card company, and it will add that order...
|
by: Roy |
last post by:
Anyone have any links and/or code samples demonstrating how this can be
done? Current procedure is that john doe clicks an item on a datagrid
and after however long, gets the info he wants.
What...
|
by: CK |
last post by:
I have a web page called PageOne.aspx which is supposed to do a long
process but I don't need to show any results to the client, so I want
to redirect the client to PageTwo.aspx right in the...
|
by: ~Maheshkumar.R |
last post by:
hi groups,
I have developed one FTP application in ASP.NET, When i upload a file, i
want to show something on screen like " Please wait ...! Your file is being
uploading........"
As soon the file...
|
by: seanmayhew |
last post by:
I have a form page that that while editing saves the data to an xml doc
before submitting to db. On each page
unload it saves the xmldoc as the user can add multiple items to the
company like...
|
by: Janusz Jezowicz |
last post by:
Hello!
I would like to have one page on the server, which would be a target
processing page for a number of other aspx pages.
E.g
Processing page
\portal_page.aspx
Target pages
|
by: Tony Cheng |
last post by:
Hello everybody,
I got a aspx which have several user controls on it. My question is that
what is the order of ASP.NET execute the page and user control ? does it
like this :
Page Init ->...
|
by: pattersonc |
last post by:
Hello! I'm new to this forum and also new to using ASP and SQL and Access. I am in need of some help. I'm trying to make a database to help my football team. I've pretty much got the whole thing...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |