Jump to content


Help with array use, using $_POST

  • Please log in to reply
3 replies to this topic

#1 stern

  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 28 October 2006 - 11:53 PM


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


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.


#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 29 October 2006 - 12:12 AM

Add a hidden field with value=0 to start of each radio group

    echo '<pre>', print_r($_POST, true), '</pre>';
<form method='post'>
    <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>
    <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">

If no answer chosen you get a "0"
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 bqallover

  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • LocationLeeds, UK

Posted 29 October 2006 - 12:23 AM

Hi there.  I think the following should also do the trick.

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

#4 stern

  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 29 October 2006 - 05:35 PM

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

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users