Jump to content

Retaining Radio button check values upon reload/fail of validation etc.


sampharo

Recommended Posts

Hi guys,

 

I am having a real problem with this and I can't figure it out. I am not that much of a techie so take it easy on me.

 

I am building a survey form for a website to get feedback on the service, and naturally it can fail validation if the email address is misspelled or the security question is answered wrong. Retaining text values was easy and I managed to dig around for some solutions to retain radio button values. The problem is IT ONLY WORKS ON ONE OPTION! I tried everything including coming up with completely separate value names, yet the page still loads with all radio options set to the same shared value (usually of the last radio button question).

 

Here is my code:

 

<?PHP

$Excellent_status = 'unchecked';
$VeryGood_status = 'unchecked';
$Satisfactory_status = 'unchecked';
$Weak_status = 'unchecked';
$Horrible_status = 'unchecked';

if (isset($_POST['send'])) 
{

$selected_tk = $_POST['tk'];

if ($selected_tk == 'Excellent') {
$Excellent_status = 'checked';
}
else if ($selected_tk == 'VeryGood') {
$VeryGood_status = 'checked';
}
else if ($selected_tk == 'Satisfactory') {
$Satisfactory_status = 'checked';
}
else if ($selected_tk == 'Weak') {
$Weak_status = 'checked';
}
else if ($selected_tk == 'Horrible') {
$Horrible_status = 'checked';
}
}

$Excellent2_status = 'unchecked';
$VeryGood2_status = 'unchecked';
$Satisfactory2_status = 'unchecked';
$Weak2_status = 'unchecked';
$Horrible2_status = 'unchecked';

if (isset($_POST['send']))
{

$selected_ts = $_POST['ts'];

if ($selected_ts == 'Excellent') {
$Excellent2_status = 'checked';
}
else if ($selected_ts == 'VeryGood') {
$VeryGood2_status = 'checked';
}
else if ($selected_ts == 'Satisfactory') {
$Satisfactory2_status = 'checked';
}
else if ($selected_ts == 'Weak') {
$Weak2_status = 'checked';
}
else if ($selected_ts == 'Horrible') {
$Horrible2_status = 'checked';
}
}

?>


    			<h1 align="center">Feedback Form:</h1>
    			<p align="center">We highly value your input. Your feedback can help us serve you better and provide you with a holistic experience that meets or exceeds all your expectations. <br />
Please take the time to fill out the form below.</p>
    			<br />
  <br />
  <p align="center"><strong>(Fields marked with a <span class="required">*</span> are necessary. The course information is to validate which course and batch the survey is regarding, and the name and email allows our admin to verify the details and gain more information from you if needed)</strong></p>
  <p align="center"><strong>The identity in this form is strictly confidential, and it will not be revealed to course trainer, co-ordinator, or your sponsor.</strong></p>
  <!--Error Message-->
  <?php echo $error;?>
  <form  method="post" name="contFrm" id="contFrm" action="">
    <label><span class="required">*</span>Name:</label>
    <input name="name" type="text" class="box" id="name" size="40" value="<?php echo $name;?>" /><br />
    <label><span class="required">*</span>Email: </label>
    <input name="email" type="text" class="box" id="email" size="40" value="<?php echo $email;?>" /><br />
    <label><span class="required">*</span>Course attended: </label>
    <input name="course" type="text" class="box" id="course" size="40" value="<?php echo $course;?>" /><br />
    <label><span class="required">*</span>Course Date and City: </label>
    <input name="dateplace" type="text" class="box" id="dateplace" size="40" value="<?php echo $dateplace;?>" /><br /><br /><br />

    <table width="980" border="1" cellpadding="5" cellspacing="1" bgcolor="#FFFFFF">
<tr>
  <td width="280" align="left" bgcolor="#285A80"><strong><font color="#FFFFFF" size="+1">Attribute</font></strong></td>      
        <td width="115">
          <div align="center">EXCELLENT! 
          </div></td>
        <td width="115">
          <div align="center">Really Good. 
          </div></td>
        <td width="115">
          <div align="center">Satisfactory. 
          </div></td>
        <td width="115">
          <div align="center">Weak, needs attention. 
          </div></td>
        <td width="115">
          <div align="center">Horrible! Unacceptable. 
          </div></td>
      </tr>
<tr>
  <td width="280" align="left" bgcolor="#71C4CD"><strong><font color="#333333">Trainer Knowledge</font></strong></td>      
        <td>
          <div align="center">
            <input type="radio" name="tk" id="Excellent" value="Excellent" <?PHP print $Excellent_status; ?>/>
          </div>
        </td>
        <td>
          <div align="center">
            <input type="radio" name="tk" id="VeryGood" value="VeryGood" <?PHP print $VeryGood_status; ?>/>
          </div>
        </label></td>
        <td>
          <div align="center"> 
            <input type="radio" name="tk" id="Satisfactory" value="Satisfactory" <?PHP print $Satisfactory_status; ?>/>
          </div>
        </td>
        <td>
          <div align="center"> 
            <input type="radio" name="tk" id="Weak" value="Weak" <?PHP print $Weak_status; ?>/>
          </div>
        </td>
        <td>
          <div align="center"> 
            <input type="radio" name="tk" id="Horrible" value="Horrible" <?PHP print $Horrible_status; ?>/>
          </div>
        </td>
      </tr>
<tr>
  <td width="280" align="left" bgcolor="#71C4CD"><strong><font color="#333333">Trainer Style and Presentation</font></strong></td>      
        <td>
          <div align="center">
            <input type="radio" name="ts" id="Excellent2" value="Excellent2" <?PHP print $Excellent2_status; ?>/>
          </div>
        </td>
        <td>
          <div align="center">
            <input type="radio" name="ts" id="VeryGood2" value="VeryGood2" <?PHP print $VeryGood2_status; ?>/>
          </div>
        </label></td>
        <td>
          <div align="center"> 
            <input type="radio" name="ts" id="Satisfactory2" value="Satisfactory2" <?PHP print $Satisfactory2_status; ?>/>
          </div>
        </td>
        <td>
          <div align="center"> 
            <input type="radio" name="ts" id="Weak2" value="Weak2" <?PHP print $Weak2_status; ?>/>
          </div>
        </td>
        <td>
          <div align="center"> 
            <input type="radio" name="ts" id="Horrible2" value="Horrible2" <?PHP print $Horrible2_status; ?>/>
          </div>
        </td>
      </tr>
    </table>
    <br /><br />
    <label>Particularly positive points you appreciated: </label>
    <textarea name="message1" cols="80" rows="3"  id="message1"><?php echo $message1;?>
</textarea><br />
    <label>Particularly negative points you found disappointing: </label>
    <textarea name="message2" cols="80" rows="3"  id="message2"><?php echo $message2;?>
</textarea><br />
    <label>Any specific message you would like to provide us: </label>
    <textarea name="message3" cols="80" rows="3"  id="message3"><?php echo $message3;?>
</textarea><br />
<label><span class="required">* Security Question</span></label><? require_once ("ClassMathGuard.php"); MathGuard::insertQuestion(); ?>
    <br />
    <br />
    <!-- Submit Button-->
    <input name="send" type="submit" class="button" id="send" value="" />
  </form>

 

Fact is there are 8 questions (all multiple choice with radio button answers) but I decided to save you a long code read. Basically what I get is that despite choosing different answers for each question, the retention is only of the last option or question, and all the above question choices are set to that choice button.

 

What is wrong?

Link to comment
Share on other sites

Do something like this:

 

<input type="radio" name="a" value="1" <?php if($_REQUEST['a'] == "1") echo "checked"; ?>
<input type="radio" name="a" value="2" <?php if($_REQUEST['a'] == "2") echo "checked"; ?>
<input type="radio" name="a" value="3" <?php if($_REQUEST['a'] == "3") echo "checked"; ?>
<input type="radio" name="a" value="4" <?php if($_REQUEST['a'] == "4") echo "checked"; ?>

Link to comment
Share on other sites

Correct. What I posted should be all you need if the validation fails. Though in my haste, I didn't close the tags properly... so here it is again:

 

<input type="radio" name="a" value="1" <?php if($_REQUEST['a'] == "1") echo "checked"; ?>>
<input type="radio" name="a" value="2" <?php if($_REQUEST['a'] == "2") echo "checked"; ?>>
<input type="radio" name="a" value="3" <?php if($_REQUEST['a'] == "3") echo "checked"; ?>>
<input type="radio" name="a" value="4" <?php if($_REQUEST['a'] == "4") echo "checked"; ?>>

 

Your way could work, but this is simpler and less code.

Link to comment
Share on other sites

This worked perfectly and without all that hassle!

 

Why would most tutorial sources list the earlier complicated coding and subject newbies to all the hassles when an incredibly simple code can get the job done this easily?

 

Have no idea, but thank you so much, this really sorted it.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.