I did not able to access the js variable in html file in the php file by Ajax.Why?
In index.html file
<html>
<head>
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>Hello World</h1>
<script>
var myvar = "Hey this is Naveen";
$.ajax({
type: 'POST',
url: 'example.php',
data: {
myvar: myvar
},
success: function (response) {
console.log("Success");
},
error: function (response) {
console.log("Error");
}
});
</script>
</body>
</html>
And In the example.php File:
<?php
$my_php_var = $_POST['myvar'];
echo $my_php_var;
?>
Output:
Warning: Undefined array key "myvar" in C:xampphtdocshosproExampleexample.php on line 2
New contributor
Naveen Nandha Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.