使用AJAX从数据库自动填充表单输入字段

时间:2014-07-01 17:26:43

标签: javascript php mysql ajax joomla3.0

无法让这个工作,并可以使用额外的一双眼睛来找到我做错了什么或可能缺少什么。我使用Joomla 3.3.1的RSForm Pro组件创建了一个表单。表单的目的是允许用户对我们的产品提出保修索赔。如果用户需要在产品上提交重复声明,则会显示一个输入字段,其中包含一个按钮,用于从数据库中检索数据并自动填写用户的所有者信息。对于提交的每个索赔,都会生成“id”。此“id”是用户在需要提交重复声明时应输入以检索数据的数字。我有一个运行onclick的ajax函数,并查找连接到数据库并检索所请求信息的php文件。

这是ajax ......

var ajax = getHTTPObject();

function getHTTPObject()
{
    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        //alert("Your browser does not support XMLHTTP!");
    }
    return xmlhttp;
}

function updateOwnerInfo()
{
    if (ajax)
    {
        var idValue = document.getElementById("owner_id").value;
        if(idValue)
        {
            var url = "/templates/uma-solar/html/com_rsform/getClaimInfo.php";
            var param = "?id=" + escape(idValue);
            ajax.open("GET", url + param, true);
            ajax.onreadystatechange = handleAjax;
            ajax.send(null);
        }
    }
}

function handleAjax()
{
    if (ajax.readyState == 4)
    {
        ownerarr = ajax.responseText.split(",");

        var owner_name = document.getElementById('owner_name');
        var owner_address = document.getElementById('owner_address');
        var owner_city = document.getElementById('owner_city');
        var owner_state = document.getElementById('owner_state');
        var owner_country = document.getElementById('owner_country');
        var owner_county = document.getElementById('owner_county');
        var owner_zip = document.getElementById('owner_zip');
        var owner_phone = document.getElementById('owner_phone');
        var owner_email = document.getElementById('owner_email');

        owner_name.value = ownerarr[0];
        owner_address.value = ownerarr[1];
        owner_city.value = ownerarr[2];
        owner_state.value = ownerarr[3];
        owner_country.value = ownerarr[4];
        owner_county.value = ownerarr[5];
        owner_zip.value = ownerarr[6];
        owner_phone.value = ownerarr[7];
        owner_email.value = ownerarr[8];
    }
}

这是php ...

define( '_JEXEC', 1 );
define('JPATH_BASE', '/var/www/joomla.umasolar.com/');

/* Required Files */
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );

/* To use Joomla's Database Class */
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

/* Create the Application */
$app = JFactory::getApplication('site');
$app->initialise();

//-----process DB query-------
$db = JFactory::getDBO();
$sql='SELECT 
    owner_name, 
    owner_address, 
    owner_city, 
    owner_state, 
    owner_county,
    owner_country,
    owner_zip,
    owner_phone,
    owner_email 
    FROM #__rsform_warranty_claim WHERE _id=mysql_real_escape_string($_GET[
    "owner_id"])';
$db->setQuery($sql);

//----------------------------
$row = $db->loadObjectList();
echo $row['owner_name'] . ", " . $row['owner_address'] . ", " . $row['owner_city'] . ", " . $row['owner_state'] . ", " . $row['owner_country'] . ", " . $row['owner_county'] . ", " . $row['owner_zip'] . ", " . $row['owner_phone'] . ", " . $row['owner_email'];

表单不公开所以这里有一些可能有用的截图...

img1

img2

这里的文字填写输入字段,看起来似乎是404,但在标题中显示的是1064 - 错误:1064 ......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr">

    <head>
        <title>1064 - Error: 1064</title>
        <link rel="stylesheet" type="text/css" href="/templates/uma-solar/html/com_rsform/templates/uma-solar/css/style.css" />
        <link rel="stylesheet" type="text/css" href="/templates/uma-solar/html/com_rsform/templates/uma-solar/bootstrap/css/bootstrap.css" />
        <link rel="stylesheet" type="text/css" href="/templates/uma-solar/html/com_rsform/templates/uma-solar/bootstrap/css/bootstrap-responsive.css" />
        <script type="text/javascript" src="/templates/uma-solar/html/com_rsform/templates/uma-solar/bootstrap/js/bootstrap.js"></script>
    </head>

    <body class="error">
        <center>
            <div class="errorbox">
                <div class="block">
                     <h1>404</h1> 
                     <h3>Page not found</h3>
                </div>
                <p>Sorry! The page you are looking for cannot be found. Please use the provided search box to find what you are looking for

使用此代码在错误日志中生成错误。有一些问题找出PHP代码中所需的Joomla文件的正确路径,但似乎我解决了这个问题。任何和所有的帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

最后按照Joomla关于使用JDatabase选择数据的说明进行操作。我确信这可以使用标准的SQL语句,但Joomla有时会很挑剔,而且更容易遵循他们的规则。 AJAX很好,但这就是我将PHP改为......

define( '_JEXEC', 1 );
define('JPATH_BASE', '../../../../');

//Required Joomla Files
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );

//Connect to Joomla's Database Class
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

//Create the Application
$app = JFactory::getApplication('site');
$app->initialise();
$input = $app->input;
$id = $input->getInt('id');

//Connect to db
$db = JFactory::getDBO();

//Create new query object
$query = $db->getQuery(true);
$query->select($db->quoteName(array('owner_name', 'owner_address', 'owner_city',   'owner_state', 'owner_county', 'owner_country', 'owner_zip', 'owner_phone', 'owner_email')));
$query->from($db->quoteName('#__rsform_warranty_claim'));
$query->where($db->quoteName('_id') . '=' . $db->quote($id));

//Reset the query using our newly populated query object
$db->setQuery($query);

//Get a single record from the DB table
$row = $db->loadAssoc();
echo $row['owner_name'] . ", " . $row['owner_address'] . ", " . $row['owner_city'] . ", " . $row['owner_state'] . ", " . $row['owner_country'] . ", " . $row['owner_county'] . ", " . $row['owner_zip'] . ", " . $row['owner_phone'] . ", " . $row['owner_email'];