Auto fill form input fields from database using AJAX

谁说胖子不能爱 提交于 2020-04-11 07:39:09

问题


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

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...

img1

img2

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

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!


回答1:


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


来源:https://stackoverflow.com/questions/24516096/auto-fill-form-input-fields-from-database-using-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!