2

I want to update cookie value after checking the specific condition in javascript function. After checking it I am calling one php function(cookie updatingg) from javascript function. Everytime when I refresh the page my cookie value get updated with +1 in short it's not satisfying condition.

My code is:

JavaScript Function:

<script type="text/javascript">
function on_call_php(){
    var result="<?php php_func();?>";
    alert(result);
    return false;
}

Php Function Code:

<?php
    function php_func(){
      setcookie("correct", $_COOKIE["correct"]+1, time() + (86400 * 30), "/");
      echo "hello";
    }
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    You can't call a PHP function from JavaScript without using Ajax. This is because PHP is a server side language, once the page is loaded the PHP is done, it doesn't "do" anything else. JavaScript is a client side language, it can't interact directly with PHP, but you can use Ajax to call a separate PHP script. You could, in this case, simply [use JavaScript to set the cookies](https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie). – GrumpyCrouton Nov 07 '19 at 20:03

0 Answers0