Jump to content

Recommended Posts

I'm doing a contest, the contest consists of 50 yes or no questions.

 

The questions are pulled from a database.

 

What I cannot figure out, is how to post the information back to the database.

 

When the questions are pulled from the database, each on has radio buttons for yes or no

I cannot name the radio the radio buttons "yes" or "no", because then it will only work for one question

 

I am doing this

<input name="<? echo "q_$dailyrow[id]"; ?>" type="radio" value="1">

for the radio buttons, but how can I input into the database the question ID plus yes or no for each question?

 

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/126603-solved-radio-buttons-for-contest-help/
Share on other sites

The value for the radio buttons is either going to be 1 for yes, or 0 for no.

 

The name of the radio buttons cannot be the same because you will only be able to select one question.

So each radio button pair have to have a different name, so I'm using the question ID as the radio button name

Use arrays.  One holds the name, the second holds the value of that question's answer (yes/no).  Here's a simple example that you could use but need to implement an array:  home and learn

 

What I cannot figure out, is how to post the information back to the database.

 

What's all the information that you need to store?  User/Question/Answer?

I understand that.

But how do I get the info that was submitted for the database?

 

My radio buttons are like so

<input name="<? echo "q_$dailyrow[id]"; ?>" type="radio" value="1">yes
<input name="<? echo "q_$dailyrow[id]"; ?>" type="radio" value="0">no

 

Then the process part is where I get stuck

for($counter=0;$counter < count($name);$counter++ ){
          $guess_string .= $_POST['q_'.$name][$counter];
          echo $guess_string;
}

 

I need to be able to echo out the question id along with 1 or 0

 

And I'm not sure how to do it

I've been trying different things, and nothing was being echoed out.

 

I finally got the value to echo, but not the name of the button.

 

Here is what my radio button looks like

<input name="<?=$dailyrow['id']; ?>" type="radio" value="1">

 

And here is the code that process's it

foreach ( $_POST as $postitem ) {	
echo '$name - <input type="text" name="postitem[]" value="' . $postitem .'"><br>';
}

 

Can anybody tell me how I can actually echo out the name of the button too?

The table should contain the User ID, the Question ID and the answer.  Since it's a Yes/No, you only need to store 0 or 1

 

 

uID | qID | Ans

 

1       1       0

1       2       1

 

 

etc

 

I think you are over thinking it.  You don't need a seperate name for each button, just a different array slot.

Very true

But what I'm trying to do, is give them a page that they would verify their answers before actually submitting them.

 

That's why I'm trying to echo the questionID with the answer, then the questionID and grab the appropriate question and display it for them, if they are satisfied, then they click on submit, if they want to change an answer, then they would click change answer.

yes.

I need to store

questionID

name

date

answer

So, are u using some kind of statement that is looping your format each time with a different question, right?

how about something like-

<?php //your for, while, or whatever you use
$i = $questionID; echo $question.': <input name="'.$i.'" type="radio" value="1" /><input name="'.$i.'" type="radio" value="0" />'; //close your statment and whatever. ?>

I'm using this

 

<?php
$dailysql = "SELECT * FROM pool ORDER BY id LIMIT 5";
$dailyres = mysql_query($dailysql);				
while($dailyrow = mysql_fetch_assoc($dailyres)) { 
?>
<?=$dailyrow['question']; ?> - <input name="<?=$dailyrow['id']; ?>" type="radio" value="1">
<?=$dailyrow['question']; ?> - <input name="<?=$dailyrow['id']; ?>" type="radio" value="0">
<?php } ?>

This grabs all the questions.

 

That isn't the problem.

 

The problem is, when they click on submit, how would I have the page echo the question along with the answer?

I can echo the answer with no problem, but since the input name is different with every question, I cannot figure out how to echo <input name="<?=$dailyrow['id']; ?>">

 

If someone could just show me the proper way to echo out the name of the input name, I would be greatly appreciative.

Sorry, my last comment wasn't of any help, you are already past that.

I have an idea, again, may not be helpful.

Add an incrementing value that loops with the question. The only use for it is to count how many questions. Then, at the end, have a hidden field that has that value in it so that you can pass it to the next page. then You could use that number in your statement to tell it how many times to loop. but again, I'm not sure if that is even helpful to you.

If you are having a problem sending the question ID along, have a hidden field for each question that contains the question ID in the value.

<input name="question<?php echo $i++; ?>" type="hidden" value="<?php echo $dailyrow['id']; ?>" />

 

btw, how are you resubmitting the values once you have them on the page for review? just interested.

I figured it out with list

 

Form element

<input name="q_[<?=$dailyrow['id']; ?>]" type="radio" value="1">
<input name="q_[<?=$dailyrow['id']; ?>]" type="radio" value="0">

 

and the process part

$name = $_POST;
while (list ($name,$val) = @each ($stock_)) {
echo "$name, $val<br>";
} 

 

Thanks for everyone's help

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.