Coldfusion and JSON problem

Hello experts.
I'm trying to use the jquery validation plug-in :http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
There is a paragraph for "Inline AJAX validation"
The test files include a php page that is returning true or false in json format.
I 'm testing this with serialize function but it is not working.I get an error in firebug:
syntax error
            data = eval( "("+data+")"); // GET JSON DATA FROM PHP AND PARSE IT

(the error is on line 311 in the jquery.ValidationEngine.js file
The php file is checking three values:
validateValue
validateId
validateError
And returns an array.
Any help to get this work?
below is the php version and my cfm page
      
<?php

/* RECEIVE VALUE */
$validateValue=$_POST['validateValue'];
$validateId=$_POST['validateId'];
$validateError=$_POST['validateError'];







	/* RETURN VALUE */
	$arrayToJs = array();
	$arrayToJs[0] = $validateId;
	$arrayToJs[1] = $validateError;

if($validateValue =="karnius"){		// validate??
	$arrayToJs[2] = "true";			// RETURN TRUE
	echo '{"jsonValidateReturn":'.json_encode($arrayToJs).'}';			// RETURN ARRAY WITH success
}else{
	for($x=0;$x<1000000;$x++){
		if($x == 990000){
			$arrayToJs[2] = "false";
			echo '{"jsonValidateReturn":'.json_encode($arrayToJs).'}';		// RETURN ARRAY WITH ERROR
		}
	}
	
}

?>


my cfm page
<cfset validateValue = "#Form.validateValue#">
<cfset validateId = "#Form.validateId#">
<cfset validateError= "#Form.validateError#">

<cfset arrayToJs=ArrayNew(1)>
<cfset arrayToJs[1]="validateId">
<cfset arrayToJs[2]="validateError">
<cfif session.captchaText EQ validateValue >
  <cfset arrayToJs[3] = 'true'>
<cfelseif session.captchaText NEQ  validateValue >
  <cfset arrayToJs[3] = 'false'>
</cfif>


<!--- The output is missing --->

Open in new window

LVL 2
PanosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hammo777Commented:
I think you would need to add:
<cfoutput>#serializeJson(arraytoJs)#</cfoutput> to your .cfm file.  Also remember that if your .cfm file triggers an application.cfm file then any content generated by the application.cfm file will be included and cause it to bomb.  To get around this you could put it in a subdirectory with an empty application.cfm file.
PanosAuthor Commented:
Hi Hammo777
If i use an empty application.cfm i get an error for the session.captha
PanosAuthor Commented:
I did change the post to "Get" in the plug in to check what is sending and what is returning.
That is what is be sended for value test:
mydomain/Includes/checkcaptcha/checkcaptcha_json.cfm?validateValue=test&validateId=captchaTextField&validateError=ajaxCaptcha&extraData=

And putting that in the url i get :
["validateId","validateError",false]

the firebug error:
data.jsonValidateReturn is undefined
  ] ajaxisError = data.jsonValidateReturn[2];

(i did change the Form to Url in the cfm page)

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

PanosAuthor Commented:
A few steps forward:
The output of the php page is like this:
{"jsonValidateReturn":[null,null,"false"]}

I did change the <cfset 's to:
<cfset arrayToJs=ArrayNew(1)>
<cfset arrayToJs[1]= '#validateId#'>
<cfset arrayToJs[2]= '#validateError#'>
<cfif session.captchaText EQ validateValue >
  <cfset arrayToJs[3] = 'true'>
<cfelseif session.captchaText NEQ  validateValue >
  <cfset arrayToJs[3] = 'false'>

and the output to:
{"jsonValidateReturn":<cfoutput>#serializeJson(arraytoJs)#</cfoutput>}

I have no errors now but the returning messages are wrong.
I get allways the alertTextOk message

Hammo777Commented:
I am no PHP expert but I believe the PHP is returning a structure with one key, jsonValidateReturn, that contains an array.  Try
<cfsetting showdebugoutput="false"><!--- turn off debug output --->
<cfset tmp=structNew()>
<cfset tmp["jsonValidateReturn"]=arrayToJs>
<cfoutput>#serializeJson(tmp)#</cfoutput>

As for the session stuff, you can use your existing application.cfm (which sets up the session) as long as it doesn't OUTPUT anything.  Any generated output will screw up the JSON.  The other option would be to duplicate the <cfapplicaiton> tag and any session logic in the new applicaiton.cfm.  Also make sure you turn debuggin off in your .cfm with


Your page should output something like:
{"jsonValidateReturn":["validateId","validateError",true]}

In fact you should be able to replace everything in your page with the above line to test it.
Hammo777Commented:
Looks like you posted while I was writing my post.  I had the output wrong it should be:
{"jsonValidateReturn":["validateId","validateError","true"]} with quotes around the true like the PHP output.  It looks like you've about got it.  My code and yours should output the same thing.  Try pasting the PHP response into a blank .cfm file and see how it acts.  Using firebug you should be able to see the response from the call to your .cfm file and you can check and see if any additional text or whitespace is being sent back.
PanosAuthor Commented:
Hi again.
The problem is to get quotes around the false and true value as you have noticed.That's why it does not work.
Both codes are returning output like:
{"jsonValidateReturn":["captchaTextField","ajaxCaptcha",false]}
i need:
{"jsonValidateReturn":["captchaTextField","ajaxCaptcha","false"]}
Any idea to solve this?

The idea using a blank cfm page is returning this error?
syntax error
      data = eval( "("+data+")"); // GET JSON DATA FROM PHP AND PARSE IT
PanosAuthor Commented:
I have a solution but i don't know if it is the right way.
Because i know the output form i did this:
<cfset validateValue = '#Form.validateValue#'>
<cfset validateId = '#Form.validateId#'>
<cfset validateError= '#Form.validateError#'>
<cfif session.captchaText EQ validateValue >
  <cfset pass = 'true'>
<cfelseif session.captchaText NEQ  validateValue >
  <cfset pass = 'false'>
</cfif>
{"jsonValidateReturn":[<cfoutput>"#validateId#","#validateError#","#pass#"</cfoutput>]}

this is working.
What do you think?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hammo777Commented:
If it works it's the right way!  From what you said it seems like the serialzieJSON function is treating the true like a boolean value even when you put quotes around it.  It has other quirks too. Congrats.
PanosAuthor Commented:
Thank you for your help.
regards
panos
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.