Ajax function not working

Hi Everyone.

I am trying to use Ajax in SuiteCRM to retrieve a database value and display it in a field, for this I do the following:

  1. I create a js to use Ajax:
$(function getAjax(){
    $('#btn_empresa_labora_c').focus(function(){
        var empresa = $('#empresa_labora_c').val();
        $.ajax({
            type: 'POST',
            url: 'ajax_1.php',
            data: empresa,
            success: function(resp){
                if(resp!="") {
                    $("#last_name").val(resp);
                }
            }
        });
    });
});

In the ajax_1.php file I have the following:

<?php
  function getAjaxResult()
  {
   $GLOBALS['log']->fatal("Funcion --> getAjaxResult --> Entrando");
   echo "Entrando a la funcion";
   $resultadoConsulta = "";
   $con = $this->Conexion();
   if($con)
   {
    $id = $_POST["empresa_labora_c"];
    $GLOBALS['log']->fatal("Funcion --> getAjaxResult --> Conexion establecida: id: $id");
    $sqlType = "SELECT name FROM accounts WHERE id = '".$id."';";
    $resultType = mysqli_query($con, $sqlType);
  
    if(mysqli_num_rows($resultType) != 0)
    {
      while($fila_type = mysqli_fetch_array($resultType))
      {
        $resultadoConsulta = $fila_type['name'];
        $GLOBALS['log']->fatal("Resultado consulta: $resultadoConsulta");
      }
    }
    //Cerramos conexion a BD
    mysqli_close($con);
    return $resultadoConsulta;
   }
   else
   {
    $GLOBALS['log']->fatal("No se logró realizar la conexión a la base de datos.");
    return $resultadoConsulta;
   }
  }
   
  function Conexion()
  {
      $servername = "localhost";
      $username = "xxxxxxxxx";
      $password = "xxxxxxxxx";
      $db = "basedatos";
      
      try
      {
          $conn = mysqli_connect($servername, $username, $password, $db);
          //echo "Connected successfully"; 
      }
      catch(exception $e)
      {
          echo "Connection failed: " . $e->getMessage();
      }
      
      return $conn;
  }
?>

When I debug the js, it goes to

> data: empresa

, I don’t know why it doesn’t invoke ajax_1.php

I appreciate your help.

Hi
I hope it helps you

  1. Trigger Ajax in js file.

Source Code :

$('#btn_empresa_labora_c').focus(function(){
var empresa = $('#empresa_labora_c').val();
$.ajax({
type: 'POST',
url: 'index.php?entryPoint=ajax_1',
data: {empresa : empresa },
success: function(resp){
if(resp!="") {
$("#last_name").val(resp);
}
}
});
});
  1. Register Entrypoint

File Path : custom/Extension/application/Ext/EntryPointRegistry/fetchAccountDetailsEntryPoint.php

Source Code :

$entry_point_registry['ajax_1'] = array(
'file' => 'custom/accountDetails/ajax_1.php',
'auth' => true
);
  1. Create an Entrypoint file which is registered in step 2. It’s used for retrieving the Data from the Database.

File Path : custom/Extension/application/Ext/EntryPointRegistry/fetchAccountDetailsEntryPoint.php

Source Code :

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
 
class ajax_1{
	public function __construct(){
    	$this->getAjaxResult();
	}
 
	public function getAjaxResult() {
   	 //retrieve data of accounts module
    	$id = $_POST['empresa'];
    	$sqlType = "SELECT name FROM accounts WHERE id = '$id'";
    	$resultType = $GLOBALS['db']->fetchOne($sqlType);
    	$name = $resultType['name'];
    	echo $name;
	}//end of function
}//end of class
new ajax_1();
?>
1 Like