Search code examples
javascriptphpjqueryajaxcloud9-ide

c9 - How to run php function from script tag?


I use cloud 9 platform. I have php file with several functions call 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!";
}
?>

From the form I have a button and when the onclick event occurs, I need to run the function checkUser.

login.php file:

<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>

The ajax call is not working. Thank you in advance for your help.


Solution

  • Try this:

    <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>
    

    It looks like your if statements are keeping your ajax call from running. I've commented them out for you, and you can see their values using console.log. I also changed your ajax call to a more concise post function

    And change your php file like so:

    <?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";
    }
    ?>