开发者

Recording values of radio buttons in a db(php)

开发者 https://www.devze.com 2023-04-13 08:57 出处:网络
I have a page of questions that have two options as radio buttons.Each Question has a form tag.Now i want to store the values checked by the user to a database through a single function in php.How to

I have a page of questions that have two options as radio buttons.Each Question has a form tag.Now i want to store the values checked by the user to a database through a single function in php.How to do it开发者_JAVA技巧? My html code is as follows:

    <li>Question1 </li>
  <br /><form>A.     
    <input type="radio" name="radio" id="1ARadioButton" value="1ARadioButton" />
    <label for="1ARadioButton">Answer1</label><br /><br />
  B.    
    <input type="radio" name="radio" id="1BRadioButton" value="1BRadioButton" />
    <label for="1BRadioButton"> Answer2</label>
    </form><br /><br />
  <li>Question2</li>
  <br /><form>A. 
    <input type="radio" name="radio" id="2ARadioButton" value="2ARadioButton" />
    <label for="2ARadioButton">Answer1</label>
     <br /><br />
  B.     
    <input type="radio" name="radio" id="2BRadioButton" value="2BRadioButton" />
    <label for="2BRadioButton">Answer2</label>
     </form><br /><br />
  <li>Question3</li>
  <br /><form>A.     
    <input type="radio" name="radio" id="3ARadioButton" value="3ARadioButton" />
    <label for="3ARadioButton">Answer1</label>
     <br /><br />
   B.    
    <input type="radio" name="radio" id="3BRadioButton" value="3BRadioButton" />
    <label for="3BRadioButton">Answer2</label>
     </form><br /><br />


You've got the same name on all your radio buttons, so you're only going to get the value of the LAST radio button that's checked off. You're going to be recording the same answer for ALL questions (which is usually C, right?).

element IDs are NOT used for form submissions. They're used only for DOM operations. On HTML forms, only the name and value type attributes are relevant for the form submission process.

What you should have is:

Question 1:
    <input type="radio" name="question1" value="option_A" />
    <input type="radio" name="question1" value="option_B" />
Question 2:
    <input type="radio" name="question2" value="option_A" />
    <input type="radio" name="question2" value="option_B" />
etc...

As for storing them in the database, that's the same as storing any other form-data in a database. Get the radio's value with $_POST['question1'] or whatever, and do the usual escaping/query building/inserting.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号