Malcolmhire2001 Posted January 12, 2007 Share Posted January 12, 2007 hello i have created a script that will allow a list box to search through an XML file so that it brings up the type of study a student is on.and i am geting this problem: Notice: Undefined index: btnList in C:\Server\Apache2.2\htdocs\Work\cg0119wk10t2.php on line 13I can workout why this doen't work if anyone could helpm please.The code i am using:[code]<!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" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CG0119 Week 10, Task 2</title></head><body><h1>Student Names by Study Type (using XPath)</h1><?php if (is_null($_POST['btnList'])) {?> <form action="<?php echo $PHP_SELF; ?>" method="POST"> List by: <select name = "studyType"> <option value = "U">Undergraduate</option> <option value = "P">Postgraduate</option> <option value = "all">All</option> </select> <input type="submit" value="List" name="btnList" /> </form><?php } else { // ************************ // *** PROCESS THE FORM *** // ************************ $doc = new DOMDocument; $doc->preserveWhiteSpace = false; $doc->Load('student.xml'); $xpath = new DOMXPath($doc); // Create the query string based on which option the user selected // (start at the root element and include a condition if required). if ($_POST['studyType'] == "P") { // *** Display postgraduate students *** echo "<h2>Postgraduate Students</h2>\n"; $query = '//students/student/studyTypeID[. = "P"]'; } elseif ($_POST['studyType'] == "U") { // *** Display undergraduate students *** echo "<h2>Undergraduate Students</h2>\n"; $query = '//students/student/studyTypeID[. = "U"]'; } else { // *** Display all students echo "<h2>All Students</h2>\n"; $query = '//students/student/studyTypeID'; } // Execute the query and return a DOMNodeList containing all // of the nodes that match the XPath expression. $entries = $xpath->query($query); echo "<ul>\n"; // Display the required values for each entry returned by the XPath query. foreach ($entries as $entry) { echo "<li>{$entry->previousSibling->previousSibling->nodeValue} {$entry->previousSibling->nodeValue}</li>\n"; } echo "</ul>\n"; } // close the process the form if statement?></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/ Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 Is it occurring the first time the page is loaded, or after the form has been submitted?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-158991 Share on other sites More sharing options...
Orio Posted January 12, 2007 Share Posted January 12, 2007 Replace "is_null($_POST['btnList'])" with "!isset($_POST['btnList'])"Orio. Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-158992 Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 depending on your error level setting (error_reporting) in PHP, you may or may not get notices. They're not errors as such, but always still good practice to deal with them. In your case, you have assumed that $_POST['btnList'] exists and then just checked its value. try this instead:[code] if (!isset($_POST['btnList'] || is_null($_POST['btnList']))[/code]which will carry out the action if it's null, as before, but will ALSO carry out the action if it isnt set at all. being NULL means that $_POST['btnList'] has been given no value, which isn't the same as the btnList key being set in the first place.hope that helpscheers Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-158994 Share on other sites More sharing options...
Malcolmhire2001 Posted January 12, 2007 Author Share Posted January 12, 2007 Orio your solution was great the error has gone now, but when i am selecting from the list box then clicking list it is displaying this error[b]Forbidden[/b]You don't have permission to access /<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>C:/Server/Apache2.2/htdocs/testXpath.php</b> on line <b>18</b><br /> on this server.Any help! Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-158997 Share on other sites More sharing options...
Malcolmhire2001 Posted January 12, 2007 Author Share Posted January 12, 2007 redbullmarky solution brings up this errorParse error: parse error, unexpected T_BOOLEAN_OR, expecting ',' or ')' in C:\Server\Apache2.2\htdocs\testXpath.php on line 13any help! Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-159000 Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 oops sorry. missed a bracket :([code] if (!isset($_POST['btnList']) || is_null($_POST['btnList']))[/code]as for the other error, try replacing the $PHP_SELF line with:[code] <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">[/code][b]edit[/b] in fact, if you want the form to post to the CURRENT page, then you dont need to specify a value for action. just[code] <form action="" method="POST">[/code]would do the same. Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-159001 Share on other sites More sharing options...
Malcolmhire2001 Posted January 12, 2007 Author Share Posted January 12, 2007 [quote author=redbullmarky link=topic=122081.msg503011#msg503011 date=1168602820]oops sorry. missed a bracket :([code] if (!isset($_POST['btnList']) || is_null($_POST['btnList']))[/code]as for the other error, try replacing the $PHP_SELF line with:[code] <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">[/code][b]edit[/b] in fact, if you want the form to post to the CURRENT page, then you dont need to specify a value for action. just[code] <form action="" method="POST">[/code]would do the same.[/quote]Thank You very much my problem is now solved i used [code]<form action="" method="POST">[/code]and[code]if (!isset($_POST['btnList']) || is_null($_POST['btnList']))[/code]everything is working how it shouldCheers, Mal Quote Link to comment https://forums.phpfreaks.com/topic/33879-undefined-index-problem/#findComment-159007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.