Hello

I am using for loop on a form and posting these to the next page and I can echo the array and print 1 if there is value. If there isnt any value nothing print but I want 0 to be printed. I have tired few ways and so far nothing works. Can someone help me

<?php foreach ($_POST["choice"] as $question => $answer) { 
if($answer = "") {

$val=0;}

else $val = 1;

echo "Choice ID: .$question. $val<br />"; 
 }?>

Recommended Answers

All 23 Replies

if condition always takes == for comparison

if($answer == "")

Nope doesnt work. It only print 1 for those who have the value

Member Avatar for diafol
<?php 
foreach ($_POST["choice"] as $question => $answer) { 
if($answer == "") {
 $val=0;
}else{
 $val = 1;
}
echo "Choice ID: .$question. $val<br />";
?>

It only prints the one that are ticked. I am using checkbox for this. The once that is not ticked is just ignored.

while( $info = mysql_fetch_array( $sqll )){
 
 echo "<input type='hidden' name=\"Que_ID\" value=\"{$info['Que_ID']}\" /> ";
  echo " $intNum, {$info['Que_Question']} <br />\n"; 
   $intNum++;
 
 for ($i =1; $i < 5; $i++) {  
 
 echo "<input type=\"checkbox\" name=\"choice[{$intnumber}]\" />{$info['Que_Choice'.$i]}<br />\n";  
  $intnumber++;
 
 }

 }
Member Avatar for diafol

Yes, it will as unchecked checkboxes aren't sent - so don't show up in $_POST. Perhapos you'd be better off using radiobuttons Yes/No?

But for my program, the user can choose more then one checkbox and you can't do that with radio button. Is there any other method I can use?

Member Avatar for diafol

Well, you can use checkboxes as long as your form handler page knows which checkboxes are present in the form being sent to it. Just check each one to see if it's set (isset).

The checked box does get diplayed as 1. If I dont click one then that checkbox is skipped. Since I have different choice name I know which one is missing. Will I be able to do something like

foreach($_POST[Que_ID])... something like for ($i =1; $i < 5; $i++)
echo $_POST[choice]

or something like that. So for each Question ID I have a for loop which will loop the choices up to four. So Que_ID(choice1,choice2,choice3,choice4) THEN Que_ID(choice5,choice6,choice7,choice8)..

I am just guessing, can I do something like that. or NOT

Member Avatar for diafol

Looks a bit complicated. Are there always 4 choices to every question?

If so, loops can be built for:

choice checkbox id = (4 * (Question_id - 1)) + 1 (first one for question)
loop x times until last choice.

Loop for each question. Test isset against each one and echo on match.

I'm not entirely sure here so don't shoot me if I'm wrong, but in C#

""

and

null

are not the same. Have you tried using:

if($answer == null)
{
//code
}
Member Avatar for diafol

You can place this type of thing into your htm:

<input type="checkbox" name="que[1][1]" id="..." />
<input type="checkbox" name="que[1][2]" id="..." />
<input type="checkbox" name="que[1][3]" id="..." />
<input type="checkbox" name="que[1][4]" id="..." />
<input type="checkbox" name="que[2][1]" id="..." />
<input type="checkbox" name="que[2][2]" id="..." />
<input type="checkbox" name="que[2][3]" id="..." />
<input type="checkbox" name="que[2][4]" id="..." />

These corresspond to question 1, answer 1,2,3,4; question 2 answer 1,2,3,4
You can create these via php/MySQL on a loop

In your form handler:

$

ans = $_POST['que'];
$i = 1;
$output = "";
while($i < 6){ //for 5 questions
  if(isset($ans[$i])){
     //check for isset on 1 to 4
     $j = 1; $x = "";
     while($j < 5){
       if(isset($ans[$i][$j]))$x .= $j . ",";
       $j++;
     }
     $x = substr($x, 0, -1);
     $output .= "Your answer(s) to Question $i: $x<br />";
  }else{
     $output .= "You didn't answer Question $i<br />";
  }
  $i++;
}

echo $output;

Doesnt harm to try, I tired it and didnt work.

@ardav. Yes that was me posting that but I have a different method for that post but anyways. II am confused with your loop. so far I can display the choices and the value for them if its 1. I cant do 0. and I though I might as well do this with radio. Now I need to post the question ID for the four choices. But stuck with this.

<?php 
while($submit = $_POST['submit']){
$questionid= $_POST['Que_ID'];
echo "Question ID: $questionid";

foreach ($_POST["choice"] as $question => $answer) 
{ 

if($answer == NULL) 
{ $val=0;
}else{
 $val = 1;
 }
 
    echo "choice id: $question. value: $val";
  }
  

  }

Thanks for your help :D

I didnt see your reply. I like your form method. But when I tried to put the action form, it gives me

Notice: Undefined index: choice in C:\xampp\htdocs\Exam_Online\Student_login\radio1.php on line 2
You didn't answer Question 1
You didn't answer Question 2
You didn't answer Question 3
You didn't answer Question 4
You didn't answer Question 5

I have four choices for each question Id. But the question ID is not limited, which mean I can have as many question as I want on a page. But lets start with 10 maximum.

Member Avatar for diafol

Yes well, I can't see your method working, as an unselected checkbox will not appear, so $val = 0 will never happen.

If you want to keep it simple (as opposed to fiddling with my previous code to get it to work), use a duplicate set of question ids. However, you are producing duplicate names:

Pseudocode:

<input type="checkbox" name="choice[{$intnumber}]" />

You're creating choice[1] to choice[4] on every set of questions. You can't have duplicate names if they are not indexed arrays - i.e. they need to be multidimensional OR use the question number to change the name of the 'name' attribute:

Pseudocode:

<input type="checkbox" name="choice_{$q_id}[{$intnumber}]" />

Now you have a specific name for each question and 4 items in each question array.

I have changed that to your input but I am for looping this

<input type="checkbox" name="que[1][1]" id="..." />

my code

$intNum = 1;
$intnumber = 1;


while( $info = mysql_fetch_array( $sqll )){  
  echo "<input type='hidden' name=\"Que_ID\" value=\"{$info['Que_ID']}\" /> ";
  echo " $intNum, {$info['Que_Question']} <br />\n"; 
   $intNum++;
 
 for ($i =1; $i < 5; $i++) {  
 
 echo "<input type=\"Radio\" name=\"choice[{$info['Que_ID']}.$i][]\" />{$info['Que_Choice'.$i]}<br />\n";  
  $intnumber++;
 }
 }
?>

<input type="submit" value="submit" name="submit">
</form>
Member Avatar for diafol

So what's happening now? Using radio buttons? So, you can't have more than one correct answer to each question. I assumed that you could. If not we've been wasting a lot of time.

Its giving me the quote I have quoted it before. I couldnt do checkbox as you did try to help with this, and since I cant echo the NULL value it is kind if hard for me to continue with this. So I though may be Radio could be easier and tried that. But nothing seems to be working. All I want is to output the question id and the choices for these and POST them to the next page and display them.

I do appreciate the help from you and if I have bothered you or wasted your time then I apologies.

I solved it. Now I am on to the next level. Thank you so much. I used your form and action. I did it the long way but it worked.

Thank you once again :D

have you tried this, just a suggestion

if($answer === null) {

Probably will do the same thing, but if your checking if it's null, you should specify it in the if statement.

Member Avatar for diafol

No prob - WRT wasting a lot of time - I didn't mean that in a stroppy manner. Just that if I realised that we could use a radio button sooner, the solution would have been much easier. Glad you got it sorted though.

I was working on this for ages, just wanted to work. So tried few ways and it gave me the result. So I stopped it there..
Quick question. Are you good with PHP and mysql?

Member Avatar for diafol

> Are you good with PHP and mysql

Who me? Not brilliant, but I have a go from time to time. Just a hobbyist. However, if you're looking for a pro, there's one stand out guy here at the moment (well a few actually), but I won't mention names. If you search this forum for recent posts, you'll see who I mean.

ohh thanks though I just need insert what i am echoing. But i have posted thats on the forum. Thank you so much for your help :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.