[jQuery] AJAX not working
I am having trouble with AJAX:
In document ready i have:
$.post("test.php", function(data){alert('Success');});
and test'php is just <?php ?>...the alert never comes up
This is just a test... In addition to the callback not working, when
the php file updates a mySQL database, those updates are never made.
Why could this be? I'll put all my js code below:
id=0;
p1uid = 0;
p2uid = null;
multiplayer = false;
diff=3;
p1=true;
lock = false;
intID = 0;
cash = 500;
hisCash = 500.5;
turn=0;
bid=0;
hisBid=0;
localturn=0;
$(function() {
if(multiplayer){
$('input.mult').hide();
$('select.mult').hide();
}id = $('form').attr('id');
$.post("test.php", function(data){alert('Success');});
//intID = setInterval("cycle()", 5000);
});
function cycle(){
if(!lock){
localturn = turn;
updateLocals();
if(turn != localturn) play();
if(turn > 4) clearInterval(intID);
}
}
function play(){
if(turn == 0 || turn == 2){
$('input.msg').val("Enter a Bid");
}else if(turn == 3){
$('input.msg').val("He bid " + stringify(hisBid) + ". Make a
move!");
}else if(turn == 1){
$('input.msg').val("Waiting for opponent to bid.");
}else if (turn == 4){
$('input.msg').val("He bid " + stringify(hisBid) + ". His turn");
if(!multiplayer) compMove();
}else{
$('input.msg').val("Game Over!");
}$('input.cash').val(stringify(cash));
$('input.hisCash').val(stringify(hisCash));
}
function updateLocals(){
lock = true;
$.post("update.php", {id: id, player: p1}, function(data){
cash = data["cash"];
hisCash = data["hisCash"];
turn = data["turn"];
bid = data["bid"];
hisBid = data["hisBid"];
for(i=0; i<9; i++){
position = "b"+i;
$(position).val(data[position]);
}
}, "json");
lock = false;
}
function clear_with(form) {
lock = true;
cash = 500.5;
hisCash = 500;
turn = 0;
for (i=0;i<9; ++i) {
position="b"+i;
form[position].value=' ';
}
form.cash.value = "500*";
form.hisCash.value = "500";
form.output.value = "Enter a bid to begin";
form.bid.value = "";
$.post("reset.php", {id: form.id, token: true}, function(data)
{form.id = data;});
lock = false;
}
// change board when button is clicked
function clear_all(form) {
lock = true;
hisCash = 500.5;
cash = 500;
turn = 0;
for (i=0;i<9; ++i) {
position="b"+i;
form[position].value=' ';
}
form.cash.value = "500";
form.hisCash.value = "500*";
form.output.value = "Enter a bid to begin";
form.bid.value = "";
$.post("reset.php", {id: form.id, token: false}, function(data)
{form.id = data;});
lock = false;
}
function stringify(num){
if(num % 1 == 0) {return num;}
else{return (Math.floor(num) + "*");}
}
function pickBid(form){
lock = true;
placeBid(form, form.chip.value == ' ' && form.cash.value.indexOf('
') != -1);
lock = false;
}
function setBothBids(form, tok){
hisBid = (Math.random()/2 + 1/4)*hisCash;
if(tok) {hisBid = Math.round(hisBid);}
else{hisBid = Math.round(hisBid*2)/2;}
$.get("setBothBids.php", {id: form.id, player2: hisBid, player1:
bid}, function(data){
alert(data.turn);
turn=data.turn;
cash=data.cash;
hisCash=data.hisCash;
}, "json");
}
function setBid(form){
$.post("setBid.php", {id: form.id, b: bid, player1: p1},
function(data){
turn = data.turn;
if(turn != 1){
hisBid = data.hisBid;
cash=data.cash;
hisCash=data.hisCash;
}
}, "json");
}
function placeBid(form, tok){
if(turn != 0 && turn != 2){alert("It is not time to bid!" + turn);
return;}
if(isNaN(form.bid.value)) {alert("Enter a number"); return;}
bid = parseInt(form.bid.value);
if(tok) bid += .5;
if(bid > cash || bid < 0) {alert("Bid out of range!"); return;}
if(!multiplayer){
setBothBids(form, tok);
}else{
setBid(form);
}play();
}
// change board when button is clicked
function clickit(field) {
lock = true;
if (turn > 4) {alert("Reset to play again"); return;}
if (field.value != ' ')
{alert("Can't go there"); return;}
if(turn < 3) {alert("Still in bidding stage"); return;}
field.value= "X";
if (eval_pos(field.form)) {
field.form.output.value="You Win!";
turn = 5;
//$.post("click.php", {id: field.form.id, pos: field.name, player1:
p1, bid: bid, hisBid: hisBid, turn: 'win'});
return;
}
//$.post("click.php", {id: field.form.id, pos: field.name, player1:
p1, bid: bid, hisBid: hisBid, turn: 'bid'});
field.form.bid.value = "";
turn = 0;
lock = false;
play();
}
function compMove(form){
lock = true;
position = get_move(form);
if (position=="") {
form.output.value="No Winner.";
turn = 7;
//$.post("draw.php", {id: form.id});
return;
}
form[position].value="O";
if (eval_pos(form)) {
form.output.value="You Lose!";
turn = 6;
//$.post("click.php", {id: form.id, pos: position, player1: !p1,
bid: hisBid, hisBid: bid, turn: 'win'});
return;
}
//$.post("click.php", {id: form.id, pos: position, player1: !p1,
bid: hisBid, hisBid: bid, turn: 'bid'});
turn = 0;
lock = false;
play();
}
// see if there is a winner
function eval_pos(form) {
if ((form.b0.value!=' ' && form.b0.value==form.b3.value &&
form.b0.value==form.b6.value)||
(form.b0.value!=' ' && form.b0.value==form.b1.value &&
form.b0.value==form.b2.value) ||
(form.b0.value!=' ' && form.b0.value==form.b4.value &&
form.b0.value==form.b8.value) ||
(form.b1.value!=' ' && form.b1.value==form.b4.value &&
form.b1.value==form.b7.value) ||
(form.b2.value!=' ' && form.b2.value==form.b5.value &&
form.b2.value==form.b8.value) ||
(form.b2.value!=' ' && form.b2.value==form.b4.value &&
form.b2.value==form.b6.value) ||
(form.b3.value!=' ' && form.b3.value==form.b4.value &&
form.b3.value==form.b5.value) ||
(form.b6.value!=' ' && form.b6.value==form.b7.value &&
form.b6.value==form.b8.value))
return true;
else
return false;
}
// get position for move.
function comp_move(form,player,weight,depth) {
var cost;
var bestcost=-2;
var position;
var newplayer;
if (player=="X") newplayer="O"; else newplayer="X";
if (depth==diff) return 0;
if (eval_pos(form)) return 1;
for (var i=0; i<9; ++i) {
position='b'+i;
if (form[position].value != ' ')
continue;
form[position].value=player;
cost = comp_move(form,newplayer, -weight, depth+1);
if (cost > bestcost) {
bestcost=cost;
if (cost==1) i=9;
}
form[position].value=' ';
}
if (bestcost==-2) bestcost=0;
return(-bestcost);
}
// get position for move.
function get_move(form) {
var cost;
var bestcost=-2;
bestmove="";
// don't think about first move.
if (form.b4.value==' ') return "b4";
for (var i=0; i<9; ++i) {
localposition='b'+i;
if (form[localposition].value != ' ')
continue;
form[localposition].value="O";
cost=comp_move(form,"X", -1, 0);
if (cost > bestcost) {
if (cost==1) i=9;
bestmove=localposition;
bestcost=cost;
}
form[localposition].value=' ';
}
return bestmove;
}
function onEnter( evt, frm ) {
var keyCode = null;
if( evt.which ) {
keyCode = evt.which;
} else if( evt.keyCode ) {
keyCode = evt.keyCode;
}
if( 13 == keyCode ) {
frm.Bid.click();
return false;
}
return true;
}
function changeDiff(){
$.post("diff.php", {id: id, diff: diff});
}