Problem with Checked Syntax

I have a Function which retrieves a Question from my database, and based on the Question-Type, it builds the entire Question/Answer pair, and returns the finished HTML in a variable which is then echoed by my calling script.

I am trying to add code to make each Answer “sticky” in case there are data entry errors.

The code below is not working, and is driving me crazy…


$surveyResponse=1;

$surveyItem =
	"<li>
		<fieldset class='optionYesNo'>
			<legend>$questionNo.) $questionStem</legend>
			<div>
				<input id='Question" . $articleSurveyQuestionID . "_Y' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='1' " . ($surveyResponse==1) ? "checked='checked'" : "" . " />
				<label for='Question" . $articleSurveyQuestionID . "_Y'>Yes</label>
				<input id='Question" . $articleSurveyQuestionID . "_N' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='0' />
				<label for='Question" . $articleSurveyQuestionID . "_N'>No</label>
			</div>
		</fieldset>
	</li>";

I am trying to mimic the code below which is from another script.

While the code below is admittedly different, since it appears in the HTML section and needs to use a PHP echo, I still thought my adjusted code above was following the same format?! :-/


	<input type="radio" name="gender" value="1" <?php echo (isset($gender) && $gender == "1") ? 'checked="checked"' : ''; /> Male

When I load my Form, I am just getting this…

checked=‘checked’

What am I doing wrong?

Thanks,

Debbie

You’re going to have to break out of defining your variable $surveyItem in order for the ternary operator to work. Try this:


$surveyResponse=1;

$surveyItem =
    "<li>
        <fieldset class='optionYesNo'>
            <legend>$questionNo.) $questionStem</legend>
            <div>
                <input id='Question" . $articleSurveyQuestionID . "_Y' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='1'";
$surveyItem .= ($surveyResponse==1) ? " checked='checked'" : "";
$surveyItem .= " />
                <label for='Question" . $articleSurveyQuestionID . "_Y'>Yes</label>
                <input id='Question" . $articleSurveyQuestionID . "_N' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='0' />
                <label for='Question" . $articleSurveyQuestionID . "_N'>No</label>
            </div>
        </fieldset>
    </li>";

Thanks for the response, but that is pretty messy, especially since you added code for 1 of the 2 radio buttons.

What happens on Questions that have 10 radio buttons?! :eek:

There must be a more efficient way…

Thanks,

Debbie

DoubleDee: I’ve had this problem, too. Try enclosing your ternary operator expression in another set of parentheses, like this:

( ($surveyResponse==1) ? “checked=‘checked’” : “” )

HTH; let me know.

If you were here I’d KISS YOU!!!

Once again, parentheses to the rescue!

I had come up with a better solution than the recommendation above, but your solution get the prize!! :tup:

Thanks,

Debbie

Glad I could help.

Just wanted to explain why adding the parentheses is necessary.

Here was your original code

$surveyItem = 
    "<li> 
        <fieldset class='optionYesNo'> 
            <legend>$questionNo.) $questionStem</legend> 
            <div> 
                <input id='Question" . $articleSurveyQuestionID . "_Y' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='1' " . ($surveyResponse==1) ? "checked='checked'" : "" . " /> 
                <label for='Question" . $articleSurveyQuestionID . "_Y'>Yes</label> 
                <input id='Question" . $articleSurveyQuestionID . "_N' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='0' /> 
                <label for='Question" . $articleSurveyQuestionID . "_N'>No</label> 
            </div> 
        </fieldset> 
    </li>";  

Here is your original code rewritten (as the compiler would write it)

$surveyItem = 
    "<li> 
        <fieldset class='optionYesNo'> 
            <legend>$questionNo.) $questionStem</legend> 
            <div> 
                <input id='Question" . $articleSurveyQuestionID . "_Y' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='1' ";
if ($surveyResponse==1)
  $surveyItem .= "checked='checked'";
else
  $surveyItem .= "" . " /> 
                <label for='Question" . $articleSurveyQuestionID . "_Y'>Yes</label> 
                <input id='Question" . $articleSurveyQuestionID . "_N' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='0' /> 
                <label for='Question" . $articleSurveyQuestionID . "_N'>No</label> 
            </div> 
        </fieldset> 
    </li>";  

See the problem? The parentheses prevents the compiler from making your else expand to the rest of your output, it scopes it to only apply between “checked=‘checked’” and “”.

So you end up with something similar to, but written in a much smaller/less messy manner

$surveyItem = 
    "<li> 
        <fieldset class='optionYesNo'> 
            <legend>$questionNo.) $questionStem</legend> 
            <div> 
                <input id='Question" . $articleSurveyQuestionID . "_Y' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='1' ";
if ($surveyResponse==1)
  $surveyItem .= "checked='checked'";
else
  $surveyItem .= "";

 $surveyItem .= " /> 
                <label for='Question" . $articleSurveyQuestionID . "_Y'>Yes</label> 
                <input id='Question" . $articleSurveyQuestionID . "_N' name='responseToQuestion[" . $articleSurveyQuestionID . "]' type='radio' value='0' /> 
                <label for='Question" . $articleSurveyQuestionID . "_N'>No</label> 
            </div> 
        </fieldset> 
    </li>";  

cpradio,

Thanks for the additional explanation!

Debbie