kangjacob 2021-08-08 17:50 采纳率: 96.8%
浏览 47
已结题

为什么表格提交之后,处理页面没有获得部分变量?

下面是提出问题的页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>中医九种体质问卷</title>
</head>
<?php
$servername = "--";
$username = "--";
$password = "-";
$dbname = "---";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}else{echo '连接成功!<br>';}
//firstly we should make sure the password is right?
$userName=$_COOKIE['userName'];
$userPassword=$_COOKIE['userPassword'];
$sql = "SELECT * FROM user_table WHERE userName='$userName' and userPassword='$userPassword'";//php的双引号里面可以放变量
$result = $conn->query($sql);
if ($result->num_rows>0) {
    setcookie('cautionWord','健康开心每一天');
 } else {
    setcookie('cautionWord','用户名或密码有误');
    header("location:./loginPage.php");
}
//make sure the time of test

$row=$result->fetch_assoc();
$userSex=$row['userSex'];
$testTime=$row['testTime']+1;//this is one more time
echo $userName.'<br>';
echo $userSex.'<br>';
echo '这是您的第'.$testTime.'次测试';

//then we can go on
$currentQuestionNumber=$_COOKIE['questionNumber'];
if($currentQuestionNumber != Null||$currentQuestionNumber >=1){
$i=$currentQuestionNumber;
}else{
$i=1;
setcookie('questionNumber',$i);//record how many questions we have finished.

}
$sql = "SELECT * FROM the9kinds WHERE id=$i";//php的双引号里面可以放变量
$result = $conn->query($sql);
?>
<body>
<form  action="storeData.php" method="get" onsubmit="return questionForward()">

    <?php
    $testUser=$_COOKIE['userName'];//read userName from cookies
    if ($result->num_rows > 0) {
        // 输出数据
        while($row = $result->fetch_assoc()) {
            echo  $row["question"]. "?<br>";
            ?>

            <input  type='radio' name='answer'  value='<?php echo $row["nothing"]?>'> 从来没有 <?php //echo  $row["nothing"]?><br>
            <input  type='radio' name='answer'  value='<?php echo $row["alittle"] ?>'> 很少的时候 <?php //echo $row["alittle"]?><br>
            <input  type='radio' name='answer'  value='<?php echo $row["sometimes"] ?>'>有些时候<?php //echo $row["sometimes"]?> <br>
            <input  type='radio' name='answer'  value='<?php echo $row["often"] ?>'> 经常这样 <?php //echo $row["often"]?><br>
            <input  type='radio' name='answer'  value='<?php echo $row["always"] ?>'>总是这样 <?php //echo $row["always"]?><br>
            <?php
        }
    } else {
        echo "0 结果";
    }

    ?>
    <input type="hidden" name="userSex" value="<?php echo $userSex ?>"
    <input type="hidden" name="testTime" value="<?php echo $testTime?>"
    <input type="hidden" name="testUser" value='<?php echo $testUser ?>'>
    <input type="hidden" name="questionOrder" value='<?php echo $i ?>'>
    <input type='button' value="上一题" onclick="questionBack()" <?php if($i==1){?>style="VISIBILITY: hidden"<?php } ?>><!--这里做了设定,避免呢超出范围-->
    <input type="submit" value="提 交"  <?php if($i>67){?>style="VISIBILITY: hidden"<?php } ?>><!--这里做了设定,避免超出范围-->
</form>
<script>
    function questionBack(){

        var x=document.getElementsByName("questionOrder")[0];
        var i=x.value;
        var reduceQuestionNumber=Number(i)-1;
        alert(reduceQuestionNumber);
        document.cookie='questionNumber='+String(reduceQuestionNumber);//这里不使用string()也可以,但主要是提醒自己string的转换函数
        window.location.reload();//只有submit按钮有自动刷新的功能,其他的按钮没有这个功能,所以要加上这个刷新语句
    }

    function questionForward(){
        var theRadioInputs = document.getElementsByName('answer');
        var radioIsChecked = 0;
        //heck radio is checked
        for(let m=0;m<theRadioInputs.length;m++)
        {
            if(theRadioInputs[m].checked){
                radioIsChecked = 1;
            }
            }
        if(radioIsChecked==1){
            var x=document.getElementsByName("questionOrder")[0];
            var i=x.value;
            alert(i);
            var addQuestionNumber=Number(i)+1;
            //alert(addQuestionNumber);
            document.cookie='questionNumber='+String(addQuestionNumber);//这里不使用string()也可以,但主要是提醒自己string的转换函数
            //check radio which shoulb be checked before we submit
            return true;
        }else{
            alert('you should check one radio at least');
            return false;
        }


    }

</script>
<?php
$conn->close();
?>
</body>

</html>




下面是处理问题的页面:

<?php
echo '用户名'.$_GET['testUser'].'<br>';
echo '第'.$_GET['questionOrder'].'题';
echo '答案:'.$_GET['answer'].'<br>';
echo '您是第'.$_GET['testTime'].'次测试';
?>
<?php
$servername = "--";
$username = "--";
$password = "--";
$dbname = "---";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}else{echo '连接成功!<br>';}
?>
<?php
$loginName=$_GET['testUser'];
$questionOrder=$_GET['questionOrder'];
$questionAnswer=$_GET['answer'];
$testTime=$_GET['testTime'];
$userSex=$_GET['userSex'];
//$conn->query('set names utf8;');
/*
$sql="INSERT INTO answer_table (userName,questionOrder,questionAnswer,testTime)VALUES('$loginName','$questionOrder','$questionAnswer','$testTime')";
if($result = $conn->query($sql)){  //nn ->query is important
    echo '成功录入';
    if($userSex=='male' and $questionOrder==37){
        header('location:./corePage.php');
    }
    if($userSex=='female' and $questionOrder==38){
        header('location:./corePage.php');
    }
    if($questionOrder==67){
        header('location:./resultPage.php');
    }else{
        header('location:./corePage.php');
    }
}else{
    echo 'please insert again';
}
$sql="INSERT INTO user_table(testTime)VALUES($testTime)WHERE userName='$loginName'";//insert the testTime
$result = $conn->query($sql);
*/
$conn->close();

?>





问题是处理页面无法获取下面的内容:

echo '用户名'.$_GET['testUser'].'<br>';
echo '第'.$_GET['questionOrder'].'题';
echo '答案:'.$_GET['answer'].'<br>';
echo '您是第'.$_GET['testTime'].'次测试';

以及


$userSex=$_GET['userSex'];
  • 写回答

1条回答 默认 最新

  • kangjacob 2021-08-08 18:47
    关注

    自己找到问题了,原来是type=‘hidden'的元素写错了,末尾少了‘>'

    评论

报告相同问题?

问题事件

  • 系统已结题 8月16日
  • 创建了问题 8月8日