Forums

Returning a single value back from DB

givsmart 19 Oct, 2012
Hi,

I have been trying to output some info about a city limit based on an entered zipcode using AJAX, and it's just not working at all. I've tried everything, it's either displaying "null" or not displaying anything or just a Fatal Error :?

Please help. Thank you!

Form: http://givsmart.com/index.php?option=com_chronoforms&chronoform=DonationsFormSM-Copy

JavaScript Code

window.addEvent('domready', function()
    {
///        CITY LIMIT          ////
        limitInfo=$('limitInfo');
var url2 = 'index.php?option=com_chronoforms&tmpl=component&chronoform=DonationsFormSM-Copy&event=ajaxCitylimit&format=raw';
        //Ajax CityLimit check
                
        var Zipcode2 = $('Zipcode');
        
        Zipcode2.addEvent('blur', function(e)
        {

            
            var value2 = Zipcode2.value.trim();
            
                var request2 = new Request(
                {
                
                        method: 'post',
                        url: url2,
                        data: { 'Zipcode2' : value2 },
						onRequest: function() { limitInfo.innerHTML="Looking...";
    },
                        onComplete: function (r) {
                            limitInfo.innerHTML="Attention! " +r + "!!!";
                                     
                        }                        
                }).send();
            });    
  });
       


AJAX custom Code Action:
<?php

// get the query info
$Zipcode2;
$Zipcode2 = JRequest::getString('Zipcode2', '', 'post');
$Zipcode2 = trim($Zipcode2);
$db2 =& JFactory::getDBO();
$query2 = "
SELECT `City`,`Zip`
FROM `#__chronoforms_data_CityLimit` 
WHERE `Zip`= ".$db->quote($Zipcode2).";
";
$db2->setQuery($query2);
$data2 = $db2->loadResult;
echo "$data2";
?>
GreyHead 20 Oct, 2012
Hi givsmart,

Checking with Firebug I can see that the parameter is being sent to the Ajax query OK but nothing is coming back. The Response is completely empty.

You can add some debug code there to check in FireBug step buy step if you need to.

Looking at the code this line looks odd and may be throwing an error:
$Zipcode2;
Also you probably don't need both &tmpl=component and &format=raw in the URL, just the format should do what you need (but I don't think this will cause an error).

Bob
givsmart 22 Oct, 2012
Thanks, Bob!

It's working now! I guess there was something wrong with my DB access code. Changing it to the one below fixed it:

$db2 = JFactory::getDBO();
$query2 = "
  SELECT ".$db->nameQuote('City')."
    FROM ".$db->nameQuote('#__chronoforms_data_CityLimit')."
    WHERE ".$db->nameQuote('Zip')." = ".$db->quote($Zipcode2).";
  ";
$db2->setQuery($query);
$result = $db->loadResult();
echo "$result";


Have a good day!
Austre 22 Nov, 2012
I got a problem looks like this one.

Those are my CF Events.
[attachment=1]cf_events_order.JPG[/attachment]

My Custom Code - getSaldoDisponivel
<?php
$year = date('Y');
$month = date('m');
$idAcao =& JRequest::getString('sad_acao', '', 'post');

if($idAcao){
 $conn = mssql_connect("MyServer", "MyUser", "MyPass");
 $bco = mssql_select_db("MyDB", $conn);
 $sql  = "exec MySP '$idAcao', $month, $year";
 $result = mssql_query($sql,$conn);
 
 if(count($result)){
  $row = mssql_fetch_row($result);
  $return = $row[0];
 }else{
  $return = 0;
 }
}else{
 $return = 0;
}

echo $return;
$mainframe->close();
?>


My Load JS

window.addEvent('domready', function() {
  $('sad_acao').addEvent('change', function(){
    var idAcao = $('sad_acao').value;
    getSaldoDisponivel(idAcao);
  });
});

function getSaldoDisponivel(idAcao){
  var formName = "sad";
  var url = "index.php?option=com_chronoforms&chronoform="+formName+"&event=changeAcao&format=raw";
 
  new Request.HTML({
    url: url,
    method: 'post',
    data: { 'sad_acao': idAcao },
    onRequest: function(){
     $('sad_saldodisponivel').set('value', 'Calculando...');
    },
    onComplete: function(r){
     $('sad_saldodisponivel').set('value', r);
    }
  }).send();
}


The $('sad_saldodisponivel') is an Input Text and when CF execute, this element value appears like image below:
[attachment=0]cf_form_view.JPG[/attachment]

My Custom Code - getSaldoDisponivel returns only a number, looks like 1000, 200, 1234.33 and i don't know why i can't get this returned value and set it to the Input Text.
Austre 22 Nov, 2012
It's working now too!
:o

The "r" is an Object Text. To access his "value" we have the Property named data. I tryed "text", "value", "getText" and others, but without references.

$('sad_saldodisponivel').set('value', r[0].data);
This topic is locked and no more replies can be posted.