Forums

how to add custom code select box to form->data

rockie12_us 23 Sep, 2012
Hi
I have a custom code in the On Load, which does a query of a database and then builds a drop down box.

Question, is how do I add that select drop down box to the form->data so I can use the value selected in the drop down box in the next form?

The select box is input_select_4 it creates this drop down just fine, but it does not appear to be part of the form->data per the debug element. I do a redirect user to this same page, so as they select an item, I query the database again and bring back another set of values... except this is not working. It only brings back the first set of values.

Here is my custom code
<?php
// localhost
$dbhostname='localhost:8889';
$dbusername='root';
$dbpassword='root';
$dbname='cfps';


$con = mysql_connect($dbhostname,$dbusername,$dbpassword);
mysql_select_db($dbname, $con);



$inValue = $form->data['input_select_4']; 
$sql = 'select parent_id, btn_txt, image_file, page_text from sa_content where parent_id = '.$inValue;



$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

if($row['btn_txt'] == '' && $row['image_file'] = '')
{
// print out page text
$pgtext = $row['page_text'];
echo "<h2>$pgtext</h2>";
} 
else if($row['btn_txt'] == '' && $row['page_text'] = '')
{
// show the image
$imageFile = $row['image_file'];
echo "<img src='/images/$imageFile'";
}
else
{
// show select box input_select_4
$result1 = mysql_query($sql) or die(mysql_error());
echo "<select name='input_select_4'>";
while($row1 = mysql_fetch_array($result1))
{
$prnt_id = $row1['parent_id'];
$btntxt = $row1['btn_txt'];
echo "<option value='$prnt_id'>$btntxt</option>";
}
echo "</select>";
}


?>
GreyHead 24 Sep, 2012
Hi rockie12_us,

If you look at the page source I suspect that you will find that your select box HTML is outside the <form> tags. That would explain why the result isn't included in the Debugger data.

You can try switching the Custom Code action Mode setting to View instead of Controller. The Help tab says that this mode is used for HTML output.

Personally I prefer to put code like this into a Custom Element element so that I can control where it appears in the form. Or I split the code with keeping the data retrieval in the Custom Code action and putting the output into a Custom Element element.

Bob
rockie12_us 24 Sep, 2012
Hi Bob

I switched it to view and it is still out side the form tag

Not sure how to make a custom element element as you suggested.

i have this in a custom code and set to view but it is still outside the form tag

How do I fix this so my custom generated code is inside the form tag?

are there any examples that you might point me to?

Thanks
Dean-O
rockie12_us 25 Sep, 2012
Hi all

I found the custom element element

and I put one of those in the preview of the form its field name is input_custom_3 so then I tried my hand at setting the content of it, which I was successful using the custom code element, but it is still outside the form tag

here is my custom code I used trying to set the custom element element

<?php
// localhost
$dbhostname='localhost:8889';
$dbusername='root';
$dbpassword='root';
$dbname='cfps';


$con = mysql_connect($dbhostname,$dbusername,$dbpassword);
mysql_select_db($dbname, $con);



$inValue = $form->data['input_select_4']; 
$sql = 'select cf_id, btn_txt, image_file, page_text from sa_content where parent_id = '.$inValue;



$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

if($row['btn_txt'] == '' && $row['image_file'] = '')
{
// print out page text
$pgtext = $row['page_text'];
$htmlStr = "<h2>$pgtext</h2>";
//echo "<h2>$pgtext</h2>";
$form->data['input_custom_3'] = $htmlStr;
} 
else if($row['btn_txt'] == '' && $row['page_text'] = '')
{
// show the image
$imageFile = $row['image_file'];
//echo "<img src='/images/$imageFile'>";
$htmlStr = "<img src='/images/$imageFile'>";
$form->data['input_custom_3'] = $htmlStr;
}
else
{
// show select box input_select_4
$result1 = mysql_query($sql) or die(mysql_error());
$htmlStr = "<select name='input_select_4'>";
//echo "<select name='input_select_4'>";
while($row1 = mysql_fetch_array($result1))
{
$cf_id = $row1['cf_id'];
$btntxt = $row1['btn_txt'];
//echo "<option value='$prnt_id'>$btntxt</option>";
$htmlStr = $htmlStr . "<option value='$cf_id'>$btntxt</option>";
}
//echo "</select>";
$htmlStr = $htmlStr."</select>";

$form->data['input_custom_3'] = $htmlStr;

}


?>



Is this really this hard?

Does the version 3 book contain answers to this type of situation even though I am using version 4 of chronoforms?

Thanks in advance if anyone can help get me over this hump.

Dean-O
GreyHead 25 Sep, 2012
Hi rockie12_us,

It's pretty hard to understand the logic of the code you have here. I don't think that the way you have the second loop for the options will work at all. And there is nothing at the end to output your $HtmlStr.

I had a go at re-writing it but I'm not convinced that the loops will work correctly. It may give you some hints though:
<?php
$options = array(
  'driver' => 'mysql',
  'host' => 'localhost:8889',
  'user' => 'root',
  'password' => 'root',
  'database' => 'cfps'
);//
$db2 = & JDatabase::getInstance( $options );
$sql = "
  SELECT cf_id, btn_txt, image_file, page_text
    FROM `sa_content`
    WHERE `parent_id` = '{$form->data['input_select_4']}';
";
$db2->setQuery( $sql );
$options_data = $db2->loadResultArray();
$first_result = true;
$build_select = false;
foreach ( $options_data as $d ) {
  if ( $first_result && $d['btn_txt'] == '' && $d['image_file'] = '' ) {
    // print out page text
    $htmlStr = "<h2>{$d['page_text']}</h2>";
    continue;
  } elseif ( $first_result && $d['btn_txt'] == '' && $d['page_text'] = '' ) {
    // show the image
    $htmlStr = "<img src='/images/{$d['image_file']}' >";
    continue;
  } else {
    // show select box input_select_4
    if ( $first_result ) {
      $htmlStr = "<select name='input_select_4'>";
      $build_select = true;
    }
    $cf_id = $row1['cf_id'];
    $btntxt = $row1['btn_txt'];
    //echo "<option value='$prnt_id'>$btntxt</option>";
    $htmlStr .= "<option value='{d['$cf_id']}' >{$d['btntxt']}</option>";
  }

}
if ( $build_select ) {
  $htmlStr .= "</select>";
}
echo $htmlStr;
?>

Bob
rockie12_us 25 Sep, 2012
Hi Bob
Thanks for your reply

The quest is to start at main page select something from a drop down and be redirected on submit to next_page1

Then when next_page1 comes up it goes through a db query to determine whether to show one of the following.

1. and image
2. a list of items in a table
3. a drop down box with new options.

If it is the drop down that is displayed, when the user selects something from the drop down and hits submit, it takes them to next_page1 again and the process repeats showing one of the 3 things listed above.

The custom code I had would show the drop down list but it would always appear outside the form tag.

Will your modifications make it appear in the form tag of the recursively navigated to next_page1 page?

THanks for you continued support. This tool looks like something I would want to use and pay for, but this simple task of creating the recursive page navigation is seeming harder than it should be... but then again, I do not know the tool well so there is my frustration🙂

THanks again
Dean-O
GreyHead 25 Sep, 2012
Hi Dean-O,

[quote}. . . it goes through a db query to determine whether to show one of the following.

1. and image
2. a list of items in a table
3. a drop down box with new options.[/quote]I don't think that your code actually does that. Have you tested it?

Bob
rockie12_us 25 Sep, 2012
Yes I tested it. It will bring back the list of items for next_page1 and shows them but they are outside the form tag

How can I put screen shots in these replies?
GreyHead 25 Sep, 2012
Hi Dean-O,

Use the Upload attachment link under the edit box.

Bob
rockie12_us 25 Sep, 2012
So how do I get the code inside the form tag?
GreyHead 26 Sep, 2012
Hi Dean-O,

Provided that you echo it out from the Custom Element element, of use the View mode of the Custom Code element it will be inside the form tags. More info in my previous posts.

Bob
rockie12_us 26 Sep, 2012
This is still not working.

I have attached a detailed summary of what I am trying to do with screen shots.

if someone could help me accomplish this task that would be great.

Thanks
Dean-O
GreyHead 27 Sep, 2012
Hi Dean-O,

As far as I can see I have already answered this question. Please check back over the previous posts.

Bob
rockie12_us 27 Sep, 2012
I guess I missed that

I have tried the custom code. Custom element element too

Am I to make the custom code update the custom element element?

If so how?

Am I to just use a custom element element in view mode?

I guess a simple working example of how this can be done would go a lone way to making this understood

I have taken the customs code I have and tested it as a stand alone php page in which I had to put my own form tags in and it works like a champ. So I know my logic is valid.

How can I do the same thing with his tool?
GreyHead 27 Sep, 2012
Hi Dean-O,

Somewhere you need to output the string you've created. The easiest way to do that is to echo it out from a Custom Element element. See my example earlier:
}
echo $htmlStr;
?>

Bob
rockie12_us 27 Sep, 2012
So just use a custom element element in view mode? Not a custom code and a custom element both?
GreyHead 27 Sep, 2012
Hi Dean-O,

Personally I prefer to put code like this into a Custom Element element so that I can control where it appears in the form. Or I split the code with keeping the data retrieval in the Custom Code action and putting the output into a Custom Element element.


Bob
rockie12_us 27 Sep, 2012
Hi Bob
I saw that but what is not clear is

Do I just put all the custom php code in a custom element element and not use a custom code element?

Or if I put the custom php code in a custom code element how do I get the outcome of the custom code to update the custom element element?

I appreciate all you responses

For a newbie a simple working example would go along way to clarify the process

Once I get this working I will post the working example I create so others can benefit

I am sure this is something others would benefit from
rockie12_us 28 Sep, 2012
I will ask again. Please answer these question here so I know, since I missed your explanation earlier.

Am I to make the custom code update the custom element element?

If so how?

Or...

Am I to just use a custom element element in view mode?
rockie12_us 28 Sep, 2012
Bob thanks for your patients...

I just built it with only a custom element element and it works like a champ. May not be the best solution... please advise.

I am going to get some sleep and post a solution tomorrow with screen shots etc.

Dean-O
GreyHead 28 Sep, 2012
Hi Dean-O,

That's a perfectly good solution. I prefer sometimes to keep the code part in a Custom Code action, save the data in a $form->data array entry, and then display it using a Custom Element element. That's a little more complicated but helps separate the code and the HTML parts of the form which can be helpful with a complicated form.

Either approach is OK though.

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