试图使我的按钮连接到AJAX脚本工作

时间:2016-01-05 09:39:34

标签: php jquery mysql ajax forms

您好我有两个应该互相连接的文件。我想向另一个使用sql查询发送表单信息的页面发送一个AJAX请求。

我正在尝试创建的应用程序是一个包含八个不同问题的问卷,每个问题有四个答案,每个答案都有一个来自数据库的值。回答完所有八个问题后,您将看到一个按钮,它向页面test.php提交了一个AJAX请求,(名为submitAJAX)。

问题是请求没有通过,表单中的值没有发送到我的数据库。

表单页

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
<?php
$localhost = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($localhost, $username, $password) or die ("Kunde inte koppla");
mysqli_select_db($connect, 'wildfire');

$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'];?>">
<input type='hidden' name='tid' class="tid" value="<?php echo $row1['tid'];?>">
<?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>

<script src="https://code.jquery.com/jquery-1.11.3.js"></script> 
  <script type="text/javascript">
function goBack() {
    window.history.go(-1);
}
$(document).ready(function(){ 
$("#submitAjax").click(function(){
        if($('.radiobtn').is(':checked')) { 
            var radiobtn = $('.radiobtn:checked').val();
            var qid = $('#qid').val();            
             var answer = $('input[name=answer1]:radio').val();
            $.ajax(
            {
                type: "GET",
                url: 'test.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>

    </body>

test.php的

<?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']);
$tid = intval($_GET['tid']);
   $sql2 = "INSERT INTO result (qid, points, tid) VALUES ($qid, $answerPoint, $tid)";
   $connect->query($sql2); 
   $lastid = $connect->insert_id;
   if($lastid>0) {
    echo json_encode(array('status'=>1));
}
   else{
    echo json_encode(array('status'=>0));
} 
}
?>

我已经检查了控制台日志,以及inspect元素中的网络设置,但我没有收到任何错误。 目前,当我查看test.php文件时,收到此消息{"status":0} 如果我得到{"status":1},那么请求将被发送到php文件,值将被发送到我的数据库。

为了将表单值发送到我的数据库,我需要修复我的代码有什么问题吗?

0 个答案:

没有答案