如何使用AJAX / Javascript / PHP

时间:2015-09-04 21:22:44

标签: javascript php jquery ajax jsp

我尝试使用AJAX / Javascript / PHP填充JSP表单。我想让它工作,以便当用户输入一个ID时,如果ID与数据库中的ID匹配,则ajax会填充其他3个字段,但是我得到的很长时间是"未定义"每当我在表单中键入ID(documento)时都会出现字段。我已经检查了php脚本的输出,并且格式正确(即XXXXX,XXXXX,XXXXX,XXXX),所以我认为我的问题在于我的Ajax功能:

function CreateXmlHttpObject() { //function to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();//creates a new ajax object
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");//this is for IE browser
        }
        catch(e){
            try{
            req = new ActiveXObject("Msxml2.XMLHTTP");//this is for IE browser
            }
            catch(e1){
                xmlhttp=false;//error creating object
            }
        }
    }

    return xmlhttp;
} 
function ajaxPersonas(documento)
{
    var http = CreateXmlHttpObject(); // function to get xmlhttp object
    var strURL = "http://localhost/formpersonas.php?documento="+documento;
    if (http){
        http.onreadystatechange = function(){
            if (http.readyState == 4) { //data is retrieved from server
                if (http.status == 200) { // which reprents ok status                    
                    var results = http.responseText.split(",");
                    $('#nombrep').html(results[0]);
                    $('#apellido').html(results[1]);
                    $('#nacimiento').html(results[2]);
                    $('#documento').html(results[3]);
                }
                else
                { 
                    alert("Hubo un problema con el XMLHTTP:\n");
                }
            }            
        }        
        http.open("GET", strURL, true); //open url using get method
        http.send(null);//send the results
    }
}

我的PHP脚本。我还不太确定如何发送' $ param' JSP文件中的参数:

<?php
    header('Access-Control-Allow-Origin: *');
$con = pg_connect("host=localhost port=5432 dbname=sisben user=postgres password=posgres");
if (!$con) {
    die('Could not connect: ' . pg_error());
}

if (strlen($param) > 0) {
    $result = pg_query("SELECT primernombre, primerapellido, fechanacimiento, numeroidentificacion FROM tpersonas 
     WHERE numeroidentificacion LIKE '%$param%'");
    if (pg_num_rows($result) == 1) {
        while ($myrow = pg_fetch_array($result)) {
            $nombrep = $myrow["primernombre"];
            $apellido = $myrow["primerapellido"];
            $nacimiento = $myrow["fechanacimiento"];
            $documento = $myrow["numeroidentificacion"];
            $textout .= $nombrep . "," . $apellido . "," . $nacimiento . "," . $documento;
        }
    } else {
        $textout = " , , ," . $param;
    }
}
echo $textout;
pg_close($con);
?>

最后是我的JSP表单。注意我使用每个文本输入的id字段来传递相应的值:

<form name="formpersonas">
<table>
<tr>
  <td>Documento:</td>
  <td><input id="documento" type="text" onKeyUp="ajaxPersonas();"
        name="idnum"></td>
</tr>
<tr>
<td>Nombre:</td>
  <td><input id="nombrep" type="text"
        name="name"></td>
</tr>
<tr>
<td>Apellido:</td>
  <td><input id="apellido" type="text"
        name="surname"></td>
</tr>
<tr>
<td>Fecha de Nacimiento:</td>
  <td><input id="nacimiento" type="text"
        name="dob"></td>
</tr>
<tr>
  <td><input type="reset" value="Clear"></td>
  <td></td>
</tr>
</table>
</form>

原谅我任何明显的错误,我现在已经学习了几个星期的计划;)

0 个答案:

没有答案