Radio button

Retaining initial radio button selections upon page reload

Page 1 of 1

3 Replies - 1752 Views - Last Post: 15 October 2007 - 09:58 AM Rate Topic: -----

#1 nick.moult  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-October 07

Radio button

Posted 15 October 2007 - 03:15 AM

HI All,
I'm currently trying to creat a page that allows users to complete questions online. If they click submit without answering some of the questions the page is reloaded saying "You didn't answer question 1" etc. Now while I have this working fine, I can't retain their old selections! Ie: they get the error message and have to start again with a blank questionnaire. I'm not sure how to correct this. I've attached the important part of the code I'm currently working with. Note that this example only shows 2 questions, this is just to avoid repetition. In reallity there are a lot more questions and would like this functionality to exist in all cases. So any help would be much appreciated!


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>




so to reitterate - If the user doesn't click button 2, he gets a page returned to him saying "you didn't answer question 2" (that part works so far) and question one retains his original selection......(this doesn't work yet and this is where i need the help!)

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Radio button

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5327
  • View blog
  • Posts: 27,246
  • Joined: 10-May 07

Re: Radio button

Posted 15 October 2007 - 05:37 AM

Since you have php available... use it.

I'd loop over all 10 values, rather than using html to hard code each one.

//this branch is only triggered once submit has been pressed and the page "re runs" itself.
if (isset($_POST['submit'])){
  
	if (isset($_POST["questionOne"])){
		$questionOne = $_POST["questionOne"];
	}
	else {
		$err = "you didn't fill question 1 out!";
	}
	
	if (isset($_POST["questionTwo"])){
		$questionTwo = $_POST["questionTwo"];
	} 
	else {
		$err "you didn't fill question 2 out!";
	}
	
	if(isset($err) { echo $err; }
	
	$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");

}


$i=0;
for($i=0;$i<10;$i++) {
	echo "<td><input type=\"radio\" name=\"questionOne\" value=\"".$i."\" ";

	if(isset($questionOne)) { 
		if($questionOne=$i) { 
			echo "Selected"; 
		}
	}
	echo ">".$i."<span style=\"color:#FF0000\"></span> </td>";
}

for($i=0;$i<10;$i++) {
	echo "<td><input type=\"radio\" name=\"questionTwo\" value=\"".$i."\" ";

	if(isset($questionTwo)) { 
		if($questionOne=$i) { 
			echo "Selected"; 
		}
	}
	echo ">".$i."<span style=\"color:#FF0000\"></span> </td>";
}


Was This Post Helpful? 0
  • +
  • -

#3 nick.moult  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-October 07

Re: Radio button

Posted 15 October 2007 - 08:33 AM

Hey thanks! With a couple of minor changes to what you sent through I've got it working perfectly.
:D :^:
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5327
  • View blog
  • Posts: 27,246
  • Joined: 10-May 07

Re: Radio button

Posted 15 October 2007 - 09:58 AM

View Postnick.moult, on 15 Oct, 2007 - 08:33 AM, said:

Hey thanks! With a couple of minor changes to what you sent through I've got it working perfectly.
:D :^:

That's great news. & welcome to Dream In Code!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1