PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,153 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,670 people online right now. Registration is fast and FREE... Join Now!




Radio button

 

Radio button, Retaining initial radio button selections upon page reload

nick.moult

15 Oct, 2007 - 02:15 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 3


My Contributions
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!

CODE


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!


User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Radio Button

15 Oct, 2007 - 04:37 AM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,499



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
Since you have php available... use it.

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

CODE

//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>";
}

User is online!Profile CardPM
+Quote Post

nick.moult

RE: Radio Button

15 Oct, 2007 - 07:33 AM
Post #3

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 3


My Contributions
Hey thanks! With a couple of minor changes to what you sent through I've got it working perfectly.
biggrin.gif icon_up.gif

User is offlineProfile CardPM
+Quote Post

no2pencil

RE: Radio Button

15 Oct, 2007 - 08:58 AM
Post #4

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,499



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
QUOTE(nick.moult @ 15 Oct, 2007 - 08:33 AM) *

Hey thanks! With a couple of minor changes to what you sent through I've got it working perfectly.
biggrin.gif icon_up.gif

That's great news. & welcome to Dream In Code!
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:53PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month