I have a form and I want to insert the value of this form as soon as someone clicks a radio button. No better excuse than that I am not good at jQuery at all. This is what I've got so far.
HTML:
<form>
<input type="radio" checked="true" id="q1r1" name="q1" value="Awesome">
<label class="button1" for="q1r1">Awesome</label>
<input type="radio" id="q1r2" name="q1" value="Ok">
<label class="button2" for="q1r2">Ok</label>
<input type="radio" id="q1r3" name="q1" value="Awful">
<label class="button3" for="q1r3">Awful</label>
</form>
get.JSON:
$('#q1r1').on('click',function(){
var get_var_name = $(this).val();
$.getJSON('json/question1.php?team=<?php echo $teamid ?>',{q1:get_var_name},function(data,status){
console.log(data);
if(status == 'success'){
console.log(data);
} else {
alert('Status: ' + status);
}
});
});
...json/question1.php:
<?php
require('db_connect.php');
session_start();
$uid = $_SESSION['uid'];
if($_GET['q1']) {
$teamid = $_GET['team'];
$insertq1 = $_GET['q1'];
$sql = "UPDATE result
SET q1='$insertq1'
WHERE userid='$uid' AND teamid='$teamid' ";
$result = $mysqli->query($sql);
}
?>
As you probably see, I'm really no good at this at all, but this is what I had planned to happen:
- The logged in user selects an option, Awesome, OK or Awful.
- Without the page refreshing or a submit button being clicked, the get.JSON will automatically nudge the question1.php with the values teamid (saved in $teamid) and q1 (the value of the users choice).
- The php file silently updates the users database with the values, everybody happy.
Thing is that nothing happens when I click on "Awesome" as of right now, and I don't know why. I've been trying to follow multiple guides, but I'm just not sure how to go about solving this one. Any advice or help is much appreciated.
Kind regards, Mo.
P.s!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
is included.