2016-01-05 16:02
浏览 154


Hello I have two files that are supposed to be connected to one another. I want to send an AJAX request to another page that uses a sql query to send form information.

The application that I'm trying to create is a questionnaire with eight questions, each questions has four answers paired together with the same id (qid) and each answer has a value from the database. After you answer eight questions you will see a button that sends an AJAX request to the page test.php, (named submitAJAX).

The problem is that although my connection with AJAX is working, the values from the form are not being sent to my database. Previously I thought that the problem may lie with the form page, but now I I think the problem lies in this file:

test.php (file with json)

$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)";
   $lastid = $connect->insert_id;
   if($lastid>0) {
    echo json_encode(array('status'=>1));
    echo json_encode(array('status'=>0));

I think that the problem may lie in the row where: if($lastid>0) { $lastid should always be more than 0, but whenever I check test.php I get this message: {"status":0} What's intended is that I get this message: {"status":1}

        <meta charset="utf-8">
$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?>">
$sql1 = mysqli_query($connect,"SELECT * FROM question where answer != '' && qid =".intval($qid));
<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 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 } ?>  

<script src=""></script> 
  <script type="text/javascript">
function goBack() {
        if($('.radiobtn').is(':checked')) { 
            var radiobtn = $('.radiobtn:checked').val();
            var qid = $('#qid').val();            
             var answer = $('input[name=answer1]:radio').val();
                type: "GET",
                url: 'test.php',
                dataType: "json",
                data: "radiobtn="+radiobtn+"&qid="+qid,
                success: function (response) {
                    if(response.status == true){
                        alert('points added');
                        alert('points not added');   

            return false;


The values that I want to send to my database from test.php are:

qid(int), tid(int), Point(int)

There is a database connection, and my test.php file's sql query should work, but its not sending form information. Is there something that I need to rewrite or fix to make it work?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • druhoytza979667566 2016-01-05 16:12

    First, your data parameter in the AJAX call is not using the correct syntax. You're missing brackets. It should look like:

    data: JSON.stringify({ radiobtn: radiobtn, qid: qid }),

    Second, I'd suggest using POST instead of GET:

    type: "POST",

    which means that you need to look for your data in $_POST['radiobtn'] and $_POST['qid'] on test.php. NOTE: you should check for the key you expect using isset() before assigning the value to a variable, like so:

    $myBtn = isset($_POST['radiobtn']) ? $_POST['radiobtn'] : null;

    Third, for testing, use a console.log() inside your condition that checks for the checkbox being checked in order to verify that condition is working as expected.

    if($('.radiobtn').is(':checked')) {


    Fourth: You should specify the content type in your AJAX call, like so:

    contentType: "application/json; charset=utf-8",
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题