Jump to content

Ajax if/else not working....


galvin

Recommended Posts

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.