Link to home
Create AccountLog in
Avatar of Panos
PanosFlag for Germany

asked on

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

Avatar of Hammo777
Hammo777

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.
Avatar of Panos

ASKER

Hi Hammo777
If i use an empty application.cfm i get an error for the session.captha
Avatar of Panos

ASKER

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)

Avatar of Panos

ASKER

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

SOLUTION
Avatar of Hammo777
Hammo777

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Panos

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.
Avatar of Panos

ASKER

Thank you for your help.
regards
panos