r/PHPhelp • u/Comfortable_Tip_1434 • Dec 04 '25
PHP script doesn't work but is syntactically correct.
This script does not update the "result" variable.
<!DOCTYPE html>
<html>
<head>
<title>
How to call PHP function
on the click of a Button !
</title>
</head>
<body style="text-align:center;">
<h1 style="color:green;">
Button test
</h1>
<h4>
How to call PHP function
on the click of a Button ? <br><br>
</h4>
<h4 id="result">placeholder</h1><br>
<script>
function button1() {
document.getElementById("result").innerHTML = "Button1 selected";
}
function button2() {
document.getElementById("result").innerHTML = "Button2 selected";
}
</script>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['button1'])) {
button1();
} elseif (isset($_POST['button2'])) {
button2();
}
}
?>
<form method="post">
<input type="submit" name="button1"
class="button" value="Button1" >
<input type="submit" name="button2"
class="button" value="Button2" >
</form>
</body>
</html>
0
Upvotes
22
u/Bubbly-Nectarine6662 Dec 04 '25
You’re mixing up the server side and the client side data manipulation. Either you choose client side and have the button onclick execute the JavaScript function buttonX or you use the form submit and have the server rewrite the whole page.
This makes no sense, as the server cannot trigger the client script like this.
16
u/sental90 Dec 04 '25
From the code on your post. The php functions button1() and button2() are undefined. There are javascript functions by those names but it doesn't work like that. Php is run before the page loads and knows nothing about the javascript.
9
u/Fair-Parking9236 Dec 04 '25 edited Dec 05 '25
There is nothing even remotely correct about this. You declared a function iside a script tag (javascript function) and call it just like that inside php tag? It doesnt work like that. Either go full php and declare and call function inside php file or tag, after the check and ( if you really want to include javascript), get the result variable with js after the php updated it with the submit button you clicked (1 or 2). You really dont even need formal functions for this.
2
u/tholder Dec 04 '25
This is a perfect example of when someone should be vibe coding. Even the worst of llms won’t write php this badly.
3
u/CenturiesAgo Dec 05 '25
Hopefully you will figure it out yourself but if not then this should help
2
u/michawb Dec 04 '25
but why do you not do:
1
u/colshrapnel Dec 04 '25
PHP nowhere works like that. See here: https://stackoverflow.com/q/13840429/285587
1
u/xreddawgx Dec 04 '25
button#() are defined as js but you're trying to call it server-side. No Bueno there.
1
u/thisdogofmine Dec 05 '25
Echo out the function calls. i.e :Echo "button1();";
1
u/Tricky_Astronaut_586 Dec 10 '25 edited Dec 10 '25
-1
u/vita10gy Dec 04 '25 edited Dec 04 '25
You need to move </script> to after those is set checks.
Or wrap those calls in script tags so it's <script>button1()</script>
Edit: At the risk of being discouraging though I would say that unless you're trying to prove some concept for something more substantial this approach has the smell of a more fundamental misunderstanding of what's happening here. You can't call JS from PHP like that, but also you either don't need JavaScript involved to do this OR you don't need PHP involved to do this. There ARE cases where you might want a blended approach, but not for anything near this simple.
-2
u/mauriciocap Dec 04 '25
Only a tiny fraction of syntactically correct programs work. We can always invoke Turing's halting problem to give our bugs some prestige!
-3
u/[deleted] Dec 05 '25
PHP doesn't know what your button1() and button2() functions are, because they are JavaScript. To use PHP to call a JS function you can change your if statement to: