How to autopupulate data by search a field?

shirzuis 14 Dec, 2011
Hi there,

I am creating an entry form. Where users can add their entries.
TO make it easir/quicker for users to fill the long form, therefore, i want to add a FIELD called membershipID, which is already saved in a table somewhere with data.
When a user enter a MembershipID and click search, then it should automatically fills some of the fields, from the table other than it is going to save the new form.

I found this code from somewhere but i can't fiqure it how can i use it?
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT *
    FROM ".$db->nameQuote('#__chronoforms_customers123')."
    WHERE ".$db->nameQuote('cus_mbsn')." = ".$db->quote($some_value).";
  ";
$db->setQuery($query);
$row = $db->loadRow();
?>


Can someone please help me.
Many thanks in advance
GreyHead 15 Dec, 2011
Hi shirzuis ,

In Joomla! the 'usual' way of handling this is to have them login and then look up any saved data from their User ID.

If they aren't logged in and you have a separate Membership number then I'd probably crete two forms linked together. In the first one you just get the Membership number. When it's submitted you check it for validity and then user the Profile Page plug-in (n CFv3) to load the matching data.

Bob
shirzuis 16 Dec, 2011
Hi Bob,
Yes i have created two tables and two forms. One form for agents to add customers and 2nd form for agent to retrive customer basic info and add transaction to it and submit.
Just to mention that the customers information is held in a different table rather then the joomla's default table. becuase one user(agent) can have their own members.

here is a snap of the work i want.
How to autopupulate data by search a field? image 1
GreyHead 16 Dec, 2011
Hi shirzuis,

In that case I'd use a two stage form. In the first form put a drop-down list of all of the current user's members for them to select from. When that submits use the member id from the drop-down to populate the second stage form.

Bob
shirzuis 17 Dec, 2011
Hi Bob,

I have managed to get it work directly by creating a new php file, but couldn't able to integrate it with Joomla & Cfv3!

here is the php file code: (config.php)
<?
$dbhost = 'localhost';
$dbuser = 'xxxx';
$dbpass = 'xxxx';
$dbname = 'xxxx';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

$return_arr = array();

/* If connection to database, run sql statement. */
if ($conn)
{
	$fetch = mysql_query("SELECT * FROM jos_chronoforms_customers where cus_mbsn like '%" . mysql_real_escape_string($_GET['term']) . "%'"); 

	/* Retrieve and store in array the results of the query.*/
	while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
		$row_array['id'] = $row['cf_id'];
		$row_array['value'] = $row['cus_mbsn'];
		$row_array['cus_name'] = $row['cus_name'];
		$row_array['cus_surname'] = $row['cus_surname'];
		$row_array['cus_dob'] = $row['cus_dob'];

        array_push($return_arr,$row_array);
    }
}

/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

?>



Here is the form page file (page1.php):
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>jQuery UI Autocomplete - PHP Example</title>
		<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>

<script type="text/javascript">
$(function() {

            $('#abbrev').val("");

            $("#cus_mbsn").autocomplete({
                source: "states.php",
                minLength: 2,
                select: function(event, ui) {
                    $('#cf_id').val(ui.item.id);
                    $('#cus_name').val(ui.item.cus_name);
					$('#cus_dob').val(ui.item.cus_dob);
					$('#cus_surname').val(ui.item.cus_surname);
                }
            });

        });
</script>
	</head>
<body>

<div class="container">
<form action="<?php echo $PHP_SELF;?>"  method="post">
<fieldset>
<legend>jQuery UI Autocomplete Example - PHP Backend</legend>

<p class="ui-widget">

<label for="cus_mbsn">cus_mbsn (abbreviation in separate field): </label>

<input type="text" id="cus_mbsn"  name="cus_mbsn" /> <br>
<input readonly="readonly" type="text" id="cus_name" name="cus_name" maxlength="10" size="10"/><br>
<input readonly="readonly" type="text" id="cus_dob" name="cus_dob" maxlength="10" size="10"/><br>
<input readonly="readonly" type="text" id="cus_surname" name="cus_surname" maxlength="10" size="10"/></p>

<input type="hidden" id="cf_id" name="cf_id" />


<p><input type="submit" name="submitBtn" value="Submit" /></p>

</fieldset>
</form>




</div>
</body>
</html>



So i only need it to be working with CFv3.
Can you please help me with integrating it in to Cfv3?
GreyHead 18 Dec, 2011
Hi shirzuis,

That looks like something very different. It's a jQuery autocompleter. The standard JavaScript framework for Joomla is MooTools so you'd be better using the MooTools AutoCompleter*.

But the method I described used a second form instead of an AutoCompleter.

Bob

* You can use JQuery with Joomla but it can cause major problems unless you know exactly what you are doing.
shirzuis 18 Dec, 2011
Hi Bob,

yes Bob you are right, it is kind of different from the topic. but i found it useful, because it has more features.

I couldn't find something like this on Mootools.

1. I have tried to convert the PHP while loop in order to use it in Cfv3, but couldn't succeeded, sorry i am not that good with converting it.
if you help me to convert the php in order to use in the form, it would be great πŸ™‚
-----------------------------------
2. I like your suggestion too but i don't want to use a drop down list, rather i would like to use an input box, so the user calls a unique id to get the data out of the table and pass it to second form and then fill the rest of it and submit.

can you help me how can i do this?
shirzuis 19 Dec, 2011
Hi Bob,

Thanks for the examples links you provided. I like the second example much more. I tried it, but it didn't work for me.
I think i am missing something or calling the wrong files.

can you please provide me the javascript files or if possible the full-package?
many thanks BOB
GreyHead 20 Dec, 2011
Hi shirzuis,

All the JavaScript files are listed in the example I linked to - you can download them from digitarald's site.

Bob
shirzuis 21 Dec, 2011
Hi Bob,

I tried to get it work in every possible ways, but unfortunately it didn't work😟
GreyHead 22 Dec, 2011
Hi shirzuis,

I'm sure that it works, you need to read the documents and go through step by step carefully.

Bob
This topic is locked and no more replies can be posted.