Radio button code works in one form, but not another

I have two forms that use radio buttons (I’m using CodeIgniter 3 as a framework, but not using their form helper for the radio buttons). I have exactly the same code in each form. In the one form, this works fine -

<input type="radio" name="guidelines" id="signed_yes" value="yes" <?php echo ($guidelines == 'yes') ? 'checked' : ''; ?> />
<label for="signed_yes">Yes</label>
<input type="radio" name="guidelines" id="signed_no" value="no" <?php echo ($guidelines == 'no') ? 'checked' : ''; ?> />
<label for="signed_no">No</label>

In the other form,

<input type="radio" name="caseworker" id="cw_yes" value="yes" <?php echo ($caseworker == 'yes') ? 'checked' : ''; ?> />
<label for="cw_yes">Yes</label>
<input type="radio" name="caseworker" id="cw_no" value="no" <?php echo ($caseworker == 'no') ? 'checked' : ''; ?> />
<label for="cw_no">No</label>

gives me the following error:

Severity: Notice
Message: Undefined variable: caseworker
Filename: views/assessment.php
Line Number: 36

I can’t see any difference between the two. What could be causing this error message?

In the first form somewhere, there is a line that says something like $guidelines = ‘x’

In the second form, a similar line is NOT there. Add a line that creates/initializes the field and you should be fine.

I know to do that in a pure php application, but this is CodeIgniter 3 (which I am still new at) so I have missed something).

The top of the first file is this (the second one is the same thing):

<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

<div id = "wrapper">
    <?php if (isset($_SESSION['logged_in'])) : ?>
    <header id="page-header">
        <h1>Client Registration</h1>
            Coordinator: <?php echo $user->first_name . ' ' . $user->last_name; ?>
            Today's Date: <?php echo date('M. j, Y',  time()); ?>
    <?php if (validation_errors()) : ?>
        <ul class='errors'>
            echo validation_errors("<li>", "</li>");
    <?php endif; ?>

Both forms are processed in controllers that are set up exactly the same way: if (validation rules aren’t followed redirect to form with validation errors) else (send date to model to insert into database then redirect back to form with fields filled in).

That’s why I don’t know where to look for the cause of the error.

Okay, I think I found it - in my first controller, I passed the $_POST value back to the view in the $data array, but I forgot to do that with the other controllers. Thank you for pointing me in the right direction.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.