Jump to content

Recommended Posts

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 13

I 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]
Link to comment
https://forums.phpfreaks.com/topic/33879-undefined-index-problem/
Share on other sites

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 helps
cheers
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!
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 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 should

Cheers, Mal

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.