Am trying to execute php code within javascript with button onclick event. I have two pages test.php and demo.php with button in each page. Now what I want to do is. when I click button in test.php, it should disable the button in demo.php and when test.php is reloaded,it should check the status of button in demo.php and according to that it should display it.
Now I have tried something which works fine as per the requirement. problem is that button on both pages doesn't work. code executes only when the page is refreshed.
Here is the code that I have tried.
first test.php
<?php
session_start();
?>
<html>
<head>
<script type="text/javascript">
function enable(){
var a='<?php
$_SESSION['demo']='enable';
if($_SESSION['btn_demo']=='enabled'){
$_SESSION['btn_test']='disable';
unset($_SESSION['demo']);
}
else {
$_SESSION['btn_test']='enable';
}
?>';
}
</script>
</head>
<body>
<input type="button" value="<?php echo $_SESSION['btn_test']; ?>" name="submit" id="submit" onclick="enable()" >
</body>
</html>
when I click on enable button in test.php , it should enable the button in demo.php.
second demo.php
<?php
session_start();
error_reporting(0);
?>
<html>
<head>
<script type="text/javascript">
function enable(){
<?php
$_SESSION['test']=$_SESSION['demo'];
if($_SESSION['test']=='enable'){
$_SESSION['btn_demo']='enabled';
} else
{
$_SESSION['btn_demo']='disabled';
}
?>}
</script>
</head>
<body>
<style>
</style>
<input type="button" value="<?php echo $_SESSION['btn_demo']; ?>" name="submit" id="submit" onclick="enable()" >
</body>
</html>
here the button should be enabled/disabled when button in test.php is clicked. this both pages work fine when I refresh. doesn't work when either of the button is clicked. how to solve this ? need help.