如何通过带有json的ajax获得价值?

时间:2018-10-09 01:35:36

标签: php json ajax

您好朋友,这是我关于stackoverflow的第一个问题,我在Json中面临的问题是无法使用Ajax。我不明白,我不明白哪里出了问题。 Myb代码如下... 我有action.php文件 如果我在没有json的情况下尝试运行,但是我在使用json则无法获得任何响应

<?php 
 $con = new mysqli("localhost", "root", "", "psycho"); /* REPLACE NECESSARY DATA INSIDE */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
 exit();
} 
if($stmt = $con->prepare("SELECT qid, question, opt1, opt2, opt3, opt4 FROM question ORDER BY qid LIMIT 1")){
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($qid, $question, $opt1, $opt2, $opt3, $opt4); /* BIND THE RESULT TO THESE VARIABLES */
$stmt->fetch(); /* FETCH THE RESULT */
$stmt->close();
} /* END OF PREPARED STATEMENT */
?>
<h1 id="question"><?php echo $question; ?></h1>
<input type="hidden" id="qid" value="<?php echo $qid; ?>">
<input type="radio" name="a1" id="op1" value="<?php echo $opt1; ?>"><span id="op1text"><?php echo $opt1; ?></span><br/>
<input type="radio" name="a1" id="op2" value="<?php echo $opt2; ?>"><span id="op2text"><?php echo $opt2; ?></span><br/>
<input type="radio" name="a1" id="op3" value="<?php echo $opt3; ?>"><span id="op3text"><?php echo $opt3; ?></span><br/>
<input type="radio" name="a1" id="op4" value="<?php echo $opt4; ?>"><span id="op4text"><?php echo $opt4; ?></span><br/>
<input type="submit" name="submit" id="submit" value="Next"> <!-- THIS SERVES AS THE SUBMIT AND NEXT BUTTON -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
 $(document).ready(function(){
  $("#submit").click(function(){ /* WHEN SUBMIT IS CLICKED */
  var qid = $("#qid").val(); /* GET THE question id */
  var selected = $("input[type='radio'][name='a1']:checked");
  if (selected.length > 0) { /* CHECK THE SELECTED RADIO BUTTON */
    answer = selected.val();
  }
  $.ajax({
    type: "POST", /* THE METHOD WE WILL BE USING TO PASS THE DATA */
    url: "action.php", /* THIS IS WHERE THE DATA WILL GO */
    data: {"questionid" : qid, "answer" : answer}, /* THE DATA WE WILL BE PASSING */
    dataType : 'json',
    success: function(result){ /* WHEN IT IS SUCCESSFUL */
      /* THIS WILL REPLACE THE DATA IN OUR QUESTION PAGE */
      $("#qid").val(result.questionid);
      $("#question").html(result.question);
      $("#op1").val(result.op1);
      $("#op2").val(result.op2);
      $("#op3").val(result.op3);
      $("#op4").val(result.op4);
      $("#op1text").html(result.op1);
      $("#op2text").html(result.op2);
      $("#op3text").html(result.op3);
      $("#op4text").html(result.op4);
    }
  }); /* END OF AJAX */
});
});
 </script>

我的第二个文件代码在这里 action.php

<?php
 if(isset($_POST["questionid"])){
/* INCLUDE OUR NEW ESTABLISHED CONNECTION HERE */
/* PUT HERE YOUR INSERT QUERY WHICH STORES THE USER'S ANSWERS */

/* THEN FETCH THE NEXT QUESTION */
if($stmt = $con->prepare("SELECT qid, question, opt1, opt2, opt3, opt4 FROM question WHERE qid > ? ORDER BY qid LIMIT 1")){
  $stmt->bind_param("i", $_POST["questionid"]);
  $stmt->execute();
  $stmt->bind_result($qid, $question, $opt1, $opt2, $opt3, $opt4);
  $stmt->fetch();
  $stmt->close();
} /* END OF PREPARED STATEMENT */

/* THIS SET OF DATA WILL REPLACE THE DATA IN OUR CURRENT QUESTION PAGE */
echo json_encode(array("questionid" => $qid, "question" => $question, "op1" => $opt1, "op2" => $opt2, "op3" => $opt3, "op4", => op4));
   } /* END OF ISSET */
?>

2 个答案:

答案 0 :(得分:2)

您应包含以下代码,以更改标题中的数据类型。

header('Content-Type: application/json');

将其添加到PHP代码后,JQuery将能够读取JSON响应,这是因为,除非指定此内容类型,否则PHP会自然响应HTML的内容类型,除非在其他地方指定。

答案 1 :(得分:-1)

使用此代码:

InputBox