I'm having a page home.php which has a button 'UPDATE'.
1. onclick function of 'UPDATE' should call a javascript function 'write()'.
2. 'write()' function get's the username and a value - both from the session (php comes in use here) and write it to a database.
Here is the code : home.php
<html>
<head>
<title> Sample Page </title>
</head>
<body>
<script>
function write(){
<?php
include("dbConnect.php"); // connection works perfectly with other php files
// gets 'latestValue' from session variable
// gets 'username' from session variable
// updates 'patients' table with the latestValue, against the username
$query = "UPDATE patients SET lastLDNId = '$_SESSION[latestValue]' WHERE username='$_SESSION[username]'";
$result = mysqli_query($con, $query);
?>
}
</script>
<input type="button" onclick="write()" value="UPDATE">
</body>
</html>
When i run the code and checking by Inspecting element, I'm getting the following error in the function write(), as the browser interprets it
function writeLDN(lDNId){
<br />
<b>Notice</b>: Undefined index: latestValue in <b>C:\xampp\htdocs\MedPhil\home.php</b> on line <b>98</b><br />
}
When i remove the php part inside the javascript function, the above error is not generated, which means it's not possible to use php in the way I've used.
Can anyone give me a solution other than echoing the whole script content using php?
Thanks in advance