Jump to content

Repopulating Form Info


The14thGOD

Recommended Posts

Hey,

I have a quiz form that I made for a class. Once the user submits, my php script checks to see if the answers are all filled out. If not it sends them back to the form. However, the information is not repopulated, forcing the user to re-answer all the questions. I have some fields working but I'm not sure how to get the radio buttons to work. I have an idea but if anyone has a suggestion as to what is more efficient that would be cool too.

The method I am trying to do is to store the values in cookies. Here's the code in my action script:
[code]for($y=1; $y<21; ++$y) {
setcookie("question[$y]", $question[$y]);
}[/code]
I don't think that is working though because when I try to echo the value of the cookie, I get nothing. The code looks right to me..since it matches the setcookie code I have above that, and it works. Unless I am not able to have a variable in the name of the cookie?

Now once I get that working I was going to do this, this is the part I think is inefficient:
[code]<?
if($_COOKIE['question[1]'] == 1) {
echo(" checked ");
} ?> [/code]
I would have to do that for each of my four options on all 20 questions ^_^.

I just learned PHP in this last quarter of class and we didn't touch on cookies too much. So any help would be much appreciated.

Thanks in advance,

The14thGOD

btw, sorry for the bad code format, I've never posted code before ^_^.
Link to comment
Share on other sites

Here's a method I've used to keep the radio button selections during form processing.

Set up the radio buttons like this:
[code]
<input type="radio" name="radio1" value="Y" <?php echo $ry_check; ?>>Yes
<input type="radio" name="radio1" value="N" <?php echo $rn_check; ?>>No
[/code]

Then use something like this in your form processing.
[code]
<?php
  $ry_check = ($_POST['radio1']=='Y') ? 'checked' : '';
  $rn_check = ($_POST['radio1']=='N') ? 'checked' : '';
?>
[/code]

That should keep the values selected by the user.
Link to comment
Share on other sites

[quote author=bljepp69 link=topic=117932.msg481544#msg481544 date=1165634190]
Here's a method I've used to keep the radio button selections during form processing.

Set up the radio buttons like this:
[code]
<input type="radio" name="radio1" value="Y" <?php echo $ry_check; ?>>Yes
<input type="radio" name="radio1" value="N" <?php echo $rn_check; ?>>No
[/code]

Then use something like this in your form processing.
[code]
<?php
  $ry_check = ($_POST['radio1']=='Y') ? 'checked' : '';
  $rn_check = ($_POST['radio1']=='N') ? 'checked' : '';
?>
[/code]

That should keep the values selected by the user.
[/quote]

I have 20 questions with 4 options.
So should my code be something like this for radio buttons:
[code]
<input type="radio" name="question[2]" value="1" <? echo $q2_1_check ?> />A: Epistomology.<br />
<input type="radio" name="question[2]" value="2" <? echo $q2_2_check ?> />B: Hermeneutics.<br />
<input type="radio" name="question[2]" value="3" <? echo $q2_3_check ?> />C: Ontology.<br />
<input type="radio" name="question[2]" value="4" <? echo $q2_4_check ?> />D: Metaphysics.
[/code]
And then in the form action:
[code]
  $q2_1_check = ($_POST['question[2]']=='1') ? 'checked' : '';
  $q2_2_check = ($_POST['question[2]']=='2') ? 'checked' : '';
  $q2_3_check = ($_POST['question[2]']=='3') ? 'checked' : '';
  $q2_4_check = ($_POST['question[2]']=='4') ? 'checked' : '';
[/code]
I probably should have showed how these were set up in the first place, and I apologize for that.

Also if you don't mind, could you explain this a bit. I've never seen this format, or used the post.

But if I had to guess, i would say, $_POST gets the value of the input type, in this case a radio button, and if it's equal to 'x' make the variable equal to checked, if not it leaves it blank?

Thanks again for the help,

The14thGOD
Link to comment
Share on other sites

You are exactly right. I made the assumption your form method was POST, so the $_POST super global holds the value of all form variables.

You are also correct on the interpretation of the format for the variables.  It's basically a shorthand version of an if/then statement.  It's called a ternary operator and it's defined:
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Link to comment
Share on other sites

I just tried this code and sadly it did not work.

action code
[code]
$q1_1_check = ($_POST['question[1]']=='1') ? 'checked' : '';
$q1_2_check = ($_POST['question[1]']=='2') ? 'checked' : '';
$q1_3_check = ($_POST['question[1]']=='3') ? 'checked' : '';
$q1_4_check = ($_POST['question[1]']=='4') ? 'checked' : '';
[/code]

radio button:
[code]
<td colspan="2"><span class="question">Question 01: The "devine-command-theory" holds that?</span><br />
<input type="radio" name="question[1]" value="1" <? echo $q1_1_check; ?> />A: Certain things are good for us because it says so in the Bible.<br />
<input type="radio" name="question[1]" value="2" <? echo $q1_2_check; ?> />B: Certain actions are right because they are modeled after Jesus.<br />
<input type="radio" name="question[1]" value="3" <? echo $q1_3_check; ?> />C: Certain actions are right because they are what God wills.<br />
<input type="radio" name="question[1]" value="4" <? echo $q1_4_check; ?> />D: Certain actions are divine because they are inherently good.
</td>
[/code]

Any idea why?

So after I hit the submit button, it goes to calculate.php, as I call it, and it goes through that and then after that it checks to see if all questions are answered, if not, then it should come back to the index.php page, and it says my error that it should (please answer question 2) but the question 1 field is still left blank.

Thanks again for any help.

The14thGOD
Link to comment
Share on other sites

It's because you process the form in a different file.  The values for $q1_x_check are set in calculate.php, but then the file with the form in it still doesn't know what those variables are.  If you want to process in a separate file from where the form is, your best bet is to use session variables to pass the information back to the form page.

The other option is to process the form on the same page.
Link to comment
Share on other sites

It's a bit more complicated than that.  Here's a tutorial that describes a method for multipage forms - [url=http://www.phpfreaks.com/tutorials/145/0.php]http://www.phpfreaks.com/tutorials/145/0.php[/url]

There are lots of posts on this board discussion sessions and how to implement them.

I do a lot of forms where I process on the same page.  You basically write your processing code at the top of the page and check for the existence of a certain POST or submit variable when the page loads.  If the form has been submitted, you run your processing script, otherwise, display the form.
Link to comment
Share on other sites

I think bljepp69's approach is the best, process it within the same page. It's not the most efficient, but it certainly is the easiest. But you could also keep the frontend seperate from the logic through a simple require_once(). Require your calculate.php file at the top of the form page then you still have access to all the variables you initially posted. Change the form action to the form page of course...
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.