Ajax不会填充表单

时间:2014-01-26 02:12:33

标签: joomla2.5

嘿,有人可以帮我解决这个问题吗?

我的代码有两个问题:

1 - test.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
  <script>

        var url = "getagentids.php?param=";

        function handleHttpResponse() {
        if (http.readyState == 4) {
         results = http.responseText.split(",");
            document.getElementById('formality').value = results[0];
            document.getElementById('fullname').value = results[1];
            document.getElementById('sex').value = results[2];
            document.getElementById('id').value = results[3];
            document.getElementById('joindate').value = results[4];
            document.getElementById('jobtitle').value = results[5];
            document.getElementById('city').value = results[6];
            document.getElementById('typeofsalary').value = results[7];
            document.getElementById('contract_type').value = results[8];
         }
        }


        function getagentids() {
        var idValue = document.getElementById("email").value;
        var myRandom=parseInt(Math.random()*99999999);  // cache buster
        http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true);
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
        }


        function getHTTPObject() {
        var xmlhttp;

        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
         try {
           xmlhttp = new XMLHttpRequest();
           } catch (e) {
           xmlhttp = false;
           }
          }
          return xmlhttp;
        }


var http = getHTTPObject();

  </script>
</head>

<body>
<form name="schform">
<table bgcolor="#dddddd">
<tbody>
<?php
echo $param;
include '../../../connect.php';

    $db =& JFactory::getDBO();
    $query = "SELECT email FROM dbemployeekpw";
    $db->setQuery($query);
    $result = $db->loadObjectList();
    $email  = $result[0];

echo " <select size='1' name='email' id='email' onChange='getagentids()' required >
<option value=''> Seleccione </option>";
foreach($result as $email)
{
     echo "<option value='".$email->email."'>".$email->email."</option>";
}
echo "</select>"

?>
<tr><td>Formality</td><td><input   id="formality" type="text" name="formality"></td></tr>
<tr><td>Fullname</td><td><input   id="fullname" type="text" name="fullname"></td></tr>
<tr><td>Sex</td><td><input   id="sex" type="text" name="sex"></td></tr>
<tr><td>Id</td><td><input   id="id" type="text" name="id"></td></tr>
<tr><td>Joindate</td><td><input  id="joindate" type="text" name="joindate"></td></tr>
<tr><td>Jobtitle</td><td><input   id="jobtitle" type="text" name="jobtitle"></td></tr>
<tr><td>City</td><td><input   id="city" type="text" name="city"></td></tr>
<tr><td>Typesalary</td><td><input   id="typeofsalary" type="text" name="typeofsalary"></td></tr>
<tr><td>Contract Type</td><td><input   id="contract_type" type="text" name="contract_type"> </td></tr>

<tr><td><input size="60"  type="reset" value="Clear"></td><td></td>
</tr>
</tbody></table>
</form>
</body>
</html>

和.. 2 - getagentids.php

<?php 
//$param = $_GET["param"];
include '../../../connect.php';

    $db =& JFactory::getDBO();
    $query = $db->getQuery(true);
    $query = "SELECT * FROM dbemployeekpw WHERE email = 'camilo.uribe@kantarworldpanel.com'";
    $db->setQuery($query);
    $results = $db->loadObjectList();

foreach ( $results as $result )
{
    $formality = $result->formality;
    $fullname = $result->fullname;
    $sex = $result->sex;
    $id = $result->id;
    $joindate = $result->joindate;
    $jobtitle = $result->jobtitle;
    $city = $result->city;
    $typeofsalary = $result->typeofsalary;
    $contract_type = $result->contract_type;    
    $textout = $formality.",".$fullname.",".$sex.",".$id.",".$joindate.",".$jobtitle.",".$city.",".$typeofsalary.",".$contract_type;
}

echo $textout;                  


?>

但是ajax不起作用,只有在我说出来时才有效:

    $query = "SELECT * FROM dbemployeekpw WHERE email = 'camilo.uribe@kantarworldpanel.com'";
而不是这个:

    $query = "SELECT * FROM dbemployeekpw WHERE email = '".$param."'";

但我需要代码与第二个一起使用:(

任何人都可以帮我解决这个问题吗?

谢谢!

求助(就像魅力一样!):

我改变了这个:

$jinput = JFactory::getApplication()->input;
$param = $jinput->get('param', 'param', 'filter');
而不是这个:

$param = $_GET["param"];

我还在:

  $query = "SELECT * FROM dbemployeekpw WHERE email = '".$param."'";

因为这段代码对我不起作用:

$query->select($db->quoteName('*'))
 ->from($db->quoteName('dbemployeekpw'))
 ->where($db->quoteName('email') . ' = '. $db->quote($param));

非常感谢@lodder

1 个答案:

答案 0 :(得分:0)

在此之前,让我们看看$param变量是否正确并获取值。添加以下哪个表单处理,将显示该值。如果结果为NULL,那么您首先需要确保获得正确的值。如果你得到了正确的值,那就继续阅读。

请注意,我建议您查看以下链接,而不是使用$_GET

http://docs.joomla.org/Retrieving_request_data_using_JInput

现在让我们为您的数据库查询使用最新的编码标准:

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select($db->quoteName('*'))
 ->from($db->quoteName('dbemployeekpw'))
 ->where($db->quoteName('email') . ' = '. $db->quote($param));

$db->setQuery($query);
$results = $db->loadObjectList();

希望这有帮助