Php 我无法使用ajax将radio的价值发送到另一个页面
我想使用ajax在另一个页面上发送单选按钮的值,但我无法做到这一点Php 我无法使用ajax将radio的价值发送到另一个页面,php,ajax,Php,Ajax,我想使用ajax在另一个页面上发送单选按钮的值,但我无法做到这一点 <?php session_start(); $i=1; if(isset($_SESSION['email'])) { include('config.php'); $response=mysql_query("select * from questions where subject='".$_SESSION['subject']."'");
<?php
session_start();
$i=1;
if(isset($_SESSION['email']))
{
include('config.php');
$response=mysql_query("select * from questions where subject='".$_SESSION['subject']."'");
?>
<div id="page-wrapper">
<div class="col-md-12 graphs">
<div class="xs">
<form method="post" id="quiz_form">
<?php while($result=mysql_fetch_array($response)){ ?>
<?php echo $i.".".$result['question'];?>
<div>
A:- <input type="radio" value="1" id="optiona_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
<label for="optiona_<?php echo $result['id'];?>" ><?php echo $result['optiona'];?></label>
<br/>
B:- <input type="radio" value="2" id="optionb_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
<label for="optionb_<?php echo $result['id'];?>" ><?php echo $result['optionb'];?></label>
<br/>
C:- <input type="radio" value="3" id="optionc_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
<label for="optionc_<?php echo $result['id'];?>" ><?php echo $result['optionc'];?></label>
<br/>
D:- <input type="radio" value="4" id="optiond_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
<label for="optiond_<?php echo $result['id'];?>" ><?php echo $result['optiond'];?></label>
<input type="radio" checked='checked' value="5" style='display:none' id='option_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
</div>
<?php
$i++;
}?>
<br />
<div class="text-center">
<input type="button" id="btn4" class="btn-success btn" value="Submit Quiz" />
</div>
</form>
<div id='result'>
</div>
</div>
</div>
</div>
<?php
include('adminfooter.php');
}
else {
$_SESSION['invalid']=='set';
header('location:login.php');
}
?>
<script>
$(document).ready(function(){
$("#btn4").click(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: $("form").serialize(),
success: function(msg) {
$("#quiz_form").addClass("hide");
$("#result").show();
$("#result").append(msg);
}
});
});
});
</script>
错误是,它没有在
$n
中获取值。它显示的是未定义的偏移量。与您前面的一个问题相同:请缩进/格式化您的代码…没有,我已更正了该问题,但现在问题出现在ajax.php文件中。@mapek。问题是您没有格式化代码,因此没有人能够(或想要)执行此操作为了理解你的代码。好的,我将格式化我的代码@mapek。我没有得到我的答案。请帮助。与你之前的一个问题相同的问题:请缩进/格式化你的代码…不,我已经纠正了这个问题,但现在问题出现在ajax.php文件中。@mapek。问题是你没有格式化你的代码,这样就没有人能够(或想要)了为了理解你的代码。好的,我将在mapek设置我的代码格式。我没有得到我的答案。请帮助。
<?php
session_start();
include('config.php');
$query = "select * from questions where subject='".$_SESSION['subject']."'";
$result=mysql_query($query) or die(mysql_error());
$right_answer=0;
$wrong_answer=0;
$unanswered=0;
$n=1;
while($response=mysql_fetch_array($result))
{
echo $response['answer'];//this showed the answer
echo $_POST["$n"];//this showed the radio button no of the answer clicked
if($response['answer']==$_POST["$n"])
{
$right_answer++;
}
else if($_POST["$n"]==5)
{
$unanswered++;
}
else
{
$wrong_answer++;
}
$n++;
}
echo "<div id='answer'>";
echo " Right Answer : ". $right_answer."</span><br>";
echo " Wrong Answer : ". $wrong_answer."</span><br>";
echo " Unanswered Question : ". $unanswered."</span><br>";
echo "</div>";
?>