Jump to content

Archived

This topic is now archived and is closed to further replies.

stern

Help with array use, using $_POST

Recommended Posts

Hello:

Im hoping someone can help me with what may be a simple problem for most, but has me loosing my mind  lol. I am new to php (used to Basic, C+ and raw assembler code) and its taking me a while to learn (guess cause im old  lol). Anyway, here is the problem.

Im using radio buttons to input multiple choice answers to questions (4 possible answers to each of 100 questions). So 4 copies like this

A <input type='radio' name='chk[$qid]' value='1'>

Now, when its done (display part works ok) I end up with an array that has all the answers, and its in the form of

$_POST['chk']

which if I print_r()  shows it all.  Ok, now my problem

The radio buttons set a value of 1 - 4 depending on the answer chosen, BUT, if a question isnt answered, the array ends up with (i guess) a NULL in that spot. So, if I answer questions 1 - 4 and 47 - 50 I get in the array;

Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [47] => 4 [48] => 3 [49] => 2 [50] => 1 ) when i print it.

Now,  HELP,  HOW can I stop the NULL entry and get a 0 for any radio button set (4 of them) NOT ticked, so that the array will have an entry (1-4 for selections, 0 for didnt answer) for ALL 100 questions (therefore 100 entries long, allowing me to KNOW which answer is for which question when it placed in a BD file).

So this is my delema, getting rid of Nulls so that the array will ALWAYS be 100 entries in size.

Have tried everything, but cant seem to get anywhere, especially with the $_POST thing (too used to $name values, and most sytax wont work on both types).

Anyway, hope i havent rambled to much, and any help anyone can give on how to plug the NULLS with 0's would be greatly appreciated.

Stern

Share this post


Link to post
Share on other sites
Add a hidden field with value=0 to [b]start[/b] of each radio group

[code]
<?php
    echo '<pre>', print_r($_POST, true), '</pre>';
?>
<form method='post'>
    Q1<br>
    <input type="hidden" name="chk[1]" value="0">
    <input type="radio" name="chk[1]" value="1"> ans 1.1
    <input type="radio" name="chk[1]" value="2"> ans 1.2
    <input type="radio" name="chk[1]" value="3"> ans 1.3
    <input type="radio" name="chk[1]" value="4"> ans 1.4
    <br> <br>
    Q2<br>
    <input type="hidden" name="chk[2]" value="0">
    <input type="radio" name="chk[2]" value="1"> ans 2.1
    <input type="radio" name="chk[2]" value="2"> ans 2.2
    <input type="radio" name="chk[2]" value="3"> ans 2.3
    <input type="radio" name="chk[2]" value="4"> ans 2.4
    <br> <br>
    <input type="submit" name="action" value="Submit">
</form>
[/code]

If no answer chosen you get a "0"

Share this post


Link to post
Share on other sites
Hi there.  I think the following should also do the trick.

[code]
for( $i=1; $i<=100; $i++ )
{
if( !isset($chk[$i]) )
{
$chk[$i] = 0;
}
}
[/code]

Share this post


Link to post
Share on other sites
Thankyou Barand, as that did the trick vey well.

I must also add that I think it is really great that someone who is just learning php can come here and have many people jumping in to help. Not only are people fast to assist, they do not "talk down" to php "newbee's" like myself, that can sometimes ask really basic questions.

You should all feel very proud, as these acts of helping an unknown "other" person is was makes all that is RIGHT with the world. I thank you all

Share this post


Link to post
Share on other sites

×

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.