Jump to content

Submit data once radio button is clicked


mramerica

Recommended Posts

Hello,

 

I'm trying to create a short quiz that basically has 1 question per screen and uses radio buttons for the answer choices.

I would like for the user to be able to make their answer selection and once the radio button is clicked their chosen answer value, as well as other data I'm collecting is inserted into the database.

However, I'm having trouble getting this to work properly. (I would prefer not to use a submit button on the page)

<form method="POST" action="<?php $php_self; ?>
Question: True/False - The sky is blue</br>
<input type='radio ' value='yes' id='answer' name='answer' onclick='correct_response();this.form.submit();document.location.href=\"next_question"\'> Yes</br>
<input type='radio ' value='no' id='answer' name='answer' onclick='correct_response();document.location.href=\"next_question"\'>No</br>
</form>

Here is what I'm hoping to do to insert the data into the database:

<?php
if(isset($_POST['radio'])){

$answer = $_POST['answer'];

$sql = "INSERT INTO user_answers('id', 'answer', 'question', 'date_created', 'date_modified')VALUES('NULL','$answer','date();', 'date();')
mysql_query($sql);
}
?>
Link to comment
Share on other sites

Unless you want to prevent the entire page reloading and just swap out the questions, ajax is not required. Javascript is however to trigger the submit. Just a simple onclick that calls the forms submit function is all you need.

 

<form method="POST" action="<?php $php_self; ?>
Question: True/False - The sky is blue</br>
<input type='radio ' value='yes' id='answer' name='answer' onclick='this.form.submit();'> Yes</br>
<input type='radio ' value='no' id='answer' name='answer' onclick='this.form.submit();'>No</br>
</form>
Link to comment
Share on other sites

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.