"php $_get and undefined index" Answer’s

0

Error reporting will have not included notices on the previous server which is why you haven't seen the errors.

You should be checking whether the index s actually exists in the $_GET array before attempting to use it.

Something like this would be suffice:

if (isset($_GET['s'])) {
    if ($_GET['s'] == 'jwshxnsyllabus')
        echo "<body onload="loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml',         '../bibliographies/jwshxnbibliography_')">";
    else if ($_GET['s'] == 'aquinas')
        echo "<body onload="loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')">"; 
    else if ($_GET['s'] == 'POP2')
        echo "<body onload="loadSyllabi('POP2')">";
} else {
    echo "<body>";
}

It may be beneficial (if you plan on adding more cases) to use a switch statement to make your code more readable.

switch ((isset($_GET['s']) ? $_GET['s'] : '')) {
    case 'jwshxnsyllabus':
        echo "<body onload="loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml',         '../bibliographies/jwshxnbibliography_')">";
        break;
    case 'aquinas':
        echo "<body onload="loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')">";
        break;
    case 'POP2':
        echo "<body onload="loadSyllabi('POP2')">";
        break;
    default:
        echo "<body>";
        break;
}

EDIT: BTW, the first set of code I wrote mimics what yours is meant to do in it's entirety. Is the expected outcome of an unexpected value in ?s= meant to output no <body> tag or was this an oversight? Note that the switch will fix this by always defaulting to <body>.

Wednesday, March 31, 2021
 
phirschybar
answered 10 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :