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
https://forums.phpfreaks.com/topic/209536-ajax-ifelse-not-working/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.