c9 - 如何从脚本标签运行php函数?

时间:2015-10-25 18:06:53

标签: javascript php jquery ajax cloud9-ide

我使用的是云端9平台。 我有几个函数的php文件调用phpFunctions.php:

<?php
 $servername = "127.0.0.1";
 $username = "oshrat";
 $password = "";
 $database = "myDB";
 $dbport = 3306;

// Create connection
//$conn = new mysqli($servername, $username, $password);
$conn = new mysqli($servername, $username, $password, $database, $dbport);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
// echo "Connected successfully";
mysql_select_db("myDB",$conn);

function createNewUser() {
 echo "Hello world!";
}

function checkUser($name) {
 echo "Hello world!";
}
?>

从表单中我有一个按钮,当onclick事件发生时,我需要运行checkUser函数。

login.php文件:

<html lang="en-us">

<head>
<meta charset="utf-8">
<?php include_once 'phpFunctions.php';?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">    </script>
</head>

<body>
<div  style="float:left; margin-right:45px;">            
    <button id="startButton" class="controlButtons" type="button"     target="framework" style="margin-left:30vw;">Go To Play</button>
</div>
</body>
<script type="text/javascript">
     $("#startButton").click(function(){
        var p1Name = $("#namePlayer1").val();
        var p2Name = $("#namePlayer2").val();
        //check if the two players enter the name
        if(p1Name == "" || p2Name == "")
        {
            alert("You Must Enter Two Players Name");
        }
            else 
            {
            var result = "";
            jQuery.ajax({
                type: "POST",
                url: 'phpFunctions.php',
                dataType: 'json',
                data: {functionname: 'checkUser'},
        success: function (obj, textstatus) {
                if( !('error' in obj) ) {
                            //   result = obj.result;
                              alert("success");
                          }
                          else {
                            //   console.log(obj.error);
                            alert("error");
                          }
                    }
        });
                   }
    }
});

    </script>
</html>

ajax调用无效。 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

<html lang="en-us">

<head>
<meta charset="utf-8">
<?php include_once 'phpFunctions.php';?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">    </script>
</head>

<body>
<div  style="float:left; margin-right:45px;">            
    <button id="startButton" class="controlButtons" type="button"     target="framework" style="margin-left:30vw;">Go To Play</button>
</div>
</body>
<script type="text/javascript">
     $("#startButton").click(function(){
    //two players mode
     //if(parent.modeGame == 1)
    //{

        console.log(parent.modeGame, $("#namePlayer1").val(), $("#namePlayer2").val()); //this will probably log undefined 3 times...
        /*var p1Name = $("#namePlayer1").val();
        var p2Name = $("#namePlayer2").val();
        //check if the two players enter the name
        if (p1Name == "" || p2Name == "") {
            alert("You Must Enter Two Players Name");
        }
        else 
        {*/
            $.post('phpFunctions.php', {functionname: 'checkUser'}).done(function(data) {
                console.log(data);
            }).fail(function(err) {
                console.log(err);
            });
       // }
    //}
});

    </script>
</html>

看起来你的if语句会让你的ajax调用无法运行。我已经为您评论了这些内容,您可以使用console.log查看其值。我还将您的ajax电话更改为更简洁的post功能

并改变你的php文件:

<?php
 $servername = "127.0.0.1";
 $username = "oshrat";
 $password = "";
 $database = "myDB";
 $dbport = 3306;

// Create connection
//$conn = new mysqli($servername, $username, $password);
$conn = new mysqli($servername, $username, $password, $database, $dbport);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
// echo "Connected successfully";
mysql_select_db("myDB",$conn);

if ($_POST["functionname"] == "checkUser") {
    checkUser("sample name"); //replace sample name with $_POST['name'] or something like that when you want to actually check a name
}

function createNewUser() {
 echo "Hello world!";
}

function checkUser($name) {
 echo "User ".$name." checked";
}
?>