I have this questionnaire that I'm currently working on where users are given four answer alternatives to answer. Each answer is assigned a radio button, and the radio buttons have a value from a database table (from 1-10). This way different alternatives have different values. The user can also go back and forth between questions.
After going through eight questions the test taker will see a button which replaces the next and back buttons. When the user clicks it, it should submit an AJAX request and send the form values to a database.
My problem that I'm facing is that the form values are not being sent to my database when I click the button. I've tested to see and the form displays answers, and the radio buttons have values, so the problem lies with the SubmitAjax button not sending the values.
Why isn't it working?
Table question: contains answer alternatives and point values
qid(int), answer(varchar), point(int)
Table result: values that should be sent with the AJAX function
qid(int), point(int)
PHP/AJAX
<html>
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
function goBack() {
window.history.go(-1);
}
$("#submitAjax").click(function(){
if($('.radiobtn').is(':checked')) {
var radiobtn = $('.radiobtn:checked').val();
var qid = $('#qid').val();
$.ajax(
{
type: "GET",
url: 'login.php',
dataType: "json",
data: "radiobtn="+radiobtn+"&qid="+qid,
success: function (response) {
if(response.status == true){
alert('points added');
}
else{
alert('points not added');
}
}
});
return false;
}
});
</script>
</head>
<body>
<?php
$localhost = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($localhost, $username, $password) or die ("Kunde inte koppla");
mysqli_select_db($connect, 'wildfire');
if(count($_GET) > 0){
$answerPoint = intval($_GET['radiobtn']);
$qid = intval($_GET['qid']);
$sql2 = "INSERT INTO result (qid, points) VALUES ($answerPoint, $qid)";
$connect->query($sql2);
$lastid = $connect->insert_id;
if($lastid>0)
echo json_encode(array('status'=>1));
else
echo json_encode(array('status'=>0));
}
$qid = 1;
if(count($_POST) > 0){
$qid = intval($_POST['qid'])+1;
}
?>
<form method="post" action="">
<input type="hidden" name="qid" id="qid" value="<?=$qid?>">
<?php
$sql1 = mysqli_query($connect,"SELECT * FROM question where answer != '' && qid =".intval($qid));
while($row1=mysqli_fetch_assoc($sql1)){
?>
<input type='radio' name='answer1' class="radiobtn" value="<?php echo $row1['Point'];?>">
<?php echo $row1['answer'];?><br>
<?php
}
?>
<?php if ($qid <= 8) { ?>
<button type="button" onclick="history.back();">Tillbaka</button>
<button type="submit">Nästa</button>
<?php } else { ?>
<button id="submitAjax" type="submit">Avsluta provet</button>
<?php } ?>
</form>
<?php
if(count($_GET) > 0){
$answerPoint = intval($_GET['radiobtn']);
$qid = intval($_GET['qid']);
$sql2 = "INSERT INTO result (points,qid) VALUES ($answerPoint, $qid)";
$connect->query($sql2);
$lastid = $connect->insert_id;
if($lastid>0)
echo json_encode(array('status'=>1));
else
echo json_encode(array('status'=>0));
}
?>
</body>
</html>
$.ajaxis expectingjsonback -dataType: "json",, but when you do an$.ajaxback to this same page, it is going to send ALL of you html code, including the<html><head>..., which is invalidjson. So you either need to split this into multiple pages of code, or use php to check if you are doing an ajax call and only display thejson.