My email password script is showing error:
Notice: Undefined variable: _Post in
C:\xampp\htdocs\DreamWeaver\EMPWScript.php on line 3 Fail - Please try
again!
I define the variable, what is wrong?
<?php
#session_start();
$_SESSION['EMPW'] = $_Post['Email1'];
?>
You wrote:
$_SESSION['EMPW'] = $_Post['Email1'];
but PHP is case sensitive, so try this:
$_SESSION['EMPW'] = $_POST['Email1'];
(In other words, POST has to be uppercase).
You can never be sure if it is POST or which variable type it is, so it could be better to always check and cast to the expected type.
$empw = (string) ($_POST['email] ?? '');
//here would be nice to throw some exception if email empty
$_SESSION['EMPW'] = $_POST['Email1'];
Related
I am new in this site, and i found some questions that are connected to my system error but unfortunately they can't fix the error. I am creating an offline web-based information system for my capstone project and I don't understand why P_Bday is undefined.. Here is my code
This is my code for inputting Birthdate:
input type="text" id = "P_Bday" name = "P_Bday" class="form-control" data-inputmask="'alias': 'dd/mm/yyyy'" data-mask placeholder="dd/mm/yyyy" required
And here's my code for calculating age:
function ageCalculator($dob){
if(!empty($dob)){
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
return $age;
}
else{
return 0;
}
}
$dob = $_POST["P_Bday"];
And I call my function here, where it should display the calculated age depending on the inputted birthdate:
input type='text' name = 'P_Age' id='disabledTextInput' class='form-control' value='".ageCalculator($dob)."' readonly
Every time I ran my code it says:
Notice: Undefined index: P_Bday in
C:\xampp\htdocs\PISGDH\recordclerk\RecordEntry\addPatient.php on line
47
If the line $dob = $_POST["P_Bday"]; is being run on the page before anything is sent via POST, then $_POST[foo] is invalid.
Change the line to:
if(isset($_POST["P_Bday"])) $dob = $_POST["P_Bday"];
else $dob = null;
Or:
$dob = isset($_POST["P_Bday"]) ? $_POST["P_Bday"] : null;
An Undefined index error is pretty simple to debug. You start at the file mentioned in the error message C:\xampp\htdocs\PISGDH\recordclerk\RecordEntry\addPatient.php and go to the line mentioned in the error message line 47 and find the undefined index in question on that line P_Bday and know with absolute certainty that up to this point in your code you have not defined that index for that variable. You can work your way backwards through the code to try and figure out your mistake. The mistake can be a typo (you used the wrong case/variable name) or it can be that you just forgot to initialize the variable properly.
The best way to avoid undefined variable/index errors is to initialize always and initialize early. In the few cases where you cannot be sure that variables are properly initialized (for example with $_POST/$_GET or other external variables under control of client input) you want to use isset to avoid the error and that way you can coalesce null values or write logic that prevents the code from continuing with an uninitialized value in case of user error.
Example
if (!isset($_POST['P_Bday'])) {
die("You forgot to fill out your birthday!");
} else {
echo "Yay!";
}
Some good initialization techniques with $_POST/$_GET
A good best practice for "initialize always and initialize early" when dealing with user input is to setup a default set of values for the expected input from your form and initialize from that in order not to fall into this trap.
Example
$defaultValues = [
'P_Bday' => null,
'Option1' => 'default',
'Option2' => 1,
];
/* Let's say the user only supplied Option1 */
$_POST = ['Option1' => 'foo'];
/* This makes sure we still have the other index initialized */
$inputValues = array_intersect_key($_POST, $defaultValues) + $defaultValues;
/**
* Now you can pass around $inputValues safely knowing all expected values
* are always going to be initialized without having to do isset() everywhere
*/
doSomething(Array $inputValues) {
if (!$inputValues['P_Bday']) { // notice no isset() check is necessary
throw new Exception("You didn't give a birthday!!!");
}
return (new DateTime)->diff(new DateTime($inputValues['P_Bday']))->y;
}
You are declaring the variable $dob after calling function. You have to declare your variable before function call and also use conditional statement like following:
Please write your code as follows:
if(isset($_POST["P_Bday"])){
$dob = $_POST["P_Bday"];
} else {
$dob ="";
}
function ageCalculator($dob){
if(!empty($dob)){
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
return $age;
}
else{
return 0;
}
}
I have some code that runs fine until it hits this:
if(array_key_exists($snIdx, $fields));
$surnameField = trim($fields[$snIdx]);
or other version I tried:
if(isset($fields[$snIdx));
$surnameField = trim($fields[$snIdx]);
The $snIdx = -1.
It gives me Undefined offset error at second line ($surname = trim...).
I think I don't need to paste rest of code as the exception says there is sth wrong with those functions. My PHP version is 5.4.16.
Remove the semicolon from the end of the if line. Otherwise it's equivalent to:
if( something) {
// no-op
}
$surnameField = trim($fields[$snIdx]); // undefined offset error.
I'm having trouble sending data via sessions because I'm getting errors of undefined variable while defining the variable in controller/checkout/shipping_address.php under validate() function. (checkout/shipping_address/validate).
$this->session->data['ship_date'] = $this->request->post['ship_date']; //<- line 102
In controller/checkout/shipping_method
$ship_date = $this->session->data['ship_date'];
if(empty($ship_date)) echo "var empty";
$ship_date = explode("-", $ship_date);
$ship_date = $ship_date[0] . "/" . $ship_date[1] . "/" . $ship_date[2];
and then I do
$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address, $ship_date);
Also yes, in model/shipping/fedex.php I allow usage of $ship_date parameter. Yet after that I get.
Invalid JSON: Notice: Undefined index: ship_date in
/var/www/catalog/controller/checkout/shipping_address.php on
line 102[] parsererror Notice: Undefined index:
ship_date in
/var/www/catalog/controller/checkout/shipping_address.php on
line 102[]
You should debug the arrays $this->session->data and $this->request->post.
The reason you see those errors is that there is no index ship_date in $this->session->data and in $this->request->post. So you get a Notice: Undefined index:.
Because of the notices that are printed your afterwards outputted json becomes invalid.
Actually, OpenCart only talks through JSON. So adding this will help.
$JSONarray = array("date" => $this->request->post['ship_date']);
$this->session->data['ship_date'] = json_encode($JSONarray);
When you want to use it,
$JSONarray = $this->session->data['ship_date'];
$arr = json_decode($JSONarray, TRUE);
$Value = $arr['ship_date'];
We have to make the data JSON and then send it
I am trying to implement the creating topic part in forum
Why I can't use the another php file, say b.php to get data that sent from a.php?
$topic=$_POST['title'];
$detail=$_POST['content'];
$name=$_POST['username'];
Errors show message that undefined index at these 3 inputs.
Because you are calling this script without sending POST data.
Use it in following way:
$topic = empty($_POST['title']) ? null : $_POST['title'];
$detail = empty($_POST['detail']) ? null : $_POST['detail'];
$name = empty($_POST['name']) ? null : $_POST['name'];
It will avoid errors and if you just request script without POSTing, variables will contain null values
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
Notice: Undefined index: subject in /var/www/mailer.php on line 12 Notice: Undefined index: message in /var/www/mailer.php on line 13 Notice: Undefined index: from in /var/www/mailer.php on line 14 Notice: Undefined index: verif_box in /var/www/mailer.php on line 15 Notice: Undefined index: tntcon in /var/www/mailer.php on line 23 no variables received, this page cannot be accessed directly
BELOW IS THE CODE
<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
// -----------------------------------------
// The Web Help .com
// -----------------------------------------
// remember to replace you#email.com with your own email address lower in this code.
// load the variables form address bar
$subject = $_POST["subject"];
$message = $_POST["message"];
$from = $_POST["from"];
$verif_box = $_POST["verif_box"];
// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("abhijit.infogence#gmail.com", 'TheWebHelp.com Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else if(isset($message) and $message!=""){
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
} else {
echo "no variables received, this page cannot be accessed directly";
exit;
}
?>
You're trying to access parts of a variable which don't exist. You may want to check before using them, if they exist.
The error is simply because the message $_POST array does not have a key called 'message'. It probably comes from the fact that the form has not been submitted. The error is only a notice and won't stop the program from working.
You should check what $_POST contains before you address some particular fields, take a look at the isset function. Or simply turn off display_errors ;)
It seems that you call this PHP file without submitting a form via the POST method. Make sure that your mailing form has the proper method set:
<form method="POST" action="yourfile.php">
etc.
</form>
You should also sanitize the user input before calling mail() (i. e. remove newlines and tags), otherwise you are calling for trouble.
Your $_POST and $_COOKIE arrays do not contain those indexes.
Do:
print_r($_POST);
print_r($_COOKIE);
to see what is contained in those arrays
foreach(array('subject', 'message', 'from', 'verif_box') as $val)
{
if (isset($_POST[$val]))
{
$$val = trim($_POST[$val]);
continue;
}
// some sort of error checking, like telling the end user that
// not all fields were correctly given
}
Check user-submitted data
$subject = (isset($_POST["subject"]) ? $_POST["subject"] : '');
$message = (isset($_POST["message"]) ? $_POST["message"] : '');
$from = (isset($_POST["from"]) ? $_POST["from"] : '');
$verif_box = (isset($_POST["verif_box"]) ? $_POST["verif_box"] : '');
You can even make your own function to do that
function checkPost($fieldname)
{
return (isset($_POST[$fieldname]) ? $_POST[$fieldname] : '');
}
And then do
$subject = checkPost("subject");
I recommend as well that you check required POST fields
if (!isset($_POST["xxx"]) || trim($_POST["xxx"]) == '')
{
// throw exception, display error...
}
etc.
FYI, instead of using stripslashes() to avoid "magic_quotes", you can use a simple function such as this one http://snippets.dzone.com/posts/show/5256 which will do the job for all fields.
//checking if array elements are set and changing variables values if so
$subject = isset($_POST["subject"])?$_POST["subject"]:null;
$message = isset($_POST["message"])?$_POST["message"]:null;
$from = isset($_POST["from"])?$_POST["from"]:null;
$verif_box = isset($_POST["verif_box"])?$_POST["verif_box"]:null;
// check to see if verificaton code was correct and if cookie value on 'tntcon' is set
if(isset($_COOKIE['tntcon']) && md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
Changes are on lines 12-15 and in 23.