following is the code with start button and its function which is not working but when im trying to echo hello in place of button tag then the function is working well
<button onclick="startexam()" id="btn" class="btn btn-primary btn-sm" title="Start
exam">Start Exam</button>
<?php
function php_func(){
echo '<select class="form-select" aria-label="Default select example"><option >
asdf</option></select>';
}
?>
<script>
//function called when user click on start exam button
function startexam() {
var result = "<?php php_func(); ?>"
document.write(result);
}
</script>
First, you need to understand what the technical difference between PHP and JavaScript is:
JavaScript executes scripts directly in your browser, e. g. Firefox. So, when JavaScript is executed, the page is already loaded.
PHP executes scripts on the server before the page is even in the browser.
So, if you want JavaScript to communicate with the server (the PHP script), you have to make a request to the server. To do so, use AJAX.
From W3Schools:
You can find a complete and clear introduction on W3Schools.