Forum Moderators: coopster
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!
$_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
$site = $_POST['site'];
$type = $_POST['type'];
When i run this page i m given a message:
Notice: Undefined index: site
Notice: Undefined index: type
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. ;)
This code is on page2.php and i click on a hyperlink in page1.php to arrive to page2.php
Help!
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
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.
<?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)
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?
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....
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 :) ).
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.