eshel zela
eshel zela

Reputation: 1

$_POST[#] doesn't exist ! Why?

My php code shows a form where date should be inserted then the date should be assigne to a variable. The trouble is that the value assigned vanishes !! Here is the code:

<!doctype html>
<html lang=en>
<head>
<title>Show Date</title>
<meta charset=utf-8>
</head>
<body>
<?php
$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
            OR die ('Could not connect to MySQL: ' . mysqli_connect_error () ); 
mysqli_set_charset($dbcon, 'utf8');
    $myDate = trim($_POST['exam_date']);
?>
<form action="Day_hours.php" method="post" >
    <input id="exam_date" name="exam_date" type="datetime" />
    <input id="submit"type="submit" name="submit" value="Show Date"/>
</form>
</body>

This is the error message I get:

Notice: Undefined index: exam_date in C:\xampp\htdocs\test\Day_hours.php on line 12 Can anyone help me locate $_post['exam_date']? Thanks

Upvotes: 0

Views: 284

Answers (2)

Colohanin Nik
Colohanin Nik

Reputation: 418

You must take the value only if post request was sended (it exist after submit)

if(isset($_POST){
   $myDate = trim($_POST['exam_date']);
}

Upvotes: 1

Myz Moe
Myz Moe

Reputation: 36

The first request of the page will not have the post data set. Only the request from the form will have the post data.

check for isset before using the post data $myDate = isset( $_POST['exam_date'] ) ? trim($_POST['exam_date']) : "";

Upvotes: 2

Related Questions