472,362 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,362 software developers and data experts.

trouble with arrays + sessions

I just started with php and I'm trying to make a simple interface as
follows:

- user is asked to input an integers, for example: how many students do
you want to enter?
- user is then shown a page with number of text boxes = number he gave
at the previous page
- user fills out the test boxes with names of students and clicks
submit
- the user is sent to another page where the above names are output to
the screen

here is the code for the relevant pages:
First i ask for how many text boxes the user would like...
================================================== ====================
<?
session_start();
?>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration</title>
</head>

<body>
<table>
<tr>
<td>
<p>
<?
if($_SESSION['sizeerror']) {
echo "Inputs must be
integers<br><br>";
$_SESSION['sizeerror'] =
0;
}
?>
<form method="POST"
action="transition.php">
How many instructors (TAs,
professors, etc) will need access to the system?<br>
<input type="text"
name="numinstructors" size="20"><br><br>
How many students would you like
to add?<br>
<input type="text"
name="numstudents" size="20"><br><br><br>
<input type="submit"
value="Submit" name="B1">
</form>
</p>
</td>
</tr>
</table>
</body>

</html>
================================================== ========================

if the input was correct (both were integers) you get redirected to the
next page...this part works fine (or so it seems). it displays the
correct number of text-boxes...
================================================== ========================
<?
session_start();
?>
<HTML>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration</title>
</head>

<body>
<table align="center">
<form method="POST" action="submit.php">
<tr>
<p>
Coursename:&nbsp;&nbsp;&nbsp;<input type="text"
name="coursename" size="20"><br><br>
</p>
</tr>
<tr>
<p>
Instructor ECS Lab Accountnames:<br><br>

<?
for( $k = 0; $k <
$_SESSION['numinstructors']; $k++){
echo "<input type=\"text\"
name=\"instructor[$k]\" size=\"20\"><br><br>";
}
?>
</p>
</tr>
<tr>
<p>
<br><br>Student ECS Lab Accountnames:<br><br>

<?
for( $i = 0; $i <
$_SESSION['numstudents']; $i++ ){
echo "<input type=\"text\"
name=\"student[$i]\" size=\"20\"><br><br>";
}
?>

<input type="submit" value="Submit" name="B1">
</p>
</tr>
</form>
</table>
</body>

</html>
================================================== =======================

submitting the names sends you to the following page...
================================================== ========================
<?
session_start();

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

for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
$idx = 'student[' . $i . ']';
$student[$i] = $_POST[$idx];
$_SESSION[$idx] = $student[$i];
}

for( $k = 0; $k < $_SESSION['numinstructors']; $k++ ){
$instructor[$k] = $_POST["instructor[$k]"];
$_SESSION["instructor[$k]"] = $instructor[$k];
}
?>
<HTML>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration Successful</title>
</head>

<body>
<?
for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
$x = 'student[' . $i . ']';
echo $_SESSION[$x];
echo "<br>";
}
echo "now instructors<br>";
for( $k = 0; $k < $_SESSION['numinstructors']; $k++ ){
$x = 'instructor[' . $k . ']';
echo $_SESSION[$x];
echo "<br>";
}
?>
</body>

</HTML>
================================================== ======================
i expect it to print something like:
student1
student2
student3
now instructors
teacher1
teacher2

but instead it prints:

now instructors
(so, 3 blank lines, "now instructors", then 2 blank lines)
Anyone have any idea what I'm doing wrong?
Thanks in advance,
--Lucas

Jul 17 '05 #1
3 1480
ugh... where to begin...
1st is this just a general "how do sessions work test"?
because sessions appear to be completely unnecessary here.

here's a working "transition.php"
<?php
session_start();
?>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration</title>
</head>
<body>
<form method="POST" action="submit.php">
Coursename:&nbsp;&nbsp;&nbsp;<input type="text" name="coursename"
size="20"><br><br>
<p>
Instructor ECS Lab Accountnames:<br>
<?php
for( $k = 0; $k < $_POST['numinstructors']; $k++)
echo '<input type="text" name="instructor['.$k.']" size="20"
/><br/>'."\n";;
?>
</p>
<P>
Student ECS Lab Accountnames:<br>
<?php
for( $i = 0; $i < $_POST['numstudents']; $i++ )
echo '<input type="text" name="student['.$i.']" size="20" /><br
/>'."\n";
?>
<INPUT type="hidden" name="numinstructors"
value="<?=$_POST['numinstructors']?>" />
<INPUT type="hidden" name="numstudents"
value="<?=$_POST['numstudents']?>" />
<?php
// or, if you insist on using sessions, you need to set your session
values here
// they don't magically set themselves
// $_SESSION['numinstructors'] = $_POST['numinstructors']
// $_SESSION['numstudents'] = $_POST['numstudents']
?>
<input type="submit" value="Submit" name="B1">
</p>
</form>
</body>

</html>

***********************************

in submit.php you've got a few probs

you can use:
for( $i = 0; $i < $_POST['numstudents']; $i++ )
// or $_SESSION['numstudents'] if you've defined it (you hadn't)
{
$name = 'student[' . $i . ']';
$_SESSION[$idx] = $_POST[$name];
}

but down below you're echoing
$_SESSION[1]
should be
$_SESSION['student[1]']
at least the way you've set previous pages.

and again, sessions weren't necessary... unless you're doing something
with these value in later pages...

Jul 17 '05 #2
I actually do have a transition.php. I guess I should have included it
instead of just assuming people would know that it is there. The page
that you thought was not transition.php but actually register.php. The
following is the page that should come between the first group of code
I put up and the second....sorry for the confusion

==================================================
<?
session_start();

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

$numinstructors = $_POST['numinstructors'];
$_SESSION['numinstructors'] = $numinstructors;
?>
<HTML>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System...</title>
</head>

<?
if(ctype_digit($numstudents) && ctype_digit($numinstructors)) {
echo "<meta http-equiv=\"refresh\"
content=\"0;URL=http://node1\
7/register.php\">";
}else {
$_SESSION['sizeerror'] = 1;
echo "<meta http-equiv=\"refresh\"
content=\"0;URL=http://node1\
7/classsize.php\">";
}
?>

<body>
</body>

</html>

Jul 17 '05 #3
I have modified the following lines:

echo "<input type=\"text\" name=\"instructor[$k]\"
size=\"20\"><br><br>";
and
echo "<input type=\"text\" name=\"student[$i]\" size=\"20\"><br><br>";

to
echo '<input type="text" name="student['.$i.']" size="20"><br>';
and
echo '<input type="text" name="instructor['.$k.']" size="20"><br>';

like you said....I also modified my final page (where I'm trying to
echo the names inputted by the user) to:

for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
$x = 'student[' . $i . ']';
echo $_SESSION['student[1]'];
echo "<br>";
}
echo "<br>now instructors<br>";
for( $k = 0; $k < $_SESSION['numinstructors']; $k++ ){
$x = 'instructor[' . $k . ']';
echo $_SESSION['instructor[1]'];
echo "<br>";
}

but it still does not print out any values...this leads me to believe
that maybe the problem is in the page where I actually have the form
for inputting the names into the arrays....Any ideas?

At the same time i'm not sure why you said:
but down below you're echoing
$_SESSION[1]
should be
$_SESSION['student[1]']
at least the way you've set previous pages.


I was fairly sure that the way I was printing it was infact trying to
print:
$_SESSION['student[1]'], $_SESSION['student[2]'], etc...since i had:

$x = 'instructor[' . $k . ']';
echo $_SESSION[$x];

Because if I echo $x it prints: instructor[0], instructor[1], etc....

Thanks
--Lucas

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Avinash Korwarkar | last post by:
Hi Guys, I am using sessions to store the login name and then retrieve it on every page. Here are the details of the php settings. 1) I have PHP 4.2.2 installed on a Apache 1.3.27 server. 2)...
2
by: Phil Powell | last post by:
I have a form that will be preserving form data prior to processing the form data. Upon clicking a certain submit button you will go to another PHP script that will contain the following code: ...
0
by: Mountain Man | last post by:
Hi, I'm having trouble with sessions. Text fields from a form aren't getting updated when the form is resubmitted. On the other hand, my radio buttons are updated every time. Many thanks to...
6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
3
by: John | last post by:
Hi all, I've got an issue with session variables in asp pages. I need to make a decision that I think you can hopefully help me out here. When a session starts on the iss server I need to...
0
by: David Staschover | last post by:
I'm trying to set up sessions in perl. The session is initialized fine in session1.cgi In session2.cgi, the correct session id is returned from the cookie, but when I initialize the session, a...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.