-2

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.

Sagar Pawar
  • 465
  • 4
  • 26

1 Answers1

1

PHP executes before the javascript, so you cannot use in this way, You can call the file using AJAX

Try something like this:

<script>
    function enable(){
        $.ajax({
            type: "POST",
            url: "setSession.php",
            success: function(res) {
                if(res==="Success"){
                    alert("Success!!!");
                }
            }
        });      
    }
</script>

setSession.php

<?php
$_SESSION['demo']='enable';
if($_SESSION['btn_demo']=='enabled'){
    $_SESSION['btn_test']='disable';
    unset($_SESSION['demo']);
}
else {
    $_SESSION['btn_test']='enable';
}
echo "Success";
?>
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • whether Ajax will redirect the page? if so, then I can't coz I don't want to redirect the page. only pass the value when button is clicked – Sagar Pawar Oct 30 '15 at 06:53
  • 1
    AJAX will not redirect the page, but it does exactly what you want – Thamilhan Oct 30 '15 at 06:54
  • It executes your code behind and sends the result to your current page without refreshing your entire page – Thamilhan Oct 30 '15 at 06:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93777/discussion-between-sagar-pawar-and-vijaya-sankar-n). – Sagar Pawar Oct 30 '15 at 08:40