By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,928 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,928 IT Pros & Developers. It's quick & easy.

AJAX sending wrong information - any idea why?

P: 8
I have a question that you will probably be able to answer in a snap, though it has been a constant issue of mine for over a day.

With my PHP script, I produce a variable

Expand|Select|Wrap|Line Numbers
  1. var information1 = 'ip=127.0.0.1&port=80';
When I try to use this variable in my AJAX script, this is the result being sent to my server -

checkStatus.php?information1

Now, as far as I can see, that isn't supposed to happen, its supposed to send the contents of the variable "information1".

This is my script for the AJAX

Expand|Select|Wrap|Line Numbers
  1. $.ajax({
  2. url:'inc/checkStatus.php',
  3. data: currentData,
  4. type: "GET",
  5. cache: false,
this is the currentData variable -

Expand|Select|Wrap|Line Numbers
  1. var currentData = "information" + number;
the number variable is just 1.

To sum this message up -

The way I wrote the "currentData" variable, is it correct? If not, what is the correct way of writing it, and if it is, do you have any idea why AJAX is sending "information1" as text instead of a variable?

Any help is appreciated as always, thank you in advance!
Feb 4 '12 #1

✓ answered by Rabbit

Don't use the string at all. Just use the variable name.

Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,430
That's because when you put double quotes around a string, it's treated as a string. Not a variable. "information1" is just a string. Just because you so happen to name a variable the same as what's in a string doesn't mean that the value of the variable will be placed in the string.
Feb 5 '12 #2

P: 8
Do you know how I could avoid making it a string? From what you said it means I need it as a variable.

I searched around and found a method that apparently works, it said to use "window" and some square brackets, although I do not know how I would implement this into my script, can you help me with this please?
Feb 5 '12 #3

Rabbit
Expert Mod 10K+
P: 12,430
Don't use the string at all. Just use the variable name.
Feb 5 '12 #4

P: 8
Okay I have no idea how I could possibly do that, I have some new code that may be easier to understand what I mean..

Expand|Select|Wrap|Line Numbers
  1. function processServers() {
  2.     var amountOfServers = 5;
  3.     var i=1;
  4.     while (i <= amountOfServers) {
  5.         'checkServer'+i+'()';
  6.         i++;
  7.     }
  8. }
  9.  
instead of
'checkServer'+i+'()';
I need it to say
checkServer1(), then on the next loop it would say checkServer2() and so on..

I do not know if this is even possible, can you please help? I am not a javascript coder.

----------------------------------------------
----------------------------------------------

Thank you for all your help Rabbit, after several more hours of googling and rephrasing my search, I finally found the help I needed!

Turns out I had to use the square brackets afterall.
Feb 5 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.