I am using a button from my HTML script to unset cookies, but currently I cannot make that call to the PHP script through that button click.
HTML script
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WCSST 2</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
</style>
</head>
<body>
<button style="color: black" <?php if(isset($_COOKIE["name"])) { ?> Disabled <?php } ?> value='Set Cookie'><b>Unset cookie</b></button>
<!-- END PAGE SOURCE -->
</body>
</html>
PHP script
<?php
unset($_COOKIE['name']);
unset($_COOKIE['age']);
header("Location: CM.php");
exit(0);
}
?>
How can I make that call to the PHP script through that button click?
your html script must have .php
extension becuase it have php script.
for example example.php
-- > contain your html script.
you called to unset file using include statement
<body>
<button style="color: black" <?php
if(isset($_COOKIE["name"]))
{
include 'unset.php';
}
?> value='Set Cookie'>
<b>Unset cookie</b></button>
<!-- END PAGE SOURCE -->
</body>
Or you can use JAVASCRIPT
onclick function
What you need to do is make the button call a javascript function, the function will call an Ajax which will execute your PHP code.
your HTML file:
<button onclick='my_func();'>Click me</button>
Your javascript (include jQuery to make life easier)
function my_func()
{
$.ajax({
type: 'post',
url: 'unset.php',
data: {},
dataType: 'json',
success : function (data) {},
error: function() {}
});
}
You should use a form to call a PHP
script. You can do that like this:
<form action="unset.php" method="post">
<input type="submit" value="Unset cookie">
</form>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WCSST 2</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
</style>
</head>
<body>
<?php if(isset($_COOKIE["name"])) { ?>
<button style="color: black" onclick="window.location.href='unset.php'"><b>Unset cookie</b></button>
<?php }else{ ?>
<button style="color: black" onclick="window.location.href='set.php'"><b>Set cookie</b></button>
<?php } ?>
</body>
</html>
unset.php script
<?php
unset($_COOKIE['name']);
unset($_COOKIE['age']);
header("Location: CM.php");
exit(0);
}
?>
set.php script
<?php
// set your cookie
?>
Yes, first of all you cant write php script code into .html file, so for that create .php file and in that write above your whole code. and one more thing is for unset cookies in you have to send post request using normally form method or you can also use ajax call for eg
<form name="abc" id="abc" method="post"> <input type="submit" name="unset" value="Unset Cookies"> </form> <?php if($_POST) { unset($_COOKIE['name']); unset($_COOKIE['age']); header("Location: CM.php"); exit(0); } ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.