Message from JavaScript discussions

October 2020

— Can you share the code in plain text? using pastebin or hastebin

— 

<?php
function change(){
header("location: register.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
height: 100px;
width: 100px;
background-color: green;
}
</style>
</head>

<body>
<div class="box" onclick="my()">
<button>Click me</button>
</div>

<script>
function my() {
var trigger = <?php change(); ?>
}
</script>
</body>
</html>

— You cannot call a php function from javascript.

— Php is a server. It serves data via HTTP.

— Here you use javascript on the browser (client-side) to interact with the user

— Then what is the way

— HTTP APIs.
E.g. you have a file change.php that does what you need, with javascript you can dynamically retrieve the content and display it.

e.g.:

const change = async () => {
const response = await fetch('/change.php')
const text = await response.text()
document.querySelector('#content').innerHTML = text
}

Message permanent page

— Will I get my desired result

— I think so

— But..if you want to redirect to another location, you don't need php

— You can simply do location = 'register'

— Or you just use native HTML and do:
<a href="register.php">click this to register</a>