Forums

Radio button is not passed complete

fred14 06 Dec, 2008
I am have built a radio button in Chronoform by reading data from MySQL. The radio button consists of statements such as:

o Education for Peace - Intake 2009 (Date: 2008-12-19)

The buttons are printed in the form without any problem. However, only the first word in the chosen button (for example Education” is passed to the atabase linked to the form or the email generated by it. I can not figure out what the problem is? I apprecite any help as I am stuck.
Thanks
Fred
GreyHead 06 Dec, 2008
Hi fred14,

Please can you post the HTML for the button. The most likely explanation is that ChronoForms fails to remove all the spaces in the button name or doesn't apply a value to it.

Bob
fred14 06 Dec, 2008
Bob
Here is the html cord for the buttion:

<?php
$query = "SELECT dates, title, alias, published FROM jos_db WHERE published LIKE 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<input name=course_date type=radio value={$row['title']}.{$row['dates']}/><font color=006600></font>{$row['title']} (Date: {$row['dates']})</font><br>";
} 
?>


The varaiable course_date only passes the first word of {$row['title']} (Date: {$row['dates']}).
Fred
GreyHead 06 Dec, 2008
Hi fred14,

You mostly need some quotes in there. I'd also switch to using the Joomla MySQL code as it gives you better protection.
<?php
$db = & JFactory::getDBO();
$query = "SELECT dates, title, alias, published FROM #__db WHERE published LIKE 1";
$db->setQuery($query);
$rows = $db->loadAssocList();
foreach ( $rows as $row ) 
{
    echo "<input name='course_date' type='radio' value='{$row['title']}.{$row['dates']}' />
        <span style='color:#006600;'>{$row['title']} (Date: {$row['dates']})</span><br />";
}
?>
I don't usually use the { . . .} notation so I'm not certain if the nested quotes will work correctly. The alternative is
value='".$row['title'].$row['dates']."'


Bob
fred14 07 Dec, 2008
Bob,
The code you have suggested works perfectly. I can now continue with the rest of the work.
Thank you for your help.

Fred
fred14 07 Dec, 2008
Bob,
After further testing of the code, I have noticed that if there is ' in the chosen radio button, the value is truncated at that point. For example if the chosen button is:

YBCL program in Baha'i studies- (Date: 27/1/09)

only (YBCL program in Baha) is passed as value.
Is there anyway to avoid this problem?
Thanks
Fred
GreyHead 07 Dec, 2008
Hi fred14,

This is a bit tricky because you are inserting the string into a another string that will then be evaluated as PHP. Each of those steps has the potential for a hiccup. I suspect in this case you end up with code that looks like
echo "<input name='course_date' type='radio' value='YBCL program in Baha'i studies- (Date: 27/1/09)' />
so the PHP is doing its best to parse the value and comes up with
value='YBCL program in Baha'


I think that the safest route for this is to htmlencode the string to replace the ' with the equivalent html entity. Try using
foreach ( $rows as $row )
{
    $value = htmlspecialchars($row['title'].$row['dates'], ENT_QUOTES); 
    echo "<input name='course_date' type='radio' value='$value' />
        <span style='color:#006600;'>{$row['title']} (Date: {$row['dates']})</span><br />";
}

Bob
fred14 07 Dec, 2008
Bob,
Your suggested code is working perfectly now and the form has gone live.
Many thanks
Fred
fred14 19 Jan, 2009
Bob
You helped me with the following code to generate radio buttons dynamically from a database:

<?php
$db = & JFactory::getDBO();
$query = "SELECT dates, title, alias, published FROM . . . WHERE published LIKE 1";
$db->setQuery($query);
$rows = $db->loadAssocList();
foreach ( $rows as $row )
{ 
    $ivalue=$row['title'] . ", " . $row['dates'] . ", " . $row['alias'];
    $value = htmlspecialchars($ivalue, ENT_QUOTES); 
    echo "<input name='course_date' type='radio' value='$value' />
        <span style='color:#006600;'>{$row['title']} (Date: {$row['dates']})</span><br />";
}

?>



I have been receving completed forms without the radio button value. I have now realised that they are the ones that fail in anit-spam validation and then the client forgets to check the button. I tried unsuccessfuly to modify the code to keep the radio value on validaiton based on other responses I have found on the forum. My code seems a bit complicated for me to modify it further. I would appreciate any help I can get to solve this problem.
Fred
Max_admin 19 Jan, 2009
Hi Fred,

I have edited your code alittle:


    <?php
    $db = & JFactory::getDBO();
    $query = "SELECT dates, title, alias, published FROM . . . WHERE published LIKE 1";
    $db->setQuery($query);
    $rows = $db->loadAssocList();
    foreach ( $rows as $row )
    {
        $ivalue=$row['title'] . ", " . $row['dates'] . ", " . $row['alias'];
        $value = htmlspecialchars($ivalue, ENT_QUOTES);
$checked = "";
if($_POST['course_date'] == $value){$checked = "checked";}
        echo "<input name='course_date' type='radio' ".$checked." value='$value' />
            <span style='color:#006600;'>{$row['title']} (Date: {$row['dates']})</span><br />";
    }

    ?>

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
fred14 19 Jan, 2009
Thank you for the suggested code. I have tried it. Something strange happens. Initially the right radio button is checked. But within a second, the checked button is unchecked. I think the code still does not remember which one was checked and build the radio button from scratch.
Fred
GreyHead 20 Jan, 2009
Hi fred,

The code looks pretty much OK. Try this change
    if($_POST['course_date'] == $value){$checked = "checked='checked'";}
but I don't think that should make the difference. Try adding some 'echo' outputs to see what values are being checked in this line:
echo "_POST['course_date']: ".print_r($_POST['course_date'], true)."<br /><br />";
echo "value: ".print_r($value, true)."<br /><br />";


Bob
fred14 20 Jan, 2009
Bob,
I have printed different values and also $checked. The code seems to be doing its job. The $checked is set to checked for the button ticked by the client. But somehow this value is lost when the form regeneration is complete as after re- submit $_POST['course_date'] is blank. I have spent hours today exploring all aspects of the course. Does ChronoForm reset the form values after anti-spam check? I am not getting far.
Fred
fred14 21 Jan, 2009
I have done further work on the code. I am now convinced that the $_POST is reset after the form is republished. What I have done is replace the Submit class button with an HTML submit button. After republishing, I print the value of of $_POST['course_date'] after submit. It has the right value and the right radio is checked. However, after a second it is unchecked. I don't know where to go from here.
Fred
fred14 21 Jan, 2009
Now I have chosen the option of not to republish for the anti-spam. This time radio button data is returned and others were lost. Is $_POST reset after republishing? That is the only possibility.
Fred
GreyHead 21 Jan, 2009
Hi Fred,

I've no real idea what is happening here, nor do I have time to dig into it at the moment.

$_POST isn't usually set on resposting and I can't think why your selection should suddenly change unles there is some sneaky JavaScript doing somethign odd in the background.

ChronoForms usually uses a showform() function to re-display the page and showform($_POST) will show it with the $_POST values and plain showform() show it without. I don't know if this helps at all.

Bob
Max_admin 21 Jan, 2009
Hi Fred,

put Bob's code below at the top of the form HTML between 2 PHP tags and show me whats the output once the form has been loaded with a wrong captcha assuming you will make a radio choice of course!

    print_r($_POST); echo "<br /><br />";
    echo "value: ".print_r($value, true)."<br /><br />";


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
fred14 22 Jan, 2009
Hi max,
This is the print out:

Array ( [course_date] => Women\'s Retreat, 2009-02-20, 95 [first_name] => Fred [last_name] => [date_birth] => [address] => [town] => [state] => NSW [postcode] => [phone] => [mobile] => [email] => [request] => [chrono_verification] => [1978cb08648a9539214cca2e313be3d7] => 1 [cf_wrong_security_code] => 1 )

The array([course_date] shows the right radio button checked but it is not shown on the screen after reloading of the form. The other fields were left blank apart from first_name.

Thank you for help.
Fred
GreyHead 22 Jan, 2009
Hi Fred,

It may be that something in that value string causes problems - ',', '\', and '-' can all cause parsing problems unless they are very carefully handled. I'd use a simplified version like wr_20090220_95

Bob
Max_admin 22 Jan, 2009
Hi Fred,

what if you disabled the "republishing" ? do the radios load the selected value ?

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
fred14 23 Jan, 2009
Hi Max,
Yes - when I disabled the "republishing", the radios select the right value. But other fields are lost.
Fred
Max_admin 23 Jan, 2009
Hi Fred,

That's good! I think this is some conflict between the JS used to load the radios and the dynamic creation of the radios using the PHP, so your only option now is to load all the fields using the old way with PHP, there is some FAQ about this, you just need to echo the $_POST value of every field in your field's value attribute!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.