For whatever reason my database updates as if the function ran, but it never echod anything. When I reload the page it automatically sets the name to "John", but I never clicked the button.
<?php
function a(){
$con = mysqli_connect("localhost", "example", "example", "example");
$sql = "UPDATE User SET name = 'John' WHERE name = '$username'";
mysqli_query($con, $sql);
//test to see if function fires:
echo "function executed";
}
?>
Here is my html / javascript code:
<script type="text/javascript">
function b(){
document.write("<?php a(); ?>");
//test if javascript function executes:
alert("function b() executed");
}
</script>
<button onclick="b()">Click me!</button>
I had to do the javascript because my entire page is a form (for the purpose of a single save button) and you can`t directly have a button execute a php function.
I am just really confused why it doesn't echo, but it does update my database when I reload the page, please help.