Using php in form HTML

GavinErickson 27 May, 2010
Hi there,

I'd like to add an if then else condition to my form HTML that is printing the results from a multipage form. Its all working fine up to this point.

I'm thinking something like:

<?php if {radio0} IS NULL {

echo "something"};
ELSE;
{echo "something else};
?>

but obviously my syntax is way out - presumably I need to connect to the database, and read the field directly, rather than being able to use the {radio0} shorthand - can you give me a quick pointer...

thanks,

Gavin
GreyHead 28 May, 2010
Hi Gavin,

ChronoForms usually stores the results from a multi-page form in the Session and makes them availalbe to later steps in an array called $posted. So your code might look like
<?php 
if ( !$posted['radio0'] ) {
  echo "something";
} else {
  echo "something else";
}
?>

Bob
GavinErickson 03 Jun, 2010
Thanks Bob,

That was helpful. It didn't quite get there, but am posting my solution for others' reference.

It seemed to work for something like :

<?php

if ($posted['radio0'] ="I am a Registered Medical Practitioner")  {
  echo "I do not require Environmental Health Licensing because I am a Registered Medical Practitioner";}

} else {
  echo "I require Environmental Health Licensing.";
}
?>


However, when i add a third condition something like:

<?php

if ($posted['radio0'] ="I am a Registered Medical Practitioner")  {
  echo "I do not require Environmental Health Licensing because I am a Registered Medical Practitioner";}

else if ($posted['radio0'] ="I am a Dentist") {
echo "I do not require Environmental Health Licensing because I am a Dentist";
} else {
  echo "I require Environmental Health Licensing.";
}

?>


or

<?php
$myradio0=$posted['radio0'];

switch ($myradio0) {

case ("I am a Registered Medical Practitioner"):
echo "I am a Registered Medical Practitioner";
break;

case ("I am a Dentist"):
echo "I am a Dentist";
break;

case (""):
echo "I require Environmental Health Licensing";
break;
}
?>


it doesn't add the third condition. In fact when i look deeper, i'm not sure if this method works, since if I try

<?php

echo $posted['radio0'];

?>


nothing is printed out.

So I went for a database lookup for the last post based on cf_id



<?php

     $q = mysql_query("SELECT radio0 FROM jos_chronoforms_xxx WHERE cf_id= (SELECT  MAX(cf_id) from jos_chronoforms_full_p5)") or die(mysql_error());
    

// store the record of the "example" table into $row
$row = mysql_fetch_array( $q );

// Print out the contents of the entry 
 $myradio0=$row['radio0'];


switch ($myradio0) {

case ("I am a Registered Medical Practitioner"):
echo "I do not require Environmental Health Licensing because I am a Registered Medical Practitioner";
break;

case ("I am a Dentist"):
echo "I do not require Environmental Health Licensing because I am a Dentist";
break;

default:
echo "I require Environmental Health Licensing";
break;
}


?>

?>


Which is now working. I suppose i'd be curious of ideas about preventing the wrong information being retrieved if two people were (more or less) simultaneously filling out a form.

many thanks,

Gavin
GreyHead 03 Jun, 2010
Hi Gavin,

As you say you have no way of being sure that you have the correct info from a DB query (though you could use the save value of cf_id to get the right record).

The if and switch examples both have PHP errors
if ($posted['radio0'] ="I am a Registered Medical Practitioner")  {
Needs '==' in place of '=' becasue if (a=b) always returns true.

The switch example needs to use default: in place of case '':

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