I am new in web page development, and it seems that this is a basic question but I can't figure out how to solve.
Through a button I need to change the value of a variable in php, specifically every time a button is pushed the value of a variable must increase.
The code is the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
function echoVal()
{
alert("<?php val(); ?>");
}
</script>
<?php
function val() {
static $a = 0;
$a++;
echo $a;
}
?>
<button onclick="echoVal()"> Increase </button>
</body>
</html>
The main problem is that the value of the variable $a is always 1, no increasing its value when the button is pushed. I have saved the file with extension .php (I am not sure if that will make any difference, but I just mention it).
Any suggestion to solve this issue?
Thanks in advance for any suggestion.
Edited Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
function echoVal()
{
alert("<?php val(); ?>");
}
</script>
<?php
function val() {
static $a = 0;
$a++;
$filename ='infoX.txt';
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file";
} else {
if (fwrite($handle, $a) == FALSE) {
echo "Cannot write to file";
} else {
echo $a;
}
fclose($handle);
}
} else {
echo "The file is not writable";
}
}
?>
<button onclick="echoVal()"> Increase </button>
</body>
</html>
$ato 0 within yourval()method which seems to be why your variable is always resulting to1.