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
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
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
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
Bob
Here is the html cord for the buttion:
The varaiable course_date only passes the first word of {$row['title']} (Date: {$row['dates']}).
Fred
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
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.
Bob
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 isvalue='".$row['title'].$row['dates']."'
Bob
Bob,
The code you have suggested works perfectly. I can now continue with the rest of the work.
Thank you for your help.
Fred
The code you have suggested works perfectly. I can now continue with the rest of the work.
Thank you for your help.
Fred
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
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
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
I think that the safest route for this is to htmlencode the string to replace the ' with the equivalent html entity. Try using
Bob
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
Bob,
Your suggested code is working perfectly now and the form has gone live.
Many thanks
Fred
Your suggested code is working perfectly now and the form has gone live.
Many thanks
Fred
Bob
You helped me with the following code to generate radio buttons dynamically from a database:
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
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
Hi Fred,
I have edited your code alittle:
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 />";
}
?>
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
Fred
Hi fred,
The code looks pretty much OK. Try this change
Bob
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
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
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
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
Fred
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
Fred
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
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
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!
Regards,
Max
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
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
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
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
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
Hi Fred,
what if you disabled the "republishing" ? do the radios load the selected value ?
Max
what if you disabled the "republishing" ? do the radios load the selected value ?
Max
Hi Max,
Yes - when I disabled the "republishing", the radios select the right value. But other fields are lost.
Fred
Yes - when I disabled the "republishing", the radios select the right value. But other fields are lost.
Fred
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
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
This topic is locked and no more replies can be posted.