AnsweredAssumed Answered

Javascript call php function

Question asked by Dimitrij Dimitrij on Aug 15, 2014
Latest reply on Aug 31, 2017 by Bao Tran Hoang
Hi to all .. 
I have don this part .. I have made the changes  and now I have onchange Datepicer  working..

I have a problem to call php function from the javascript file....  
I need to do , when I change the date , it need to make update in currency table..:)
but I can't  make the needed  changes ..:(
Pls. help some one..
---------------------------------------------------------------------------------------------------
getCurrency.js  :

function getDateCurrency(){
  
   var the_string =document.getElementById('invoice_date').value;

    jQuery.ajax({
        type: "POST",        
        url: 'http://localhost/testDima/modules/AOS_Invoices/Dima1.php',
        //dataType: 'json',
        //data: {functionname: 'getCurr', arguments: [the_string]}, 
        data: { arguments: the_string},
         success:function(data) {
         alert(data); 
         }
    });
}

---------------------------------------------------------------------------------------------------

Dima1.php

<?php

if (isset($_POST)) { 

    $aResult = array();
    
    if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; } 

    if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }

    if( !isset($aResult['error']) ) {

        switch($_POST['functionname']) {
      
            case 'getCurr': 

            $aResult['result'] = GetCurrencyRate($_POST['arguments']);
           // $aResult['result'] = GetCurrencyRate();

            echo ($aResult);
               break;

            default:
               $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
               break;
        }

    }

   echo json_encode($aResult);

} else {

    echo ("Nothing send");
}

   
function GetCurrencyRate($datum){
      
     $db = DBManagerFactory::getInstance();
    $query = "SELECT iso4217,re_rate FROM currency_rates WHERE date_entered= CURDATE()";
        $result = $db->query($query); 
    while ($row = $db->fetchByAssoc($result))
            {
              $db = DBManagerFactory::getInstance();             
              $query = "Update currencies set conversion_rate='".$row['re_rate']."' where iso4217= '" . $row['iso4217'] . "'";
              $db->query($query);
            }
}

?>
--------------------------------------------------------------------------------------------------------------------
Pls. help me whit this one..  
Sugarcrm CE  ver 6.5.* .... 
Thank you  all..

Outcomes