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
1 Answer
Reset to default 1From 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
版权声明:本文标题:javascript - what is required to run ajax - does adding jquery to page processes ajax calls? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745473920a2659872.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论