Forums

add value to textbox based on selected data from DB

jmarian1 09 Feb, 2011
Hi,

I am able to pull the data from DB and show as a drop down menu with column name "name" in chronoform. However I need to use a textbox to add "info" based on the selected drop down menu "name". But somehow my code doesn't work and just give me a blank white page if I add the hidden textbox. Here is my code:

<select class="cf_inputbox" id="designation_list1" size="1" title=""  name="designation_list1" style="width:500px;">
      <option value="">Select agency</option>
      <?php
    $db =& JFactory::getDBO();
    $query = "
      SELECT num, name, impact
        FROM #__agency ORDER BY name ASC ";
    $db->setQuery($query);
    $rows = $db->loadObjectList();



foreach ($rows as $row) {
echo "<option value='".$row->num."'>".$row->name."</option>";

}
    ?>
    </select> //the above code works perfectly

//using the below code to show data based from selection above doesn't work
    <?php 
	$db =& JFactory::getDBO();
    $query = "
      SELECT *
        FROM #__agency WHERE name=1 AND impact=1";
    $db->setQuery($query);
    $options = $db->loadAssocList();
	?><input type="text" name="des11" id="des11" value="<?php echo $options->impact; ?>" />


Please advice.
GreyHead 10 Feb, 2011
Hi jmarian1,

Your PHP code will be executed **before** the form is shown in the browser. So the PHP cannot know anything about what is selected in the drop-down.

Also you are adding $options->impact as the value of the input but I'm pretty sure that $options is an array, not an object so the page is probably breaking because there is a PHP error. If you set Error Reporting to Maximum in the Site Global Configuration you should see a more helpful error message.

Bob
jmarian1 11 Feb, 2011
Hi Bob,

I redo the code and find out that AJAX can do the work for me without reloading the page. However, I am not familiar with AJAX and I know only a little in javascript so I find this code helpful. I also set the error reporting to maximum and didn't get any report with below code. My HTML code below and <select> code is working great

<select class="cf_inputbox" id="designation_list1" size="1" title=""  name="designation_list1" style="width:500px;">
      <option value="">Select agency</option>
      <?php
    $db =& JFactory::getDBO();
    $query = "
      SELECT num, name, impact
        FROM #__agency ORDER BY name ASC ";
    $db->setQuery($query);
    $rows = $db->loadObjectList();

foreach ($rows as $row) {
echo "<option value='".$row->num."'>".$row->name."</option>";

}
    ?>
//the code above is working

//the code below is not showing anything
<div id="txtHint"><b>Impact Area.</b></div>

//below is the AJAX function code I copied which
//I think is helpful but no lack on showing data
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.php?option=com_chronocontact&chronoformname=oldversion&task=extra&extraid=1="+str,true);
xmlhttp.send();
} 


//below is the php code written in extra code 1 of the chronoform/form code
<?php
$designation_list1 = JRequest::getString('designation_list1', '', 'post');

$designation_list1=$_GET["designation_list1"];

 $db =& JFactory::getDBO();
    $query = "
      SELECT *
        FROM #__impact WHERE desnum='".$designation_list."'"; 
    $db->setQuery($query);
    $result = $db->loadResult();
echo "<table border='1'>
<tr>
<th>Impact Areas</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['impact_areas'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>

I have multiple drop down list which is calling from the same database and I wanted to do the same as well with the rest. I hope you can help me. Thanks.

Thanks.
GreyHead 12 Feb, 2011
Hi jmarian1,

Your code has a few problems - mainly I think that the AJAX Url needs to have &tmpl=component to avoid returning a complete Joomla! page.

There's a simple example of usign Ajax with ChronoForms here - it's an extract from The ChronoForms Book.

There's a more complex example creating a double-drop-down that you can buy for $3 here.

Bob
jmarian1 25 Feb, 2011
Hi Bob,

Thanks for the double drop down menu tutorial. It's pretty neat for a begginer like me however I can't figure out what's going on in the code. I can pull the data from database, no doubt about that but when I look the data from the database based on information selected in the first drop down menu, it can't find what it is looking for in the second drop down menu. I know I made mistake somewhere in the javascript or in the extra code. Hope you can help me fix it. Thanks in advance.

My database table includes filename: Designation_Number, Designation_Name, Impact_Areas

Here is my html form code:

 if ( !$mainframe->isSite()) {return;}
	  $db =& JFactory::getDBO();
    $query = "
      SELECT DISTINCT Designation_Number, Designation_Name FROM #__agencyname ORDER BY Designation_Name ASC ";
    $db->setQuery($query);
    $rows = $db->loadObjectList();
<select class="cf_inputbox" id="designation_list7" size="1" title=""  name="designation_list7" style="width:500px;">
      <option value="">Select agency</option>
     <?php 
	foreach ($rows as $row) {
echo "<option value='".$row->Designation_Number."'>".$row->Designation_Name."</option>";

}
    ?>
  </select>


Here is the javascript script code:
window.addEvent('domready', function(){
	$('designation_list1').addEvent('change', function() {
		var a = $('designation_list1').value;
		if (a != null && a != '') {
			getCounty(a);
		} else {
			$('ajax_getcounty').setHTML("<span id='ajax_getcounty'>Please select a state</span>")
		}
													   });
									 });

function getCounty(a){
	var url = "index.php?option=com_chronocontact&chronoformname=oldversion&task=extra&format=raw";
	new Ajax(url, {
			 method: 'get',
			 onRequest: function() {
				 $('progress_getcounty').setStyle('visibility', 'visible');
			 },
			 onComplete: function() {
				 $('progress_getcounty').setStyle('visibility', 'hidden');
			 },
			 update: $('ajax_getcounty'),
			 rows: 'desnum='+a
			 }).request();
};
			



Below is the Extra code also:
<?php
$designation_list1 =& JRequest::getString('desnum', '', 'get');

if ($designation_list1) {
 $db =& JFactory::getDBO();
    $query = "
      SELECT DISTINCT Impact_Areas AS id, Impact_Areas
        FROM #__agencyname WHERE Designation_Number = $designation_list1 ORDER BY Impact_Areas;";
    $db->setQuery($query);
    $rows = $db->loadObjectList();

    if (count($rows)){
       $m = new stdClass();
       $m->id=0;
	   $m->Impact_Areas = '==?==';
	   $rows = array_merge(array($m), $rows);
	   $return = JHTML::_('select.genericlist', $rows, 'Impact_Areas', 'class="inputbox required select" size="1" ', 'id', 'Impact_Areas', 0);
	   } else {
	   $return = "Sorry, we couldn't find that agency";
	   }
	  } else {
	  	$return = "Sorry, we couldn't find an impact area";
		}
		echo $return;
		?>
GreyHead 26 Feb, 2011
Hi jmarian1,

In the Form HTML you are missing a ?> tag after
$rows = $db->loadObjectList();


The Form HTML select tag has an id of designation_list7 but the JavaScript refers to designation_list1

In the Form JavaScript you have rows: 'desnum='+a while in my document I have data: 'desnum='+a

I'm not sure if that will fix the problem but it should take you in the right direction.

I use the Chrome Developer Tools (or FireBug in FireFox) to let me see the nuts and bolts of the Ajax transaction so that I can debug code like this.

Bob
jmarian1 28 Feb, 2011
Thanks a lot for your help Bob. I guess the code doesn't like using my $rows but instead stick to the $data. It is working now and I am almost there but I have more question. I apologize for so many question.

How can you make the second drop down invisible or hidden? I tried to include visibility:hidden but no luck.

Also, is there an easier way how to create a multiple drop down list with the same information in the first drop down and five drop down list in the second? Or do I have to create each first and second drop down list? Please advice.

Thanks again and hope to hear from you soon.
jmarian1 18 Apr, 2011
Hi. I am back again to check if someone can have answer to my question in this thread. I am running out of time and I am so worried.

Please help. Thanks.
GreyHead 19 Apr, 2011
Hi jmarian1,

Why would you want to make a drop-down invisible? That could cause some problems. Adding style='display:none;' should do it.

I'm afraid that I don't understand the question about 'five drop-down' can you explain some more or draw a quick sketch?

Bob
jmarian1 19 Apr, 2011
Hi Bob. Thanks for checking my post. The invisible is supposed to be a hidden textbox and not a drop down but because i made it work in drop down menu I leave it that way. The reason why I wanted to have an invisible or hidden textbox/drop down is because I wanted to capture the text in the hidden textbox to our email receipt so it will send as a link.

The sample form is located at http://jennycollections.com/joomla/index.php?option=com_chronocontact&chronoformname=tryout. The drop down list that I want to hide or can be a textbox is located in the bottom, Part C of the form. Sample choice to select in the first drop down list is the ACLU of Hawaii Foundation. If you select that choice, it will show the Financial Stability and Independence in the bottom. We wanted that hidden so we can capture it to our email receipt as well as how to create a multiple textbox total of five so it will list in the five textboxes their choice. Each drop down list that user will select is equal to an issue and that issue will need to be written in the hidden textbox or drop down.

I also post this as a paid job at http://chronoengine.com/forums/index.php?option=com_chronoforums&cont=posts&f=8&t=21543. If anyone interested, please email me to discuss.

Attached is a simple flowchart. Hope you can help me. Thanks a lot.

Please help. Thanks.
This topic is locked and no more replies can be posted.