Viewed   91 times

A new problem has arisen for me as I tried to run my script on a different PHP Server.

ON my old server the following code appears to work fine - even when no s parameter is declared.

<?php
 if ($_GET['s'] == 'jwshxnsyllabus')
echo "<body onload="loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml',         '../bibliographies/jwshxnbibliography_')">";
if ($_GET['s'] == 'aquinas')
echo "<body onload="loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')">"; 
 if ($_GET['s'] == 'POP2')
echo "<body onload="loadSyllabi('POP2')">";
elseif ($_GET['s'] == null)
echo "<body>"
?>

But now, on a my local server on my local machine (XAMPP - Apache) I get the following error when no value for s is defined.

Notice: Undefined index: s in C:xampphtdocsteachingindex.php on line 43
Notice: Undefined index: s in C:xampphtdocsteachingindex.php on line 45
Notice: Undefined index: s in C:xampphtdocsteachingindex.php on line 47
Notice: Undefined index: s in C:xampphtdocsteachingindex.php on line 49

What I want to happen for the script to call certain javascript functions if a value is declared for s, but if nothing is declared i would like the page to load normally.

Can you help me?

 Answers

1

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>.

Tuesday, October 11, 2022
4

first: try to strict programming

error_reporting(E_ALL | E_STRICT);

also you must use isset for check is index for array available or not

if (isset($_POST['submitbtn']) && isset($_FILES['avatar'])) {
     // ...
}

also check php configuraion

file_uploads    "1"
upload_max_filesize     "2M"
post_max_size   "8M"
max_file_uploads    20

post max size must be larger than upload max file size.

also as guys said check form enctype

Saturday, November 12, 2022
2

You have an empty line somewhere. That's why explode() will return only an empty $key, but have nothing to assign to the $v. And that's when it prints that notice.

You can rewrite it a bit to ignore such cases:

foreach ($read as $lines) {
    $key = strtok($lines, "|");
    $v = strtok("|");
    if ($v) {
        $data[$key] = $v;
    }
}

This will also avoid an empty entry in your final $data array.

Sunday, October 2, 2022
 
connor
 
5

You're not telling the JS how to send your POST parameters. Change your JS to:

data: { 'testscore':testscore },

This is the equivalent of "testscore=" + testcore in key=value form. It tells JS and PHP that you want the variable testscore to be mapped to the key "testscore", which you'll then pick up with $_POST['testscore']

Edit: See http://api.jquery.com/jQuery.ajax/, "Sending Data to the Server"

Wednesday, August 31, 2022
 
relaxxx
 
5

undefined index means that somewhere in the $_POST array, there isn't an index (key) for the key username.

You should be setting your posted values into variables for a more clean solution, and it's a good habit to get into.

If I was having a similar error, I'd do something like this:

$username = $_POST['username']; // you should really do some more logic to see if it's set first
echo $username;

If username didn't turn up, that'd mean I was screwing up somewhere. You can also,

var_dump($_POST);

To see what you're posting. var_dump is really useful as far as debugging. Check it out: var_dump

Wednesday, November 2, 2022
 
spex
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :