starrylitgal Posted December 29, 2008 Share Posted December 29, 2008 i hav to create a quiz in my website. there i hav an option to create a multiple choice quiz. Following is the code of the front end (enclosed within table tags): <tr><td>Question:</td><td><textarea cols='40' name="qtn" value=""></textarea></td></tr> <tr><td align="right"><input type="radio" name="ch" CHECKED="CHECKED"/></td><td><input type="text" name="c1"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c2"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c3"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c4"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c5"/></td></tr> <tr><td align="center" colspan='2'><input type="submit" name="submit" value="Add Question"/></td></tr> The processing goes like this: The user enters text in the appropriate textboxes and the user selects one of the radio buttons to tell that the corresponding text is the answer. When the user clicks the "Add Question" button, all the text entered (by the user:quiz creator) i.e the question, the options of the question and the correct answer will then be stored in the database. problem: how do i determine the selected radio button with the corresponding text to determine the answer??? Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/ Share on other sites More sharing options...
MatthewJ Posted December 29, 2008 Share Posted December 29, 2008 After re-reading, you may want to rethink how you have this quiz setup... It seems like you are trying to use a radio button to select a value stored in a corresponding text field. Either that, or post all of the code so it might make a bit more sense Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725543 Share on other sites More sharing options...
starrylitgal Posted December 29, 2008 Author Share Posted December 29, 2008 ya im aware that $_POST['ch'] will store the selected radio button. i didnt put any value bcoz it cannot be predefined. In the code that i had posted, along with the radio button there is a textbox field too which determines the value of the radio button(where the user types in the options for the quiz question). So the problem is individually i can retrieve values of the textboxes and the option buttons but how will i relate the option buttons with text entered by the user?? how will i assign the value of the option button with the text entered in the corresponding textboxes?? Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725548 Share on other sites More sharing options...
starrylitgal Posted December 29, 2008 Author Share Posted December 29, 2008 After re-reading, you may want to rethink how you have this quiz setup... It seems like you are trying to use a radio button to select a value stored in a corresponding text field. Either that, or post all of the code so it might make a bit more sense ya i m trying to do the former thing u mentioned. but i didnt understand the following sentence: post all of the code so it might make a bit more sense Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725552 Share on other sites More sharing options...
MatthewJ Posted December 29, 2008 Share Posted December 29, 2008 What I meant was to post the entire script here for us to see... But just quickly, name the radio buttons and text boxes to have corresponding names... Something like "q1_ch1" and "q1_txt1" (question1_checkbox1 and question1_textfield1). Then you will be able to check which item was selected upon POST. You would need to set the value of all of the checkboxes to value="true" So you could check for a certain one being selected with something like this if($_POST['q1_ch1']) { $q1_answer = $_POST['q1_txt1'] } I'm sure there is a better way, but that should at least get you thinking. Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725598 Share on other sites More sharing options...
starrylitgal Posted December 30, 2008 Author Share Posted December 30, 2008 But just quickly, name the radio buttons and text boxes to have corresponding names... Something like "q1_ch1" and "q1_txt1" (question1_checkbox1 and question1_textfield1). Then you will be able to check which item was selected upon POST. You would need to set the value of all of the checkboxes to value="true" So you could check for a certain one being selected with something like this if($_POST['q1_ch1']) { $q1_answer = $_POST['q1_txt1'] } I'm sure there is a better way, but that should at least get you thinking. ya this wud work if we are naming the radio buttons differently as u said..qt1_ch1 (for the radio btn) and qt1_txt1 (for d text field) wht i hav in my code is one common name for all the radio buttons so that a group of radio btns will b formed.This will let the user select only 1 option. Thts y d common name. Do u hav soln for this? i will write the code again: <tr><td>Question:</td><td><textarea cols='40' name="qtn" value=""></textarea></td></tr> <tr><td align="right"><input type="radio" name="ch" CHECKED="CHECKED"/></td><td><input type="text" name="c1"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c2"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c3"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c4"/></td></tr> <tr><td align="right"><input type="radio" name="ch"/></td><td><input type="text" name="c5"/></td></tr> <tr><td align="center" colspan='2'><input type="submit" name="submit" value="Add Question"/></td></tr> i hope this is enough for u to help me with my query Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725970 Share on other sites More sharing options...
MatthewJ Posted December 30, 2008 Share Posted December 30, 2008 A radio group when submitted to PHP using POST submits one value, the value in the selected items value attribute. If you want to determine which radio button was checked to correspond with the text field next to it, the radio button will have to have a unique value. <tr><td>Question:</td><td><textarea cols='40' name="qtn" value=""></textarea></td></tr> <tr><td align="right"><input type="radio" name="ch" value="ans1" CHECKED="CHECKED"/></td><td><input type="text" name="c1"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="ans2"/></td><td><input type="text" name="c2"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="ans3"/></td><td><input type="text" name="c3"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="ans4"/></td><td><input type="text" name="c4"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="ans5"/></td><td><input type="text" name="c5"/></td></tr> <tr><td align="center" colspan='2'><input type="submit" name="submit" value="Add Question"/></td></tr> Then when the form is submitted, $_POST['ch'] would contain one of the values ans1, ans2, ans3, ans4, or ans5 Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725981 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 A better solution would be to have the value of the radio button to be the name of the corresponding text field: <tr><td>Question:</td><td><textarea cols='40' name="qtn" value=""></textarea></td></tr> <tr><td align="right"><input type="radio" name="ch" value="cn1" CHECKED="CHECKED"/></td><td><input type="text" name="c1"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="cn2"/></td><td><input type="text" name="c2"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="cn3"/></td><td><input type="text" name="c3"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="cn4"/></td><td><input type="text" name="c4"/></td></tr> <tr><td align="right"><input type="radio" name="ch" value="cn5"/></td><td><input type="text" name="c5"/></td></tr> <tr><td align="center" colspan='2'><input type="submit" name="submit" value="Add Question"/></td></tr> Ken Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-725987 Share on other sites More sharing options...
starrylitgal Posted December 30, 2008 Author Share Posted December 30, 2008 hey! i managed to solve the problem. this is the code i hav used: <input type='radio' name='ch' value='c1'><input type='text' name='c1'> <input type='radio' name='ch' value='c2'><input type='text' name='c2'> etc.. i think that is wht kenrbnsn had told. thnx all for posting in to help me! Link to comment https://forums.phpfreaks.com/topic/138762-how-to-store-the-text-entered-corresponding-to-a-radio-button-in-a-variable/#findComment-726197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.