dongshi1869
dongshi1869
2016-01-05 16:02

使用AJAX使用按钮将表单信息发送到另一个页面

  • php
  • mysql
  • ajax
  • javascript
  • jquery

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)

<?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));
} 
}
?>

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}

<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>

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条回答

为你推荐

换一换