Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2012, 12:19 AM   PM User | #1
zingzing45
New Coder

 
Join Date: Jun 2012
Posts: 57
Thanks: 13
Thanked 0 Times in 0 Posts
zingzing45 is an unknown quantity at this point
Sending variable on mouseover

Hi, I am trying to send a variable on mouseover but it does not seem to work - I took this to the PHP forum, since PHP was involved, but a guy there said that it was not a PHP problem.


So can anyone spot a possible error in this code?



Code:
function addAward()

{

$(document).ready(function()

{

$.ajax(
		
		{
		
		type: "POST",
		
		url: "achievements.php",
		
		data: { HomeCoinLocator: "Yes" }

		});

		});

var div = document.getElementById("newachievementdiv");

var span = div.getElementsByTagName("span")[1]; 

span.innerHTML = "' . $endtext . '";

$(div).fadeIn("4000");

setInterval( function() { $("#newachievementdiv").fadeOut("4000"); }, 4000);
		
}




BTW, the important stuff is in bold.

Thanks.
zingzing45 is offline   Reply With Quote
Old 09-20-2012, 07:02 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Three things ...

1) And this is the main problem: You are trying to access the DIV with ID "newachievementdiv" outside of the $(document).ready() callback. So this DIV might not be ready yet
2) There is no event handler for mouseover ... so how should your program know that it should send something on mouseover?
3) You are already using jQuery, so you should not use getElementxxxxxxxxxx methods but rather the respective jQuery methods like $('#newachievementdiv span').eq(1)
devnull69 is offline   Reply With Quote
Old 09-20-2012, 01:25 PM   PM User | #3
zingzing45
New Coder

 
Join Date: Jun 2012
Posts: 57
Thanks: 13
Thanked 0 Times in 0 Posts
zingzing45 is an unknown quantity at this point
Thumbs up Ok

Thanks for the tips - the first one I'll look into, but the second one is something that you didn't see.

The function is called on a mouseover inside the HTML tag, but I didn't show that part. Finally, I might look over the third tip you gave me.


Thanks a lot, that might help me out a lot
zingzing45 is offline   Reply With Quote
Old 09-20-2012, 02:01 PM   PM User | #4
zingzing45
New Coder

 
Join Date: Jun 2012
Posts: 57
Thanks: 13
Thanked 0 Times in 0 Posts
zingzing45 is an unknown quantity at this point
?

Help -


<script>

function addAward()

{

$(document).ready(function()

{

$.ajax(

{

type: "POST",

url: "achievements.php",

data: { HomeCoinLocator: "Yes" }

});

var div = $("#newachievementdiv span").eq(1)

var span = $("span").get(4);

span.innerHTML = "' . $endtext . '";

$(div).fadeIn("4000");

setInterval( function() { $("#newachievementdiv").fadeOut("4000"); }, 4000);

});

}

</script>





Why doesn't this work?
zingzing45 is offline   Reply With Quote
Old 09-21-2012, 12:50 AM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
function addAward()

{

$(document).ready(function()
document.ready cannot (or at least, should not!) occur within a function. But you never call the function (unless you call it from some code that you haven't shown us) so the code is never executed.

[Personally, I wouldn't use div and span as variable names - it can only lead to confusion, and possible conflict.]
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 09-21-2012 at 12:55 AM..
AndrewGSW is offline   Reply With Quote
Old 09-21-2012, 01:00 AM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
This
Code:
span.innerHTML = "' . $endtext . '";
seems to be wrong. It should be
Code:
span.innerHTML = "'" . $endtext . "'";    // or perhaps..
span.innerHTML = "$endtext";
depending on where/how it is positioned within your PHP code.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 09-21-2012 at 01:03 AM..
AndrewGSW is offline   Reply With Quote
Old 09-21-2012, 01:58 PM   PM User | #7
zingzing45
New Coder

 
Join Date: Jun 2012
Posts: 57
Thanks: 13
Thanked 0 Times in 0 Posts
zingzing45 is an unknown quantity at this point
Angry This is so hard

Even though I followed all of your advice, this code below doesn't work!



<div id="secretcoin" onmouseover="addAward();">

</div>

<div id="newachievementdiv" align="center" class="awardBox"
style="display: none; margin: 0px auto; position: fixed; left: 250px;">
Congratulations! You have <span class="highlite-blue">found the secret coin</span>!
<br/>
<span></span>
</div>
<script>

$(document).ready(function()

{

function addAward()

{

$.ajax(

{

type: "POST",

url: "achievements.php",

data: { HomeCoinLocator: "Yes" }

});

var div = $("#newachievementdiv span").eq(1)

var span = $("span").get(4);

span.innerHTML = "' . $endtext . '";

$(div).fadeIn("4000");

setInterval( function() { $("#newachievementdiv").fadeOut("4000"); }, 4000);

}

});

</script>




BTW, that div junk at the beginning is where the function gets called.
zingzing45 is offline   Reply With Quote
Old 09-26-2012, 01:57 PM   PM User | #8
zingzing45
New Coder

 
Join Date: Jun 2012
Posts: 57
Thanks: 13
Thanked 0 Times in 0 Posts
zingzing45 is an unknown quantity at this point
Thumbs down

So does this have any visible errors in it? Does anyone know of any weird problems that I might be experiencing?
zingzing45 is offline   Reply With Quote
Old 09-26-2012, 09:43 PM   PM User | #9
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
I don't get it ... is this a mix of PHP and Javascript? Is $endtext the only PHP code here? If this part is PHP, please show us the resulting Javascript code from your browser.
devnull69 is offline   Reply With Quote
Old 09-27-2012, 12:50 AM   PM User | #10
zingzing45
New Coder

 
Join Date: Jun 2012
Posts: 57
Thanks: 13
Thanked 0 Times in 0 Posts
zingzing45 is an unknown quantity at this point
Code:
<!DOCTYPE html>

<html>

<head>

<title>Welcome to CoinAwards</title>

<link rel="stylesheet" href="structure/home.css" />

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>

<script src="structure/jQueryEase.js"></script>

<script src="structure/home.js"></script>

</head>

<body>



<script>

$(document).ready(function()

{

function addAward()

{

$.ajax(
		
		{
		
		type: "POST",
		
		url: "achievements.php",
		
		data: { HomeCoinLocator: "Yes" }

		});

var div = document.getElementById("newachievementdiv");

var span = div.getElementsByTagName("span")[1]; 

span.innerHTML = "Rewards:<br>+120 Coins<br>+10 Skill Points<br>+1 Award";

$(div).fadeIn("4000");

setInterval( function() { $("#newachievementdiv").fadeOut("4000"); }, 4000);
		
}

		});

</script>

<header>

<hgroup>

<h1 id="coin-awards-welcome">Welcome to CoinAwards!</h1>

<h2 id="coin-awards-welcome-subtitle">A place where you can <span class="highlite-blue">earn coins</span> and <span class="highlite-blue">trade them
</span> in for cool <span class="highlite-blue">virtual prizes</span>!</h2>

</hgroup>

</header>

<section id="extrastuff">

<div id="usefullinksdiv">

<img src="structure/rightarrow.jpg" id="ra" />

</div>

<div id="actualusefullinks">

<a href="structure/forum.php"><input type="button" value="Forum" id="forumbutton" /></a>

<a href="structure/profile.php"><input type="button" value="Profile" id="profilebutton" /></a>

</div>

</section>

<section id="loginbox" style="height:160px;">

<p id="usernamelink">Welcome, <a href="structure/profile.php" id="greenlink">gr</a> | <a href="structure/logout.php" id="orangelink">Logout</a></p>

<p id="userinfop">You have 100 coins</p>

<p id="userskillp">You have 5 skill points</p>

<p id="userawardsp">You have 1 award</p>

</section>

<section id="mainMenu" style="z-index: 2;">

<ul>  
<li id="li1" class="green">  
<p><a href="../Advance_To_Coin_Awards.php">Home</a></p>  
<a href="../Advance_To_Coin_Awards.php"><div class="subtext">Home</div></a>
</li>  
<li id="li2" class="yellow">  
<p><a href="structure/whatisthis.php">What is this?</a></p>  
<a href="structure/whatisthis.php"><div class="subtext">What is this?</div></a>
<a href="structure/moreinfo.php"><div class="subtext">More Info</div></a>
<a href="structure/tipsandtricks.php"><div class="subtext">Tips and Tricks</div></a>
</li>  
<li id="li3" class="red">  
<p><a href="structure/earncoins.php">Earn Coins</a></p>  
<a href="structure/earncoins.php"><div class="subtext">Earn Coins</div></a>
<a href="structure/achievements.php"><div class="subtext">Achievements</div></a>
</li>  
<li id="li4" class="blue">  
<p><a href="structure/chooseawards.php">Shop</a></p>  
<a href="structure/chooseawards.php"><div class="subtext">Shop</div></a>
<a href="structure/wishlist.php"><div class="subtext">Wish List</div></a>
<a href="structure/buyinghistory.php"><div class="subtext">Buying History</div></a>
</li>  
<li id="li5" class="purple">  
<p><a href="structure/profile.php">Profile</a></p>  
<a href="structure/profile.php"><div class="subtext">Profile</div></a>
<a href="structure/spruceitup.php"><div class="subtext">Spruce it up</div></a>  
<a href="structure/compare.php"><div class="subtext">Compare</div></a>
</li>  
<li id="li6" class="brown">
<p><a href="structure/allactivities.php">All Activities</a></p>
<a href="structure/allactivities.php"><div class="subtext">All Activities</div></a>
<a href="structure/games.php"><div class="subtext">Games</div></a>
<a href="structure/chatroom.php"><div class="subtext">Chat Room</div></a>
</li>
</ul> 

</section>

<section id="home-content">

<h2 id="home-content-title">Play games, chat, and earn coins!</h2>

<p id="content-1">Own a customizable profile that sports unlockable parts through the spending of coins, which can be earned by spending time on 
the site, finding secrets, completing achievements, and more. You will have the chance to play games, chat, and do more by visiting this site. While you 
are enjoying the features of this site, you will gain coins - currency. You can spend these coins on an always-updated shop. While it is possible to still 
enjoy this site without logging in (games, chat, etc.), creating an account will help you enjoy the full benefits of your experience here - why not? - 
it is free and fun!</p>

</section>

<footer>

<p id="footercrp">Copyright &copy; CoinAwards 2012 | Hosted for free under <a href="http://www.***************" style="color: #333366;">000webhost</a>. 
Site Version 1.3 by Austin Stout. V 1.3 includes release of achievement #2, as well as important site clean-up.</p>

</footer>

<div id="lsidepanel">

</div>

<div id="rsidepanel">

</div>

<div id="secretcoin" onmouseover="addAward();">
Uncaught ReferenceError: addAward is not defined

</div>

<div id="newachievementdiv" align="center" class="awardBox" 
     style="display: none; margin: 0px auto; position: fixed; left: 250px;">
Congratulations! You have <span class="highlite-blue">found the secret coin</span>!
<br/>
<span></span>
</div>

</body>

</html>				
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->






The related stuff is in bold.
zingzing45 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:05 AM.


Advertisement
Log in to turn off these ads.