galvin Posted August 1, 2010 Share Posted August 1, 2010 Anyone see anything wrong with my syntax in the AJAX snippet below? It's not doing either of the if/else statements... if (xmlhttp.responseText=='alreadyvoted') { document.getElementById("thanks").innerHTML="Sorry you already voted."; } else { document.getElementById("checkmark").style.display='inline'; document.getElementById("votedefault").style.display='none'; document.getElementById("thanks").innerHTML="Thanks for your vote."; } But if I just make it say the following, it does those three things fine... document.getElementById("checkmark").style.display='inline'; document.getElementById("votedefault").style.display='none'; document.getElementById("thanks").innerHTML="Thanks for your vote."; FULL FUNCTION: function votetwo(val) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); xmlHttp=null; } } } if (xmlHttp !== null) { xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if (xmlhttp.responseText=='alreadyvoted') { document.getElementById("thanks").innerHTML="Sorry already voted."; } else { document.getElementById("checkmark").style.display='inline'; document.getElementById("votedefault").style.display='none'; document.getElementById("thanks").innerHTML="Thanks for your vote. "; } } } xmlHttp.open("GET","submitvote2.php",true); xmlHttp.send(null); } } FULL PHP for submitvote2.php: <?php require_once("includes/session.php"); require_once("includes/connection.php"); require_once("includes/functions.php"); $sql = "SELECT ip from useranalysis WHERE ip = '{$_SESSION['ip']}' AND player1id = '{$_SESSION['player1idnum']}' AND player2id = '{$_SESSION['player2idnum']}' AND week = '{$_SESSION['week']}'"; $checkforip = mysql_query($sql, $connection); if (!$checkforip) { die("Database query failed: " . mysql_error()); } else { $num = mysql_num_rows($checkforip); if ($num > 0) { $_SESSION['alreadyvoted'] = "Sorry, you have already voted for this matchup this week"; echo "alreadyvoted"; } else { $sql = "INSERT into useranalysis (player1id, player2id, username, week, votefor, ip) values ('{$_SESSION['player1idnum']}', '{$_SESSION['player2idnum']}', 'VOTE', '{$_SESSION['week']}', '{$_SESSION['player2idnum']}', '{$_SESSION['ip']}')"; $addvote = mysql_query($sql, $connection); if (!$addvote) { die("Database query failed: " . mysql_error()); } else { $success = "Success! Vote was added!"; echo "success"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <META name="description" content=""> <META name="keywords" content=""> <link href="stylesheets/main.css" media="all" rel="stylesheet" type="text/css" /> </head> <body> </body> </html> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 2, 2010 Share Posted August 2, 2010 You're using a lowercase 'h' instead of uppercase: if (xmlhttp.responseText=='alreadyvoted') { Your object is stored in xmlHttp not xmlhttp. Quote Link to comment Share on other sites More sharing options...
galvin Posted August 2, 2010 Author Share Posted August 2, 2010 ugh, can't believe it was something so minor. Thank you so much for letting me know! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.