Forum Moderators: coopster

Message Too Old, No Replies

passing variables between pages

         

swati

10:42 am on Oct 1, 2004 (gmt 0)

10+ Year Member



Hello,

Suppose i have 2 drop down lists named Site and Type respectively and they are inside a form with method="post", my code is as follows:
(depending on the option that the user selects from the list, records are fetched from the database and displayed on the same page as the lists.)
$site = $_POST['site'];
$type = $_POST['type'];
When i run this page i m given a message:
Notice: Undefined index: site
Notice: Undefined index: type
this error occurs in the 2 lines of code that i have given above.
These links are in a page called page2.php
I have a link in page1.php which takes me to page2.php
When i click on the link in page1.php n go to page2.php , that is when i get these "Notice..."

Also i have a series of pages like page1, page2 n so on. On each of these I have BACK buttons. But when i click on BACK i have lost the information that was on the previous page and i have to select the options in the drop down list again to view the information.
Please help!

henry0

12:30 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using sessions
for ex:(top of page)
<?php
session_start();
$username=$_POST['username'];

$_SESSION['username'] = $username;

rest of code....
?>

then on each page where you want to pass the same value (top of page again):

session_start();

also PHP Manual [us4.php.net]

regards

henry

mincklerstraat

1:38 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$site = $_POST['site'];
$type = $_POST['type'];
When i run this page i m given a message:
Notice: Undefined index: site
Notice: Undefined index: type

if(isset($_POST['site'])) $site = $_POST['site'];
else $site = '';
or, if you're not entirely sure of what you're doing here, for security reasons make it:
if(isset($_POST['site'])) $site = htmlspecialchars($_POST['site']);
else $site = '';
do the same with type.
Why? These'd be elements of the '$_POST' array, and before you use them in any way, they have to be set, or you get this kind of error - it's another sort of 'security check' thing, annoying it helps you in the long term.

Later, you'll figure out how to make an array of all the input you want, and use a foreach(){} to just set all of these correctly at the top of each form page. Makes life a lot easier.

For your back button: find a good HTML reference and look up <select> and <input type="radio"> or whatever other kind of form stuff you're using in their forms section. You'll find something called "selected" for select which pre-determines which value is selected for dropdowns, and "checked" for the other input types. Kinda like "value= " for text input in forms. Figure out how to make your php output these as appropriate.

Don't fret, everybody gets frustrated with forms the first few times around. ;)

swati

8:56 am on Oct 5, 2004 (gmt 0)

10+ Year Member



Hello, here is my code. I am still getting those "undefined index" messages.
<?php
session_start();
$site=$_POST['site'];
$_SESSION['site'] = $site;
if(isset($_POST['site']))
$site = htmlspecialchars($_POST['site']);
else $site = '';
$type=$_POST['type'];
$_SESSION['type'] = $type;
if(isset($_POST['type']))
$type = htmlspecialchars($_POST['type']);
else $type = '';
?>

This code is on page2.php and i click on a hyperlink in page1.php to arrive to page2.php
Help!

swati

9:04 am on Oct 5, 2004 (gmt 0)

10+ Year Member



oops i think there were some blunders in the previous code so I have amended it as follows:
<?php
session_start();
if(isset($_POST['site']))
$site = htmlspecialchars($_POST['site']);
else $site = '';
$_SESSION['site'] = $site;
if(isset($_POST['type']))
$type = htmlspecialchars($_POST['type']);
else $type = '';
$_SESSION['type'] = $type;
?>

However I still get those messages and in fact I am getting an "undefined index" message for all the php variables that I am using....
why is it so...
also i dont understand in which page do i have to put "session_start();" apart from page2.php (in which the above shown code is)

Regards
Swati

mincklerstraat

11:05 am on Oct 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're getting these undefined index errors whenever you want to use an element of an array, before this is properly set.

I.e., if you write:
if($diddly['boo'] == 5) { whatever }
and you haven't set $diddly['boo'] - that is, haven't done soemthing like :
$diddly['boo'] = 0: or $diddly['boo'] = ''; or setting it to whatever value you want -
then you will get this error since php wants to check the value of $diddly['boo'], but finds that this has no value. What you need to do is either set the value before you use it or check it in any way (the best practice), or if you're not sure if it's set, use one of these two language constructs (like functions, but technically not functions):

- isset()
or
- empty()

isset($diddly['boo']) checks to see if this is set, and if it is returns 1, and if it isn't, returns 0. Furthermore, it doesn't go complaining and whining if it isn't set.
empty($diddly['boo']) checks to see if the value is set, and that the value isn't either a 0 or a null. Look it up in the php manual to be sure about it. It also doesn't complain about non-set variables (or in this case, array elements, which I guess in a way are varaibles to themselves).

Note also that you have the same problems when you don't set ordinary variables, and the above two language constructs also work with variables in similar situations. You just get a different error message.

Also, you can avoid all of these warnings if you just turn off error reporting for this level of warning:

error_reporting(E_ALL & ~E_NOTICE);

However, you don't really want to do this since these errors are important, and help you code better. See the php manual on error reporting for more advanced info; this is also important, since it has big security consequences.

Bonusbana

11:39 am on Oct 5, 2004 (gmt 0)

10+ Year Member



Try this:

<?php
session_start();
$_SESSION['site']=!empty($_POST['site'])? htmlspecialchars($_POST['site']) : null;
$_SESSION['type']=!empty($_POST['type'])? htmlspecialchars($_POST['type']) : null;
?>

And if you want to use the variables $type and $site later in the code just add a:

$site = $_SESSION['site'];
$type = $_SESSION['type'];

(this would probably not be neccessary if globals are turned on)

swati

12:04 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



I have tried the following two things
1.
<?php
session_start();
if(isset($_POST['site']))
$site = htmlspecialchars($_POST['site']);
else{
$site = '';
echo("NOT SET!");
}
$_SESSION['site'] = $site;
if(isset($_POST['type']))
$type = htmlspecialchars($_POST['type']);
else{
$type = '';
echo("NOT SET!");
}
$_SESSION['type'] = $type;
?>

2.
<?php session_start();
$_SESSION['site']=!empty($_POST['site'])? htmlspecialchars($_POST['site']) : null;
$_SESSION['type']=!empty($_POST['type'])? htmlspecialchars($_POST['type']) : null;
$site = $_SESSION['site'];
$type = $_SESSION['type'];
?>

It still doesnt help......i understand why the error is coming but i think these codes should have worked...what am I missing?

Bonusbana

12:25 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



Could you please post all of the code that you have in both page1.php nad page2.php

Is the error appearing on page2 or page1? Where is the form and did you make sure there are names for the select options?

swati

12:44 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



Well you see the code for page2.php is far too large....

PAGE 1 :-
This page just contains a link as follows
"VIEW THE SITE,TYPE INFO"

the user just clicks on it n is taken to page2.php(no variables are passed along with this link)

PAGE 2 :-

Heres how the code for select looks like..
<select name="site[]" onChange="MM_jumpMenu('parent',this,0)" >
<option selected />ALL
<option />FMDC
<option />PMDC
<option />ERDC
</select>

and..
<select name="type[]" onChange="MM_jumpMenu('parent',this,0)" >
<option>t1</option>
<option>t2</option>
<option>t3</option>
</select>

and the code for session etc on the top of the page looks like this...
<?php
session_start();
$_SESSION['site']=!empty($_POST['site'])? htmlspecialchars($_POST['site']) : null;
$_SESSION['type']=!empty($_POST['type'])? htmlspecialchars($_POST['type']) : null;
$site = $_SESSION['site'];
$type = $_SESSION['type'];
?>

I am lost....

Bonusbana

12:51 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



Try removing the bracelets in the form name tag.

site[] -> site
type[] -> type

swati

1:35 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



nope same problem........

But in fact i am using site and type like this
for($a=0;$a<$num;$a++){
echo("user selected $site[$a]");
}

but anyways it also shows error in other simple variables...

I am sorry to be bugging you so much.

Regards

Swati

Bonusbana

1:54 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



There is obviously some array error going on here. Im sorry I cannot help you, since each post makes me even more confused.

Try to locate the core of how you are using the type[] and site[] and test small parts of the code.

mikejson

8:24 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



from your first post it just sounds like the the index value in the line :
$_POST['site']

is what php is complaining about

site looks to it to be a string I guess(not completely familiar with php yet hehe) and any array index should be an int.

That's what the error is but how to fix it, I can't tell you, I'm not familiar with that syntax.

From what I knew, to get values from POST and GET(unsecured anyway) you just use the "name" of the form element with a $ infront of it.

<form ....>
<input ... name="example" value="2">
...

in PHP, the variable $example would == 2.... that's what I know(php noob I am :) ).

willybfriendly

8:39 pm on Oct 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<select name="site[]" onChange="MM_jumpMenu('parent',this,0)" >


The above should read <select name="site".... (no brackets)

The variable should be accessible as $_POST[site]

WBF

swati

1:30 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



I ve got another question:
I have an ADD,DELETE and MODIFY button . If ADD has been clicked then i want a certain action to be performed,likewise with every button. How do i do that?

Bonusbana

1:47 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



Give each form button a name. This name will be returned as $_POST['name'] and its either true or false.

swati

8:36 am on Oct 7, 2004 (gmt 0)

10+ Year Member



One more:
I want a hyperlink "clear all fields". When the user clicks this link i want the same page to be loaded again but with all text boxes empty so the user can enter something in the fields.

Regards
Swati

mincklerstraat

8:40 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably most people would use js for this, which I'm not particularly fluent in; I just use echo '<a href="'.$_SERVER['PHP_SELF'].'">clear fields</a>' which should do the trick for you if this is a fairly normal post form.

swati

9:16 am on Oct 7, 2004 (gmt 0)

10+ Year Member



Thanks. What I would like to know is how do I make php understand that when this link is clicked then all fields are to be cleared?

Regards
Swati

mincklerstraat

3:33 pm on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP doesn't really have to 'understand' it. If you use the above, following the link will lead to the same page, but with no $_GET or $_POST variables set, so if it's these that are filled in the value fields, or determine which are 'selected', everything will return to default.

The short of it being: just try what I put in the post above, and see if it works, it probably will.

If you are using sessions, this is a different matter - you should then have the link set a $_GET variable, and in your script have all the session variables populating your form set back to default.