AJAX sending wrong information - any idea why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dushkin
    New Member
    • Feb 2012
    • 8

    AJAX sending wrong information - any idea why?

    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

    Code:
    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 "informatio n1".

    This is my script for the AJAX

    Code:
    $.ajax({
    url:'inc/checkStatus.php',
    data: currentData,
    type: "GET",
    cache: false,
    this is the currentData variable -

    Code:
    var currentData = "information" + number;
    the number variable is just 1.

    To sum this message up -

    The way I wrote the "currentDat a" 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 "informatio n1" as text instead of a variable?

    Any help is appreciated as always, thank you in advance!
    Last edited by Dormilich; Feb 5 '12, 10:07 AM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    That's because when you put double quotes around a string, it's treated as a string. Not a variable. "informatio n1" 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.

    Comment

    • dushkin
      New Member
      • Feb 2012
      • 8

      #3
      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?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

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

        Comment

        • dushkin
          New Member
          • Feb 2012
          • 8

          #5
          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..

          Code:
          function processServers() {
          	var amountOfServers = 5;
          	var i=1;
          	while (i <= amountOfServers) {
          		'checkServer'+i+'()';
          		i++;
          	}
          }
          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.

          Comment

          Working...