missing } returned to AJAX | Member | | Join Date: May 2007
Posts: 63
| |
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: -
$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 :-)
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | 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
| | | 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. -
$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.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | 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
| | | 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.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: missing } returned to AJAX
I'm sure you've tried this, but what about escaping the ":"? ie. "\:"
|  | Newbie | | Join Date: Mar 2008
Posts: 9
| | | re: missing } returned to AJAX - $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($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
|  | Newbie | | Join Date: Mar 2008
Posts: 9
| | | 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
| | | 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. -
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. -
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?
| | Member | | Join Date: May 2007
Posts: 63
| | | re: missing } returned to AJAX Quote:
Originally Posted by RoninOni - $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($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: -
for($i=0;$i<$count;$i++)
-
{
-
if($i<$count-1)
-
{
-
$myTempVar .= $myTempArray[$i].", ";
-
}
-
else
-
{
-
$myTempVar .= $myTempArray[$i];
-
}
-
}
-
echo "javascript:mySeFunc([".$myTempVar."]);";
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|