从数据库中使用AJAX自动填写表单的输入域

问题描述:

不能得到这个工作,可以使用一个额外的一双眼睛来寻找我做错了还是什么可能丢失。我创建使用RSForm临组件为Joomla 3.3.1的形式。表格的目的是允许用户提交对我们的产品的保修索赔。如果用户需要提交的重复索赔的一个产品,那么输入字段显示一个按钮,从数据库中检索和自动填充数据,为用户所有者的信息。对于每一个索赔提交的身份证生成。这个身份证是用户应该进入,如果需要提交的重复索赔检索数据的数量。我有运行的onclick并查找连接到数据库和检索请求信息的PHP文件的AJAX功能。

Can't get this to work and could use an extra pair of eyes to find what I'm doing wrong or what might be missing. I created a form using the RSForm Pro component for Joomla 3.3.1. The purpose of the form is to allow a user to file warranty claims on our products. If a user needs to file a repeat claim on a product then an input field is displayed with a button to retrieve data from the database and auto fill the owner's info for the user. For every claim submitted an "id" is generated. This "id" is the number the user should be entering to retrieve data if needing to submit a repeat claim. I have an ajax function that runs onclick and looks for a php file that connects to the database and retrieves the requested info.

下面是阿贾克斯...

Here is the 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 ...

Here is the 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'];

该表单所以这里不公开是一对情侣截图,可能是有用的...

The form is not public so here are a couple screenshots that might be useful...

这里是填补这只是似乎是一个404的输入字段中的文本,但在标题它显示1064 - 错误:1064 ...

And here is the text that is filling in the input fields which just appears to be a 404, but in the title it shows 1064 - Error: 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

有没有错误产生与此code中的错误日志。有一些问题搞清楚正确的路径在PHP code所需的Joomla文件,但它似乎我解决这个问题。任何和所有帮助将是很大的AP preciated!

There are no errors generating in the error log with this code. Had some issues figuring out the correct path to the required Joomla files in the PHP code, but it appears I fixed that issue. Any and all help would be greatly appreciated!

终于得到了以下关于使用JDatabase选择数据的Joomla的指示后,工作。我敢肯定,这可以使用标准的SQL语句工作,但的Joomla可以挑剔的,有时它只是更容易遵循他们的规则。阿贾克斯是好的,但这里是我改变了PHP来...

Finally got it working after following Joomla's instructions on selecting data using JDatabase. I'm sure this can work using standard SQL statements, but Joomla can be picky sometimes and it's just easier to follow their rules. The AJAX was fine, but here's what I changed the PHP to...

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'];