0
let varmyname = '<?php echo isset($_SESSION['myname']);?>';

if (varmyname == "") {
     console.log("session not set "); 
     document.getElementById("inputmyname").value="";

} else {
    console.log(" session set")
    document.getElementById("input-myname").value = '<?php echo $_SESSION['myname']; ?>';
}

i am getting notice for this line= document.getElementById("input-myname").value = ''; can we ignore this notice? if not then how to solve this issue.

  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Jeto Oct 21 '20 at 19:00
  • 1
    did you start the session? `session_start`, here is link: https://www.php.net/manual/en/function.session-start.php – Marcello Perri Oct 21 '20 at 19:06
  • `can we ignore this notice` - sure, you can ignore whatever you like. Do notices exist just for php to have something to do? No. Do notices exist to point out non-fatal errors in your code which could further down the line become fatal? Yup. – imposterSyndrome Oct 21 '20 at 20:51

1 Answers1

0

isset returns true or false, and you are assigning this to your variable, so your variable is always set. Try console logging varmyname you should see this.

Therefore your if statement always falls to the else block, as if (varmyname == "") is never true, but you are trying to access it on the line you have an error for.

You need to change the condition to check for a value. You could try the null coalesce operator, e.g

let varmyname = '<?php echo ($_SESSION['myname']?? '') ;?>';

imposterSyndrome
  • 896
  • 1
  • 7
  • 18