Also do not forget to validate the post values before processing them in a database, this code also assumes that you have a form field with the name courseid
A few tips, as droopsnoot said, if you use $_POST[‘$varname’] then because you have single quotes around the variable, you are actually saying “please return me the value of the $_POST array where the key is the string $varname” where as if you remove the single quotes, you are saying “please return me the value of the $_POST array where the key is the string courseid”.
While we are on the topic, if you had used $_POST[“$varname”] (note the double quote marks), then it would have worked the same as $_POST[$varname]. This is because php checks strings defined with double quotes for any variable values. Personally I try to avoid double quotes, because they are too easy to accidentally create a security hole.
Also, I thought I should mention that your logic might not quite work how you thought. !isset($_POST[$varname]) says “if the variable isn’t set” and isset($_POST[$varname]) says “if the variable is set” because the ! character says “reverse the logical outcome (turn false to true, and true to false)”. So:
$varname = 'courseid';
if (isset($_POST[$varname])) {
// $_POST[$varname] is set as something, do something with it
} else {
// $_POST[$varname] is [B]not[/B] set as something, error?
}
Thanks for all of the replies. I removed the single quotes and it worked fine.
What I am doing is setting up an array populated with expected inputs from a form including validation information so that the
processing code is generic and does not need to be repeated for each form.
It is going well so far but so much to lean about php!!