6 Replies - 3271 Views - Last Post: 03 March 2011 - 07:35 AM

#1 DanielRH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-March 11

Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 03:23 AM

Hi :)

I'm having trouble with this fuction/AJAX call... I'm somewhat new to javascript, and i cant figure out what is wrong...

I'm trying to send the call to my other php page called "calculate.php" (without quotes), it is supposed to package up the variables and send them to the php page, and then display the results in a <div> tag...

Could someone help me figure out whats wrong with my code? Thanks in advance!!! :)

Heres the code:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
var helm = "1";
var weaponh = "1";
var weapon = "1";
var armor = "1";
var shield = "1";

function stats() {
	$(".link").click(function stats() {
	
		$.ajax({
			url: "calculate.php",
			data: 'helm='+helm+'&weapon='+weapon+'&armor='+armor+'&shield='+shield+'&weaponh='+weaponh+'',
			cache: false,
			success: function stats(html){
				$("#results").append(html);
			}
			});
	});
});
</script>
</head>
<body>
<a href="#" id="link" class="link">Update Stats</a><br />
<div id="results" class="results">
</div>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Function/AJAX call not working, wrong code?

#2 DanielRH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-March 11

Re: Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 04:56 AM

Would LOVE help... i still cant figure out whats wrong... :(
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 05:33 AM

anything in the error console?

This post has been edited by Dormilich: 03 March 2011 - 05:35 AM
Reason for edit:: did not read carefully enough

Was This Post Helpful? 0
  • +
  • -

#4 DanielRH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-March 11

Re: Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 05:51 AM

na... :( no errors in the error console...

Ugh...
Was This Post Helpful? 0
  • +
  • -

#5 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 06:05 AM

Mention clearly what is not happening as it should. Show us your PHP file. And also try this using load function :


<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
var helm = "1";
var weaponh = "1";
var weapon = "1";
var armor = "1";
var shield = "1";

$(document).ready(function(){
	$(".link").click(function(){
	

		$("#results").load("calculate.php",
    {"helm":helm,"weaponh":weaponh,"weapon":weapon,"armor":armor,"shield":shield});

     });
});
</script>
</head>
<body>
<a href="#" id="link" class="link">Update Stats</a><br />
<div id="results" class="results">
</div>
</body>
</html>



Few points:

1. You don't need to give a function name inside click event as it needs to be an anonymous function (without name).

2. The click event function is enclosed in a function. WHY ? It should rather be inside $(document).ready(function(){ //here });

Read here.

This post has been edited by chinchang: 03 March 2011 - 06:07 AM

Was This Post Helpful? 2
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 06:09 AM

View Postchinchang, on 03 March 2011 - 02:05 PM, said:

1. You don't need to give a function name inside click event as it needs to be an anonymous function (without name).

that’s not a bad approach as it allows you to reference the function inside its body.


View Postchinchang, on 03 March 2011 - 02:05 PM, said:

2. The click event function is enclosed in a function. WHY ? It should rather be inside $(document).ready(function(){ //here });

wouldn’t have been a problem if the stats() function would actually have been called, but that’s definitely the better approach.

This post has been edited by Dormilich: 03 March 2011 - 06:09 AM

Was This Post Helpful? 0
  • +
  • -

#7 DanielRH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-March 11

Re: Function/AJAX call not working, wrong code?

Posted 03 March 2011 - 07:35 AM

Thanks guys! got that done! few! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1