Jump to content

Recommended Posts

Hi, I'm having a problem trying to work out how to process arrays passed from an HTML form. This is probably due to my lack of PHP experience, but any help would be appreciated. I have a form where the user can enter a range of possible answers to a multiple choice question. For each text field there is also a radio button, which indicates which of the options is the correct answer. I've managed to get this to work using a cumbersome approach, however I really should be using arrays to make it more efficient. I need to insert the question text into a database while also checking the corresponding radio button to see whether or not it is checked as the correct answer. I have labelled the radio buttons with values corresponding to the index of each element in the answers[] array. I therefore need to process the form in a loop which allows me to track the index of each element in the array. A standard for loop using $i as an index would be how I would like to do this, but I am not sure if I can do this in PHP (the foreach loop doesn't seem as it would let me track the index).

 

Here is the HTML form....

 

<form action="<? $_SERVER['PHP_SELF'] ?>" method ="post" id="questSubmit">

Enter the answer options and ensure the radio button selected is the correct answer. <br/><br/>

<input type="radio" name="option" value="0"/> A: <input type="text" name="answer[]" size="80"/><br/><br/>

<input type="radio" name="option" value="1" /> B: <input type="text" name="answer[]" size="80"/><br/><br/>

<input type="radio" name="option" value="2" /> C: <input type="text" name="answer[]" size="80"/><br/><br/>

<input type="radio" name="option" value="3"/> D: <input type="text" name="answer[]" size="80"/><br/><br/>

<input type="radio" name="option" value="4" /> E: <input type="text" name="answer[]" size="80"/><br/><br/>

<input type="radio" name="option" value="No Answer" checked="checked" />No Answer selected

<input name="SubmitNext" value="Save and add another question" type="submit" title="Submit" /><br/><br/>

<input name="SubmitProceed" value="Save and proceed to next stage" type="submit" title="Submit" />

</form>

 

and here's the PHP i have so far - the for loop i've used doesn't actually work...

 

if (isset($_POST['SubmitNext']))

 

{

$correct = $_POST['option'];

echo $correct;

 

$answers = array($_POST['answer']);

echo count($answers);

 

for($i=0;$i<count($answers);$i++)

echo $answers[$i];

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/62542-solved-for-loop-help/
Share on other sites

Your problem is this line of code:

$answers = array($_POST['answer']);

That would make an array, with the first and only element being the value of $_POST['answer'], another array, which will make $answer a multi-dimensional array. I'm guessing that your thought process right there was that you had to cast the POST data to an array - PHP takes the data as it is, meaning that $_POST['answer'] is already an array.

I also see that you're trying to associate the options with the answer, but the way you've done it, there's a change that something could come along and screw it up. Since you're going to connect the two, manually index your input's name.

Example:

//Generate form
$count=5;
$name=array("A","B","C","D","E"); //Yeah, this was cheap. Change this later.
for ($i=0;$i<$count;$i++) {
echo "<input type='radio' name='option' value='$i'>$name[$i] <input type='text' name='answer[$i]' size='80'><br>";
}

//Check submit
if (key_exists("answer",$_POST)) {
$answer=array();
foreach ($_POST["answer"] as $i => $value) { //Indexing too
  if ($_POST["option"][$i]) echo "[] "; //Correct answer
  echo "$value<br>";
}
}

 

Is this for a test generator?

Link to comment
https://forums.phpfreaks.com/topic/62542-solved-for-loop-help/#findComment-311621
Share on other sites

many thanks deadimp - that's a much better and more efficient way of doing it than i had previously. yes it is for a test generator - this is a small part of a large form but probably the trickiest part of it. i changed the code a little to the following as the if ($_POST["option"][$i]) line was giving me a problem. hadn't come across key_exists before either so that's useful for the future -

 

//test to see which is the correct answer

if ($_POST["option"]==$i)

{

    $corr = "yes";

    $query2 = "insert into Answer values ('$id','$opt','$value','$corr')";

    $result2 = mysql_query($query2);

    if (!$result2)

echo "Invalid Insert information";

}

 

Link to comment
https://forums.phpfreaks.com/topic/62542-solved-for-loop-help/#findComment-312676
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.