mramerica Posted September 6, 2013 Share Posted September 6, 2013 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); } ?> Quote Link to comment Share on other sites More sharing options...
denno020 Posted September 7, 2013 Share Posted September 7, 2013 You will need to use javascript and ajax. In your javascript, you add an event listener so as soon as a radio button is selected, it will trigger the ajax, which will run your php script and insert the data into your database. Denno Quote Link to comment Share on other sites More sharing options...
kicken Posted September 7, 2013 Share Posted September 7, 2013 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> Quote Link to comment Share on other sites More sharing options...
Irate Posted September 7, 2013 Share Posted September 7, 2013 Also, I want to point this out - there cannot be two documents with the same ID in one document. Your SQL query is also missing delimiting double quotes and a semi-colon at the end of the variable declaration. Quote Link to comment Share on other sites More sharing options...
mramerica Posted September 9, 2013 Author Share Posted September 9, 2013 Ok, thanks. I will give these options a try and see how it works out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.