Forums

search mysql table and display fields in form fields

ddia1312 02 Mar, 2015
Hi,

At first sorry for my bad english. I installed a form with two fields. In first field I insert a value and i want to search this in a table. When the value is maching with the table field, then i want to display the value of another field of this record into the second field of form. Can someone to help me ?

Than you

Dimitrios
GreyHead 02 Mar, 2015
Hi Dimitrios,

How do you want this to work?

If the form is submitted after the first entry then you can use a DB Read to find the second value and display it.

If you want to do this in the browser then you can do it with JavaScript using Ajax.

Bob
ddia1312 02 Mar, 2015
Hi Bob,

Thank you for your direct answer. I am an old cobol programmer (66 years old) and as you understand is difficult for me to understand new languages. I have a chronoforms form with the following code

<div class="form_item">
  <div class="form_element cf_heading">
    <h1 class="cf_text">REGISTRATION FORM</h1>
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">STUDENT CODE</label>
    <input class="cf_inputbox required validate-alpha" maxlength="150" size="8" title="" id="kodik" name="kodik" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<?php
$db =& JFactory::getDBO();
$query = "
  SELECT `kod`, `firstn`, `epon`
    FROM `jos_students_deals`
    WHERE `kod` = `kodik`;
";
$db->setQuery($query);
$options = $db->loadAssocList();
?>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">NAME</label>
    <input class="cf_inputbox" maxlength="150" size="30" title="" id="onoma" name="onoma" type="text" value='<?php echo $cf_data->epon; ?>' />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="SUBSCRIBE" name="button_6" type="submit" /><input type="reset" name="reset" value="Reset"/>
  </div>
  <div class="cfclear"> </div>
</div>



Also I installed, in local database, a mysql table named "jos_students_deals" with three fields named kod, firstn, epon.
This which I want is after the field form STUDENT CODE is filled with value and cursor is sitting in next field form NAME then to display in this field the epon value of matching record.

I hope that you understand my problem.

Thank you
GreyHead 03 Mar, 2015
Hi Dimitrios,

There are some timing problems here. The Form HTML+PHP you have here is run on the server *before* the form is displayed - so at that point none of the form inputs have any values and the database query will fail.

Also the Student Code presumably only accepts certain values (those in the database table) so you don't want to use a free-form text input there. A select drop-down showing the list of codes might be more useful.

I would suggest that you put the drop-down in the first page of a multi-page form, when that page is submitted use a DB Read action to get the information about the code and show that - with other inputs - in the second page of a form.

It is also possible to do this in one page using JavaScript and Ajax to get the information but this may be too advanced for the moment.

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