CFv5 ajax request for an event

Austre 26 Mar, 2014
I'm trying get a JSON response from an event by AJAX, but always i got Fail:

My Event Code


$pj =& JRequest::getString('pj', '', 'get');

$conn = mssql_connect("server", "user", "pass");
$bco  = mssql_select_db("base", $conn);
$sql  = "select CodParceiro, CgcCpf, NomeRazaoSocial, NomeAbrevFantasia from Parceiro where CodParceiro = $pj";

$res = mssql_query($sql,$conn);
$row = mssql_fetch_array($res,MSSQL_ASSOC);
$return = json_encode($row);
return $return;



My JavaScript Code

jQuery(document).ready(function($){
	$("#searchPJ").click(function(){
		//searchPJ(jQuery("#pj").val);
		var pj = $('#pj').val();
		searchPJ(pj);
	});
});


function searchPJ(pj){
	jQuery.getJSON(url, {pj:pj}, function(info){
		var cod = info.CodParceiro;
		var cnpj = info.CgcCpf;
		var razao = info.NomeRazaoSocial;
		var fantasia = info.NomeAbrevFantasia;
		alert(cod);
		alert(cnpj);
		alert(razao);
		alert(fantasia);
    })
	.fail(function(e){
	    alert(e.status);
	    alert(e.response);
	    alert(e.responseText);
	    alert(e.statusText);
	});
}


Another JavaScript Code that i tried

jQuery(document).ready(function($){
	$("#searchPJ").click(function(){
		//searchPJ(jQuery("#pj").val);
		var pj = $('#pj').val();
		searchPJ(pj);
	});
});


function searchPJ(pj){
	var url = 'http://intranet.rn.sebrae.com.br/index.php?option=com_chronoforms5&chronoform=consultor&event=searchPJ';
	jQuery.ajax({
		url:url,
		type:'get',
		data:{'pj':pj},
		dataType: 'html',
		success:function(r){
			alert(r);
		},
		error:function(e){
			alert('fail!');
		}
	});
}
Max_admin 26 Mar, 2014
Answer
This code from the latest version should help, just add it in the click event code you have:


$.ajax({
	"type" : "GET",
	"url" : "FORM_LINK_HERE&event=your_event&tvout=ajax",
	"data" : {"field_name":"field_value"},
	"success" : function(res){
		$.each($.parseJSON(res), function(id, val){
			console.log(id);
			console.log(val);
		});
	},
});


Your ajax form event should have a custom code action with a code like this:

<?php
$data = array(1,2,3,4);
echo json_encode($data);


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Austre 26 Mar, 2014
So nice Max!

That code you post works. Now i'll make my changes.

My Event Code

<?php

$pj =& JRequest::getString('pj', '', 'get');

$conn = mssql_connect("server", "user", "pass");
$bco  = mssql_select_db("base", $conn);
$sql  = "select CodParceiro, CgcCpf, NomeRazaoSocial, NomeAbrevFantasia from Parceiro where CodParceiro = $pj";

$res = mssql_query($sql,$conn);
$row = mssql_fetch_array($res,MSSQL_ASSOC);
echo json_encode($row);

?>


My JavaScript Code

jQuery(document).ready(function($){
	$("#searchPJ").click(function(){
		//searchPJ(jQuery("#pj").val);
		var pj = $('#pj').val();
		searchPJ(pj);
	});
});


function searchPJ(pj){
	var url = 'http://mysite/index.php?option=com_chronoforms5&chronoform=consultor&event=searchPJ&tvout=ajax';
	jQuery.ajax({
		url:url,
		type:'get',
		data:{'pj':pj},
		//dataType: 'json',
		success:function(res){
			jQuery.each(jQuery.parseJSON(res), function(id, val){
   				console.log(id);
   				console.log(val);
			});
		},
		error:function(e){
		    alert(e.status);
		    alert(e.response);
		    alert(e.responseText);
		    alert(e.statusText);
		}
	});
}
This topic is locked and no more replies can be posted.