Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:08:15+00:00 2026-06-12T17:08:15+00:00

I’m trying to set a function that makes an Ajax call, but I must

  • 0

I’m trying to set a function that makes an Ajax call, but I must confess I can’t understand what’s happening when I set the onclick event. I’m trying to make an Ajax call to get fields from a database and display them to the user. The idea is that the users type a Last Name in an input field, and then, depending on the link they click on, the function I’m trying to build displays the results of a query to the database. I handle the query with a switch statement in PHP on the script that the Ajax function calls, but I can’t set the variable that handles this behavior on the Javascript code.

I’ve got the Javascript code like this:

var ajaxRequest = ajaxFunction();
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
} return ajaxRequest;
} 

function process() {
id="default";
profesional = document.getElementById('profesional');
dotal = document.getElementById('dot'); 
proyecta = document.getElementById('proy');
proyecta_A = document.getElementById('proy_af');
gastosM = document.getElementById('gastos_med');
autos = document.getElementById('aut');
polifam = document.getElementById('polif');

profesional.onclick=function() {
id="prof";
return id;  
}
dotal.onclick=function() {
id="dotal";
return id;  
}
proyecta.onclick=function() {
id="proyecta";  
return id;
}
proyecta_A.onclick=function() {
id="proyAf";
return id;  
}
gastosM.onclick=function() {
id="gastos_medicos";
return id;  
}
autos.onclick=function() {
id="autos"; 
return id;
}
polifam.onclick=function() {
id="polifam";
return id;  
}   
if(ajaxRequest.readyState==4 || ajaxRequest.readyState==0) {apellido =     encodeURIComponent(document.getElementById('query').value)
if (apellido != undefined) {
ajaxRequest.open("GET", 'adminsrc.php?buscarregistro=' +apellido +'&id='+id, true)
ajaxRequest.send("");   
ajaxRequest.onreadystatechange=handleServerResponse;
}
}
else setTimeout('process()',1000);
}

function handleServerResponse(){
{
if (ajaxRequest.readyState==4) {

if(ajaxRequest.status==200) {   
respuesta=ajaxRequest.responseXML;
respuestadoc = respuesta.documentElement;
if (apellido != undefined) {
ident = respuesta.getElementsByTagName('identidad')[0];
result = ident.firstChild;
refrescar = respuesta.getElementsByTagName('identidad')[1];
actualizar = refrescar.firstChild;
if(result!=undefined) {
resultado = result.data;
document.getElementById('profesreg').setAttribute('value', resultado);
document.getElementById('dotalreg').setAttribute('value', resultado);
document.getElementById('gmmreg').setAttribute('value', resultado);
document.getElementById('proyectareg').setAttribute('value', resultado);
document.getElementById('proyecta2reg').setAttribute('value', resultado);
document.getElementById('autosreg').setAttribute('value', resultado);
document.getElementById('polifreg').setAttribute('value', resultado);
}
if(actualizar!=undefined) {
actualiza = actualizar.data;
document.getElementById('actual_1').setAttribute('value', actualiza);
document.getElementById('actual_2').setAttribute('value', actualiza);
document.getElementById('actual_3').setAttribute('value', actualiza);
document.getElementById('actual_4').setAttribute('value', actualiza);
document.getElementById('actual_5').setAttribute('value', actualiza);
document.getElementById('actual_6').setAttribute('value', actualiza);
document.getElementById('actual_7').setAttribute('value', actualiza);
}
msje = respuestadoc.firstChild.childNodes[0]; 
if (msje !=undefined) {
mess = msje.data;
document.getElementById("res").innerHTML = "<h4>"+ mess +"</h4>";
} 
     }
    setTimeout('process()',1000);
    }
    else alert ('hubo un problema al conectarse con el servidor: ' +    ajaxRequest.statusText);
}
}
} 

//-->

Where the last part is just to set the value of hidden fields in the form so that PHP recognizes those values (they are set to update some fields in the database).
But I must say I’m pretty stuck with the javascript (I’ve never been good at it).
Any suggestions, please?

@Mate The xml file is built like so:

<identificacion>
<response>
Se encontró al asegurado con nombre Mara Fernanda Bravo Palomino, número de póliza        27608165, la cual vence el 2003-04-05 y cuyos beneficiarios contratados son Ana Camila Lpez       Bravo
</response>
<identidad>Bravo Palomino</identidad>
<identidad>si</identidad>
</identificacion>

And once (a few weeks ago) I already had it working with a function like so:

 function process() {
    if(ajaxRequest.readyState==4 || ajaxRequest.readyState==0) {apellido =        encodeURIComponent(document.getElementById('query').value)
    if (apellido != undefined) {
    ajaxRequest.open("GET", 'adminsrc.php?buscarregistro=' +apellido, true)
    ajaxRequest.send("");   
    ajaxRequest.onreadystatechange=handleServerResponse;
    }
    }
    else setTimeout('process()',1000);
    }

and the function handleServerResponse above; but as you can see I can only extract the text nodes from the xml with that complicated iteration. Surely there’s an easier way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:08:16+00:00Added an answer on June 12, 2026 at 5:08 pm

    1) Download jQuery Library and link to te correct path !!

    2) I’m a bit blind with html and xml response, but I think you can solve well.

            <script src="jquery-1.8.2.min.js" type="text/javascript"></script>
            <script type="text/javascript">
    
                function AjaxCall(id) {
    
                    //Obtengo el appelido.
                    var apellido = $('#query').val();
    
                    //Armo la url con el querystring
                    var urlPhp = 'adminsrc.php?buscarregistro=' + apellido + '&id=' + id;
    
                    $.ajax({
                        type: "GET",
                        url: urlPhp,
                        contentType: "charset=utf-8",
                        dataType: "xml",
                        success: function (xml) {
                            successProcess(xml);
                        },
                        error: function (msg) {
                            alert("error");
                        }
                    });
                }
    
                //Proceso el xml si estuvo Ok.
                function successProcess(xml) {
                    //Revisar bien el XML devuelto
    
                    var identidades = $(xml).find("identidad");
    
                    result = identidades[0];
                    refresh = identidades[1];
                    message = $(xml).find('mensaje');
    
                    SetResult(result);
                    SetActualizar(refresh);
                    SetMsj(message);
    
                }
    
                //Seteo el resultado en los valores de los controles.
                function SetResult(resultado) {
    
                    if (result != undefined) {
                        $('#profesreg').val(resultado);
                        $('#dotalreg').val(resultado);
                        $('#gmmreg').val(resultado);
                        $('#proyectareg').val(resultado);
                        $('#proyecta2reg').val(resultado);
                        $('#autosreg').val(resultado);
                        $('#polifreg').val(resultado);
                    }
                }
    
                //Seteo los dato actualizados
                function SetActualizar(actualiza) {
                    if (actualiza != undefined) {
                        $('#actual_1').val(actualiza);
                        $('#actual_2').val(actualiza);
                        $('#actual_3').val(actualiza);
                        $('#actual_4').val(actualiza);
                        $('#actual_5').val(actualiza);
                        $('#actual_6').val(actualiza);
                        $('#actual_7').val(actualiza);
                    }
                }
    
                //Muestro el mensaje
                function SetMsj(msje) {
    
                    if (msje != undefined) {
                        $('#res').html("<h4>" + msje + "</h4>");
                    }
                }
    
    
                //Este evento se dispara cuando carga el documento.
                $(document).ready(function () {
    
                    $('#profesional').click(function () { AjaxCall('prof') });
                    $('#dot').click(function () { AjaxCall('dotal') });
                    $('#proy').click(function () { AjaxCall('proyecta') });
                    $('#proy_af').click(function () { AjaxCall('proyAf') });
                    $('#gastos_med').click(function () { AjaxCall('gastos_medicos') });
                    $('#aut').click(function () { AjaxCall('autos') });
                    $('#polif').click(function () { AjaxCall('polifam') });
    
                });
    
    
            </script>
        </head>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.