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:
- 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.