Forums

How to set a selected value to a select box ?

CHIESA 31 Aug, 2010
Hello.

I use Chronoforms to edit a row from ChronoConnectivity.
All works perfectly fine, except for a detail.

When i use a "dropdown list" with a list of values, Chronoforms find correctly the value stored in the row an add

selected='selected'

to tag <option>.
Example :

<select name="pays" id="pays" >
    <option selected="selected" value="FRANCE">FRANCE</option>
    <option  value="BELGIQUE">BELGIQUE</option>
    <option  value="LUXEMBOURG">LUXEMBOURG</option>
</select>


When i use a "dropdown list" with a dynamically build list, Chronoforms does not find the selected value. And I do not find the name of the variable to check ...
Example :

<select name="pays" id="pays">
  <?php 
     foreach ( $pays as $p => $n ) {
       $s = '';
       if ( ????? == $p ) {
          $s = "selected='selected'";
       }
       echo "<option value='$p' $s >$n</option>";
     }
</select>


Does anybody have a solution , please ?
GreyHead 31 Aug, 2010
Hi CHIESA,

ChronoForms can’t see HTML generated by PHP so you need to set the selected value yourself. I think that the data is passed in a $row object so the code would be something like this:
<select name="pays" id="pays">
  <?php
     foreach ( $pays as $p => $n ) {
       $s = '';
       if ( $row->pays == $p ) {
          $s = "selected='selected'";
       }
       echo "<option value='$p' $s >$n</option>";
     }
</select>


Bob
CHIESA 01 Sep, 2010
Hi, Greyhead.
A lot of thanks for this answer.
I understand the problem.
I have tried your solution, but unfortunately it does not work.
It seems that $row is empty. i have tried also $cf_row, $chronoform_row, and a lot of other names, but in vain.
...
Best regards
GreyHead 01 Sep, 2010
Hi CHIESA,

Soory about that . . . I don't use the built-in links but create my own links to the ChronoForm so I know exactly what data is there. I'll try to test it out later.

Bob
GreyHead 01 Sep, 2010
Hi CHIESA,

I got the values of $row with this code:
<?php
if ( !$mainframe->isSite() ) { return; }
$connectionname = JRequest::getString('connectionname', '', 'get');
$MyConnection =& CFChronoConnection::getInstance($connectionname);
$rowid = JRequest::getInt('cids', '', 'get');
$row =& JTable::getInstance(str_replace($mainframe->getCfg('dbprefix'), '', $MyConnection->connectionrow->tablenames), 'Table');
$row->load($rowid);
//echo'<div>$row->text_0: '.print_r($row->text_0, true).'</div>';
?>

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