onClick监听器没有调用JavaScript [关闭]

I have an onClick listener set to trigger an AJAX call which in turn triggers a PHP script. However, it doesn't seem to recognize the click. Any ideas?

Index.html

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function doThings(){
    document.write("Test");
    $.ajax( {
    type: "GET",
    url: "script.php",
    success:function(data){
    if(data==0)
        $("#imgId").attr("src","lightbulb_0.png");
    else
        $("#imgId").attr("src","lightbulb_1.png");
    }
    });
}
</script>
</head>
<body>
<img id="imgId" src="lightbulb_0" onClick='doThings();'/>
</body>
</html>

Script.php

<?php
function toggle()
{
        $filename = "brightness";
        $otherDirectory="/sys/class/leds/led0/";
        $f = fopen($otherDirectory . $filename,"r");
        $value = fgets($f);
        $newvalue = 1;
        fclose($f);
        if ((int)$value == 1) {
           $newvalue = 0;
        }

        if ((int)$value == 0) {
           $newvalue = 1;
        }

        $f = fopen($otherDirectory . $filename,"w+");

        fwrite($f, $newvalue);

        fclose($f);

        echo $newvalue;

}
toggle();
?>

EDIT: Added code on here, and used fix from below. Now the onClick() works, but after setting the image to lightbulb_1, it won't go to 0.

EDIT2: Fixed using combination of all solutions below. Thanks alot!

Add Separate <script> tag for including js files and code. Use below code,

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function doThings(){
    //Your function code
}
</script>

//Html codes
<img id="imgId" src="lightbulb_0" onClick='doThings();'/>

Your HTML / JS is fine (with the edit you made), it is your PHP that has an error in it. You have echo newvalue when it should be echo $newvalue:

<? php
function toggle() {
    $filename = "brightness";
    $otherDirectory = "/sys/class/leds/led0/";
    $f = fopen($otherDirectory.$filename, "r");
    $value = fgets($f);
    $newvalue = 1;
    fclose($f);
    if ((int) $value == 1) {
        $newvalue = 0;
    }

    if ((int) $value == 0) {
        $newvalue = 1;
    }

    $f = fopen($otherDirectory.$filename, "w+");

    fwrite($f, $newvalue);

    fclose($f);

    echo $newvalue;

}
toggle(); 

?>

If that doesn't work, have a look at if your file brightness (without an extension?) does exist and your directory path is correct.

inside you ajax call you xud include "dataType" that your php script is returning, example if reurning json you will have above, remeber to json_encode it.

type: "GET",
dataType: "json",
url: "script.php",

if html

type: "GET",
dataType: "html",
url: "script.php",