missing } returned to AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tarik Monem
    New Member
    • May 2007
    • 63

    missing } returned to AJAX

    Hi guys, it's me one more time today.

    I have a PHP file echoing an array back to a javascript file.

    Here's the Array being generated:
    Code:
    $myTempArray[$count] .= "{SHOWS:\"".htmlentities($shows_new2)."\",GENDER:\"".htmlentities($gender_new2)."\",AGE:\"".htmlentities($age_new2)."\",FIRST_NAME:\"".htmlentities($user_first_name_new2)."\",LAST_NAME:\"".htmlentities($user_last_name_new2)."\",EMAIL_ADDRESS:\"".htmlentities($emailaddress_new2)."\",PHONE_NUMBER:\"".htmlentities($phone_number_new2)."\",CITY:\"".htmlentities($city_new2)."\",STATE:\"".htmlentities($state_new2)."\",ZIP:\"".htmlentities($zip_new2)."\",PICTURE:\"".htmlentities($picture_new2)."\"}";
    $count++;
    
    echo "javascript:mySeFunc([".$myTempVar."]);";
    When it sends the array to the Javascript file, then I receive an error in the JavaScript error console that the "}" is missing.

    What can I do, I've been using this method for many of my applications, but this is the first time that I've encountered this issue.

    I've tried stripslashes, addslashes, htmlentities, etc... but nothing seems to send the entire array without receiving some kind of error.

    As usual, thanks in advance :-)
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    [PHP] $myTempArray[$count] =
    { "SHOWS: \" " .htmlentities($ shows_new2).
    " \",GENDER:\" " .htmlentities($ gender_new2).
    " \",AGE:\" " .htmlentities($ age_new2).
    " \",FIRST_NAME:\ " " .htmlentities($ user_first_name _new2).
    " \",LAST_NAME :\" " .htmlentities($ user_last_name_ new2).
    " \",EMAIL_ADDRES S:\" " .htmlentities($ emailaddress_ne w2).
    " \",PHONE_NUMBER :\" ". htmlentities($p hone_number_new 2).
    " \",CITY:\" ".htmlentities( $city_new2).
    " \",STATE:\" " .htmlentities($ state_new2).
    " \",ZIP:\" " .htmlentities($ zip_new2).
    " \",PICTURE:\ " " .htmlentities($ picture_new2).
    " \" " };
    $count++;[/PHP]

    You have escaped all over the place! I have separated the code to be more readable, and also changed the very beginning from "{SHOWS to {"SHOWS as wellas the end from \"}" to \""}. I recommend using the ' character as well, which would let you define elements with ' and so you wouldn't have to escape (maybe) or stleast eb able to read it easier! Try this code and see how you go.

    Comment

    • Tarik Monem
      New Member
      • May 2007
      • 63

      #3
      I tried your ideas of the single "'" character and removing the escape sequences, but the same error returns. The "{" has to be part of the string being sent back to the JavaScript file, so I didn't change the "{" part of it, as you suggested.

      Code:
      $myTempArray[$count] .= '{SHOWS:"'.htmlentities($shows_new2).'",GENDER:"'.htmlentities($gender_new2).'",AGE:"'.htmlentities($age_new2).'",FIRST_NAME:"'.htmlentities($user_first_name_new2).'",LAST_NAME:"'.htmlentities($user_last_name_new2).'",EMAIL_ADDRESS:"'.htmlentities($emailaddress_new2).'",PHONE_NUMBER:"'.htmlentities($phone_number_new2).'",CITY:"'.htmlentities($city_new2).'",STATE:"'.htmlentities($state_new2).'",ZIP:"'.htmlentities($zip_new2).'",PICTURE:"'.htmlentities($picture_new2).'"}';
      $count++;
      The error has an arrow that appears after the first colon "SHOWS:" if that helps.

      Thanks once again.
      Last edited by Tarik Monem; Mar 12 '08, 01:44 AM. Reason: left out something

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        What about trying brackets around the variable input info? I think it's not registering the variable input as a single string:
        $myTempArray[$count] = (...);

        Comment

        • Tarik Monem
          New Member
          • May 2007
          • 63

          #5
          I tried adding the "(" and ")" and it does not seem to help.

          For some reason, it seems to be having an issue with the ":" character after "javascript:myS eFunc([{SHOWS:" -- it does not matter what the first entry is because I have tried to manipulate the data to have other first entries and it would still hang on the ":" after the word SHOWS of the first entry.

          The weird thing is that I've copied and pasted the data returning from the PHP page and pasted it into the JavaScript function directly and it loads the Datagrid within the Flex app with no problems or errors.

          I do not want to be responsible for cutting & pasting over 27,000 records on a regular basis.

          So any help would be appreciated.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            I'm sure you've tried this, but what about escaping the ":"? ie. "\:"

            Comment

            • RoninOni
              New Member
              • Mar 2008
              • 9

              #7
              Code:
              $myTempArray[$count] .= "{SHOWS:\"".htmlentities($shows_new2)."\",GENDER:\"".htmlentities($gender_new2)."\",AGE:\"".htmlentities($age_new2)."\",FIRST_NAME:\"".htmlentities($user_first_name_new2)."\",LAST_NAME:\"".htmlentities($user_last_name_new2)."\",EMAIL_ADDRESS:\"".htmlentities($emailaddress_new2)."\",PHONE_NUMBER:\"".htmlentities($phone_number_new2)."\",CITY:\"".htmlentities($city_new2)."\",STATE:\"".htmlentities($state_new2)."\",ZIP:\"".htmlentities($zip_new2)."\",PICTURE:\"".htmlentities($picture_new2)."\"}";
              $count++;
               
              echo "javascript:mySeFunc([".$myTempVar."]);";
              what is $myTempVar?? all I see is your setting $myTempArray, which, in that exact code, would only show up as "Array()" to the browser.

              We are either missing a vital piece of code to help you, or you are a)echoing the wrong variable and/or b) trying to echo an array which doesnt work

              Im hoping that perhaps you are missing[PHP]
              foreach($myTemp Array as $myTempVar)[/PHP] on the line prior to your javascript echo

              Oh yah, and you can probably (depending on other code in your file) remove the $count variable and set the left side on line 1 to be
              [PHP]$myTempArray[] = [/PHP]
              ^^ this is basically the same but saves you from having to use count and increment it

              wait, are you trying to append to an array key that hasn't been initialized?

              do me a favor and do a [PHP]print_r($myTemp Array);[/PHP] just to make sure those contents are what you expect

              Comment

              • RoninOni
                New Member
                • Mar 2008
                • 9

                #8
                I also noticed that you referenced AJAX.....

                if you are trying to execute Javascript through ajax you are taking the totally wrong approach. You need to have php echo the results which the AJAX function then parses and runs a function using the contents of the returned page

                Comment

                • Tarik Monem
                  New Member
                  • May 2007
                  • 63

                  #9
                  Yeah, I received a different error when escaping the ":" via illegal character error message.

                  The thing that really bothers me about all this, is that this application worked with no errors or issues before the server crashed a week and a half ago and if I cut & paste the data generated from the PHP file directly into the JS function, then it works.

                  I did discover something this afternoon as to where the issue is located -- between PHP and the JS file.

                  I know because I substituted the variable "response" for one record and it showed up in the Flex app's datagrid with no errors.

                  Code:
                  window.onload = eval(response);
                  with
                  window.onload = eval(mySeFunc([{SHOWS: "some show", GENDER: "male", AGE: "19", etc...}]));
                  The problem seems to be coming from PHP echoing the data to the JS file which evaluates the string coming, once again, I manually cut & paste the one record into the PHP file and I received the error that we started with in this post.

                  Code:
                  echo "mySeFunc([".$myComboVar2."]);";
                  with
                  echo "mySeFunc([{SHOWS: "some show", GENDER: "male", AGE: "19", etc...}]);";
                  And why would this same function that worked before the server crash, suddenly not work any longer?

                  Comment

                  • Tarik Monem
                    New Member
                    • May 2007
                    • 63

                    #10
                    Originally posted by RoninOni
                    Code:
                    $myTempArray[$count] .= "{SHOWS:\"".htmlentities($shows_new2)."\",GENDER:\"".htmlentities($gender_new2)."\",AGE:\"".htmlentities($age_new2)."\",FIRST_NAME:\"".htmlentities($user_first_name_new2)."\",LAST_NAME:\"".htmlentities($user_last_name_new2)."\",EMAIL_ADDRESS:\"".htmlentities($emailaddress_new2)."\",PHONE_NUMBER:\"".htmlentities($phone_number_new2)."\",CITY:\"".htmlentities($city_new2)."\",STATE:\"".htmlentities($state_new2)."\",ZIP:\"".htmlentities($zip_new2)."\",PICTURE:\"".htmlentities($picture_new2)."\"}";
                    $count++;
                     
                    echo "javascript:mySeFunc([".$myTempVar."]);";
                    what is $myTempVar?? all I see is your setting $myTempArray, which, in that exact code, would only show up as "Array()" to the browser.

                    We are either missing a vital piece of code to help you, or you are a)echoing the wrong variable and/or b) trying to echo an array which doesnt work

                    Im hoping that perhaps you are missing[PHP]
                    foreach($myTemp Array as $myTempVar)[/PHP] on the line prior to your javascript echo

                    Oh yah, and you can probably (depending on other code in your file) remove the $count variable and set the left side on line 1 to be
                    [PHP]$myTempArray[] = [/PHP]
                    ^^ this is basically the same but saves you from having to use count and increment it

                    wait, are you trying to append to an array key that hasn't been initialized?

                    do me a favor and do a [PHP]print_r($myTemp Array);[/PHP] just to make sure those contents are what you expect
                    Sorry about the missing code for $myTempVar, but that is used just to add a comma to the end of each record until the end is reached:
                    Code:
                    				 for($i=0;$i<$count;$i++)
                    						{
                    							 if($i<$count-1)
                    							 {
                    							 $myTempVar .= $myTempArray[$i].", ";
                    							 }
                    							 else
                    							 {
                    							 $myTempVar .= $myTempArray[$i];
                    							 }
                    						}
                    						echo "javascript:mySeFunc([".$myTempVar."]);";

                    Comment

                    Working...