/    Sign up×
Community /Pin to ProfileBookmark

Why doesn’t this work?

I have followed the discussion on https://www.webdeveloper.com/d/366159-simple-way-to-call-php-function-from-javascript.
I tried to do that myself, but it doesn’t work?
It returns “SyntaxError: expected expression, got ‘<‘ “, resulting in the function to be undefined.
This is my code (at least the important part):

“`
<button id=”button” onclick=”reverseKillSwitch()”> Kill switch is off </button>
<?php
function KillSwitch(){
$kys = fopen(“killswitch.txt”, “r”);
$data = fread($kys, 1);
$kysw = fopen(“killswitch.txt”, “w”);
if ($data == “1”) {
fwrite($kysw, “0”, 200);
echo “<script>document.getElementById(‘exampleModalLabel’).innerHTML = ‘Kill switch is off’ </script>”;
}
else {
fwrite($kysw, “1”, 200);
echo “<script>document.getElementById(‘exampleModalLabel’).innerHTML = ‘Kill switch is on’ </script>”;
}
}
?>
<script>
function reverseKillSwitch(){
var kill = <?php echo KillSwitch(); ?>
}
</script>
“`

What am I doing wrong? I would just use Node.js but I can’t figure out how to add it to WAMP.
Any help would be appreciated!

to post a comment
Full-stack DeveloperHTMLJavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@moataz14Dec 28.2019 — the fread function need to change to fread=($kys,"18")
Copy linkTweet thisAlerts:
@SempervivumDec 28.2019 — The thread you linked was too long for me to read.

Do I understand your intention correctly? Whenever the button is clicked you want to
  • 1. toggle the content of the file on the server

  • 2. change the innerHTML of the button

    It this is correct you need to use Ajax to update the file when you want to do it without reloading the page.
  • ×

    Success!

    Help @ihatemylife spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...