admin管理员组

文章数量:1426314

I am new to ajax and running xampp on my localhost I want to municate with server by using ajax in my own created page.

I want to know is there anything required to add in head section to use ajax calls ?

In my web page's head section there is no javascript file incluided other than this jquery file

<script src=".11.2/jquery.min.js"></script>

is this sufficient to make ajax calls like

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            //data: {id: 'enter_game'},
            dataType: 'json',
            cache: false,
            success: function(result) {
            if(result){
                resultObj = eval (result);
                alert( resultObj );
            }else{
                alert("error");
            }
            }
        });
        });
</script>

and in functions.php file

<?php
include("connection_to_db.php");

function is_sufficient_users()
    {
        $result = mysql_query("SELECT * FROM ".$dbprefix."users") or die ( mysql_error() );
        if(mysql_num_rows($result) >= 3 ){
            // sufficient users
            // start the game
            $data = array();
            //$word = start_game();
            $word = 'Apple';
            while($row = mysql_fetch_assoc($result)){
                $data['id']['name'] = $row['name'];
                $data['id']['ghost'] = $row['ghost']; 
            }
            $data['word'] = $word;
            $data['game'] = "Game Starts";

            echo json_encode($data);
        }else{
            $data = array();
            switch(mysql_num_rows($result))
            {
                case 0:
                $waiting = 3;
                break;
                case 1:
                $waiting = 2;
                break;
                case 2:
                $waiting = 1;
                break;
                default:
                $waiting = 3;
                break;
            }
            $data['game'] = "Waiting for ".$waiting." more users to start the game" ;
            echo json_encode($data);
        }
    }

if(isset($_GET['id']) && strtolower($_GET['id']) == "enter_game"){
    is_sufficient_users();
}
?>

but there is no response, I tried several changing but no success. Is there a fault in my code or I am not including the ajax connection file or something ? Any help would be much appreciated.

I am new to ajax and running xampp on my localhost I want to municate with server by using ajax in my own created page.

I want to know is there anything required to add in head section to use ajax calls ?

In my web page's head section there is no javascript file incluided other than this jquery file

<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>

is this sufficient to make ajax calls like

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            //data: {id: 'enter_game'},
            dataType: 'json',
            cache: false,
            success: function(result) {
            if(result){
                resultObj = eval (result);
                alert( resultObj );
            }else{
                alert("error");
            }
            }
        });
        });
</script>

and in functions.php file

<?php
include("connection_to_db.php");

function is_sufficient_users()
    {
        $result = mysql_query("SELECT * FROM ".$dbprefix."users") or die ( mysql_error() );
        if(mysql_num_rows($result) >= 3 ){
            // sufficient users
            // start the game
            $data = array();
            //$word = start_game();
            $word = 'Apple';
            while($row = mysql_fetch_assoc($result)){
                $data['id']['name'] = $row['name'];
                $data['id']['ghost'] = $row['ghost']; 
            }
            $data['word'] = $word;
            $data['game'] = "Game Starts";

            echo json_encode($data);
        }else{
            $data = array();
            switch(mysql_num_rows($result))
            {
                case 0:
                $waiting = 3;
                break;
                case 1:
                $waiting = 2;
                break;
                case 2:
                $waiting = 1;
                break;
                default:
                $waiting = 3;
                break;
            }
            $data['game'] = "Waiting for ".$waiting." more users to start the game" ;
            echo json_encode($data);
        }
    }

if(isset($_GET['id']) && strtolower($_GET['id']) == "enter_game"){
    is_sufficient_users();
}
?>

but there is no response, I tried several changing but no success. Is there a fault in my code or I am not including the ajax connection file or something ? Any help would be much appreciated.

Share Improve this question asked Mar 13, 2015 at 3:49 Abid AliAbid Ali 7701 gold badge8 silver badges15 bronze badges 1
  • 1 On the client side, including jQuery is all you need to do to make ajax requests. If its not working, there is something wrong with either the way your server is configured or with the php script. See if your server is throwing any errors and go from there. – wrshawn Commented Mar 13, 2015 at 3:52
Add a ment  | 

1 Answer 1

Reset to default 1

From your code here

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            //data: {id: 'enter_game'},
            dataType: 'json',
            cache: false,
            success: function(result) {
            if(result){
                resultObj = eval (result);
                alert( resultObj );
            }else{
                alert("error");
            }
            }
        });
        });
</script>

you have mented the data that is being sent to the functions.php page. And in functions.php page you are checking for $_GET['id'] which is not present.

Other than that there is no problem in your code, considering you have the right db connection and the jQuery file is loaded.


If you still have doubt then go with the following code for just testing

<script type="text/javascript" language="javascript">
    $('#enter').click(function(event){
    event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'functions.php?id=enter_game',
            cache: false,
            success: function(result) {
               console.log(result);
            }
        });
    });
</script>

functions.php

<?php
echo "This is just to see if Ajax is working and it does !";

本文标签: javascriptwhat is required to run ajaxdoes adding jquery to page processes ajax callsStack Overflow