Connecting Tech Pros Worldwide Forums | Help | Site Map

missing } returned to AJAX

Member
 
Join Date: May 2007
Posts: 63
#1: Mar 12 '08
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:
Expand|Select|Wrap|Line Numbers
  1. $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)."\"}";
  2. $count++;
  3.  
  4. echo "javascript:mySeFunc([".$myTempVar."]);";
  5.  
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's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#2: Mar 12 '08

re: missing } returned to AJAX


[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_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++;[/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.
Member
 
Join Date: May 2007
Posts: 63
#3: Mar 12 '08

re: missing } returned to AJAX


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.

Expand|Select|Wrap|Line Numbers
  1. $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).'"}';
  2. $count++;
  3.  
The error has an arrow that appears after the first colon "SHOWS:" if that helps.

Thanks once again.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#4: Mar 12 '08

re: missing } returned to AJAX


What about trying brackets around the variable input info? I think it's not registering the variable input as a single string:
$myTempArray[$count] = (...);
Member
 
Join Date: May 2007
Posts: 63
#5: Mar 12 '08

re: missing } returned to AJAX


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:mySeFunc([{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.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#6: Mar 12 '08

re: missing } returned to AJAX


I'm sure you've tried this, but what about escaping the ":"? ie. "\:"
RoninOni's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 9
#7: Mar 13 '08

re: missing } returned to AJAX


Expand|Select|Wrap|Line Numbers
  1. $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)."\"}";
  2. $count++;
  3.  
  4. echo "javascript:mySeFunc([".$myTempVar."]);";
  5.  
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($myTempArray 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($myTempArray);[/PHP] just to make sure those contents are what you expect
RoninOni's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 9
#8: Mar 13 '08

re: missing } returned to AJAX


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
Member
 
Join Date: May 2007
Posts: 63
#9: Mar 13 '08

re: missing } returned to AJAX


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.

Expand|Select|Wrap|Line Numbers
  1. window.onload = eval(response);
  2. with
  3. window.onload = eval(mySeFunc([{SHOWS: "some show", GENDER: "male", AGE: "19", etc...}]));
  4.  
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.

Expand|Select|Wrap|Line Numbers
  1. echo "mySeFunc([".$myComboVar2."]);";
  2. with
  3. echo "mySeFunc([{SHOWS: "some show", GENDER: "male", AGE: "19", etc...}]);";
  4.  
And why would this same function that worked before the server crash, suddenly not work any longer?
Member
 
Join Date: May 2007
Posts: 63
#10: Mar 13 '08

re: missing } returned to AJAX


Quote:

Originally Posted by RoninOni

Expand|Select|Wrap|Line Numbers
  1. $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)."\"}";
  2. $count++;
  3.  
  4. echo "javascript:mySeFunc([".$myTempVar."]);";
  5.  
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($myTempArray 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($myTempArray);[/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:
Expand|Select|Wrap|Line Numbers
  1.                  for($i=0;$i<$count;$i++)
  2.                         {
  3.                              if($i<$count-1)
  4.                              {
  5.                              $myTempVar .= $myTempArray[$i].", ";
  6.                              }
  7.                              else
  8.                              {
  9.                              $myTempVar .= $myTempArray[$i];
  10.                              }
  11.                         }
  12.                         echo "javascript:mySeFunc([".$myTempVar."]);";
  13.  
Reply