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); } ?> Link to comment https://forums.phpfreaks.com/topic/281940-submit-data-once-radio-button-is-clicked/ 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 Link to comment https://forums.phpfreaks.com/topic/281940-submit-data-once-radio-button-is-clicked/#findComment-1448576 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> Link to comment https://forums.phpfreaks.com/topic/281940-submit-data-once-radio-button-is-clicked/#findComment-1448623 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. Link to comment https://forums.phpfreaks.com/topic/281940-submit-data-once-radio-button-is-clicked/#findComment-1448634 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. Link to comment https://forums.phpfreaks.com/topic/281940-submit-data-once-radio-button-is-clicked/#findComment-1448839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.