Hi All,
I'm currently putting together a pretty simple (I'd hoped) page where users can submit a questionaire online through clicking on radio buttons (indicating a score from 1-9) for each question. If they click "submit" without answering all the questions, the page reloads saying "you didn't answer question 2) for example. However, the problem I'm running into is that I don't know how to keep all their old selections! So if the pager reloads they basically have to start over, which is hardly ideal . Can anyone help? I've included the code below....

if (hasSurveyBeenCompleted ($connection, $user_id))
{
header("location: surveyCompleted.php");
}

//this branch is only triggered once submit has been pressed and the page "re runs" itself.
if (isset($_POST['submit']))
{

if (!isset($POST["questionOne"])){
echo "you didn't fill question 1 out!";
} else if (!isset($
POST["questionTwo"])){
echo "you didn't fill question 2 out!";
} else {

     $questionOne = $_POST["questionOne"];

  $query = "INSERT into sms.new_survey (answerid, user_id , question_one) Values ($council_id, $user_id, $questionOne);";
  pg_exec($connection, $query);

  // All is good - write info to database and go to main surveys page
  header("location: options.php");

}
}
?><html>
<head>
<title>User Info</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="global.css" type="text/css">

</head>
<body class="bg" style="margin:20px;">
<form action="<? echo $_SERVER['PHP_SELF']; ?>" name="userinfoform" method="POST">
<h2><p>Please enter the appropriate responses and then click on the submit button:</p></h2>
<span style="color:#FF0000"></span>

<table width="603" border="0">

<tr>
<td colspan = 9>1. Are company x complying with your original and ongoing brief?</td>
</tr> 
<tr>
<td><input type="radio" name="questionOne" value="1">1<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="2">2<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="3">3<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="4">4<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="5">5<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="6">6<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="7">7<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="8">8<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionOne" value="9">9<span style="color:#FF0000"></span> </td>
</tr>

<tr>
<td>

</td>
</tr>

<tr>
<td colspan = 9>2. Are company X meeting their deadlines on time?</td>
</tr> 
<tr>
<td><input type="radio" name="questionTwo" value="1">1<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="2">2<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="3">3<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="4">4<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="5">5<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="6">6<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="7">7<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="8">8<span style="color:#FF0000"></span> </td>
<td><input type="radio" name="questionTwo" value="9">9<span style="color:#FF0000"></span> </td>
</tr>

<tr>
<td colspan = 9><input name="submit" type="submit" value="Submit"></td>
</tr>
</table>
</form>

</body>
</html>

😕

    I'd recommend javascript for this, unless you are unable to use javascript. It would be a function tied to the submit button that would loop through and check each choice to make sure one was picked. This would be basically instant for the client, rather than having to wait for a page reload.

    You would need to add a unique id="x" element in each radio button so the javascript could find it. Javascript would be looking at 18 id's, rather than the 2 values that the PHP looks for.

      easiest would be using javascript, however, there are always those that have it off.. so if you want to do PHP and you run into a question not being answered, you'll have to compare the answers they supplied to the options they have to answer, like:

      <td><input type="radio" name="questionOne" value="1"<?php if ($_POST['questionOne'] == "1") echo " checked"; ?>>1<span style="color:#FF0000"></span> </td>
      <td><input type="radio" name="questionOne" value="2"<?php if ($_POST['questionOne'] == "2") echo " checked"; ?>>2<span style="color:#FF0000"></span> </td>
      <td><input type="radio" name="questionOne" value="3"<?php if ($_POST['questionOne'] == "3") echo " checked"; ?>>3<span style="color:#FF0000"></span> </td>
      
      etc...
      

        Thanks for the help guys. Eventually found a solution on my own without having to use any javascript. Can't keep dodging it forever though.

          Could you describe your solution (w/code?) and then in Thread Tools at the top mark "Resolved"?

          Glad it's working!

            I gave you a purely PHP solution. It would be nice to see your solution so if others have this problem, they can potentially find the answer here.

              here's the code i used to make sure each button was checked...

              <table width="603" border="0">

              <tr>
              <td colspan = 9>1. Are Company X complying with your original and ongoing brief?</td>
              </tr> 
              <tr>
              <?
              $i=1;
              for($i=1;$i<10;$i++) {
              	echo "<td><input type=\"radio\" name=\"questionOne\" value=\"".$i."\" ";
              
              	if(isset($questionOne)) { 
                  	if($questionOne==$i) { 
                      	echo "checked = \"true\""; 
                  	}
              	}
              	echo ">".$i."<span style=\"color:#FF0000\"></span> </td>";
              }

              ?>

              Once again thanks for the help...

                that's essentially what I gave you, so yeah, it should work, i didn't loop it as i wasn't sure you wanted it that way, but that's really all mine does...

                thanks for the posting the solution.

                  Hi all.

                  As has been said in this topic,
                  this can be done using either with PHP or java.
                  I always prefer PHP.
                  I dont mind reloading the form until POST Submit has been completed.

                  I think the PHP Code for this can be made as simple & easy, as with java,
                  if not even easier!
                  Which I will try to show in this code.
                  This code has been tested, and it works!

                  I use 3 php variables in the form display wth Radio Buttons: $html1, $html2, $msg
                  .... survey.php

                  <?php
                  // Radio Button Survey
                  // Scripting by halojoy October 2007
                  
                  /* if (hasSurveyBeenCompleted ($connection, $user_id)){
                  header("location: surveyCompleted.php");
                  } */
                  
                  //This page "re runs" itself.
                  
                  // Set the variable values
                  $msg='Welcome. Please fill our survey.';
                  $posted = isset($_POST['submit_survey']) ? true:false;
                  $q1 = isset($_POST["questionOne"])? $_POST["questionOne"]:false;
                  $q2 = isset($_POST["questionTwo"])? $_POST["questionTwo"]:false;
                  
                  // All was well submitted
                  if($q1 && $q2){
                      /* INSERT values $q1 and $ q2 and goto 'options.php'
                      $query = "INSERT into sms.new_survey (answerid, user_id , question_one) 
                      Values ($council_id, $user_id, $questionOne);";
                      pg_exec($connection, $query); 
                  header("location: options.php"); */
                     $msg='Thanks. Your survey submission has been saved.';
                      echo $msg;
                      exit('<br><br>to options.php');// just for script test run
                  }
                  
                  // Test if checked and Create Radio Buttons and 
                  $html1 = $html2 = '';
                  for( $i=1; $i<=9; $i++ ){
                      $chk1 = ($q1==$i)? ' checked':'';
                      $chk2 = ($q2==$i)? ' checked':'';
                      $html1 .= '<td><input type="radio" name="questionOne" value="'.$i.'"'.$chk1.'>'.$i.'</td>'."\n";
                      $html2 .= '<td><input type="radio" name="questionTwo" value="'.$i.'"'.$chk2.'>'.$i.'</td>'."\n";
                  }
                  
                  if($posted){ // POST $q1,$q2 was not completed
                      $msg = '<span style="color:#cc0000;font-weight:bold">
                               Answer all questions.
                               Thank you!</span>';
                  }
                  
                  ?>
                  <html>
                  <head>
                  <title>User Info</title>
                  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                  <link rel="stylesheet" href="global.css" type="text/css">
                  </head>
                  <body class="bg" style="margin:20px;">
                  
                  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="userinfoform" method="POST">
                  <h3>Please enter the appropriate responses and then click on the submit button.</h3>
                  
                  <table width="603" border="0">
                  <tr><td colspan="9">1. Are company x complying with your original and ongoing brief?</td></tr>
                  <tr><?php echo $html1; ?></tr>
                  <tr><td colspan="9">&nbsp;</td></tr>
                  <tr><td colspan="9">2. Are company X meeting their deadlines on time?</td></tr>
                  <tr><?php echo $html2; ?></tr>
                  <tr>
                  <td colspan="9"><input name="submit_survey" type="submit" value="Submit"></td></tr></table>
                  
                  <?php echo $msg; ?>
                  </form>
                  
                  </body></html>

                    HI halojoy (and anyone else who would care to answer),
                    I'm new to both php and web forums in general, and am actually just curious as to how these forums work. Are the replies I've been given sent by random people just looking for questions to answer? Or is it actually somebody's job to provide solutions? Because at the moment to me its just this magic site where when i'm stuck with a problem I post it and a few days later a couple of answers appear.
                    Cheers
                    Nick
                    :o

                      nick.moult wrote:

                      HI halojoy (and anyone else who would care to answer),
                      I'm new to both php and web forums in general, and am actually just curious as to how these forums work. Are the replies I've been given sent by random people just looking for questions to answer? Or is it actually somebody's job to provide solutions? Because at the moment to me its just this magic site where when i'm stuck with a problem I post it and a few days later a couple of answers appear.
                      Cheers
                      Nick
                      :o

                      Nobody gets paid.

                      Actually, the site is most likely a net loss.

                      It's just us geeks that get done or bored with our "real work" come here to help others for free.

                      I'm also a linux developer, so when I write pseudo-code it is more "C" than "PHP" which makes me look dumb.

                      Hang around, there are lots of fun people here. I only found the site last month and haven't stopped terrorizing it since! 🆒

                        Ah cool man. well yeah, I'm actually an engineer but much prefer anything IT related and am on a one way track to geekdom, which suites me fine. See you around.

                          nick.moult wrote:

                          Ah cool man. well yeah, I'm actually an engineer but much prefer anything IT related and am on a one way track to geekdom, which suites me fine. See you around.

                          Stay around, help with what you know, or just read. You'll learn morea bout PHP in a few weeks than taking PHP 101 class at your local post-grad skool.

                            Write a Reply...