【问题标题】:How to combine php in html or access the php script variable in html?如何在html中结合php或者访问html中的php脚本变量?
【发布时间】:2017-03-20 15:31:09
【问题描述】:

我在php中创建了两个页面,在一个页面中我存储了一个会话变量,我想在另一个页面中访问这个变量并显示根据单选按钮的值选中的单选按钮。

我正在获取 session 变量的值,存储在 php 变量中,现在我想显示选中的单选按钮,如果类型为 1,则 SSgt 类型的单选按钮将被选中,其他人也一样。

mcq.php

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MCQ Questions</title>
</head>
<body>

<?php
session_start();
//echo "type" . $_SESSION["type"] . ".<br>";

    $type = $_SESSION["type"];

<div id="types">

    if(strcmp($type,"1") == 0)
    {   
        SSgt <input name="type" type="radio" id="t2" value="SSgt" checked="checked">
    }
    else{
        SSgt <input name="type" type="radio" id="t2" value="SSgt">
    }

 TSgt <input name="type" type="radio" id="t1" value="TSgt"> 
 MSgt <input name="type" type="radio" id="t3" value="MSgt">
</div>
?>

<form method="post" action="uploadQuestion.php" enctype="multipart/form-data">
<p> Enter the question :</p> <input name="question" type="text"> <br><br>
Select question type : <br><br>



<p> Enter options :</p>
Enter option 1 : <input name="opt1" type="text"> <br><br>
Enter option 2 : <input name="opt2" type="text"> <br><br>
Enter option 3 : <input name="opt3" type="text"> <br><br>
Enter option 4 : <input name="opt4" type="text"> <br><br>

<p> Enter correct answer :</p>

<input name="ans" type="input"> <br><br>

<input type="submit" value = "Submit">

</form>
</body>
</html>

我是网络开发的初学者,谁能帮忙解决这个问题?谢谢。。

【问题讨论】:

    标签: php html variables


    【解决方案1】:

    这样做

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>MCQ Questions</title>
    </head>
    <body>
    
    <?php
    session_start();
    //echo "type" . $_SESSION["type"] . ".<br>";
    
        $type = $_SESSION["type"];
    ?>
    <div id="types">
    <?php
        if(strcmp($type,"1") == 0)
        {   
    ?>
     SSgt <input name="type" type="radio" id="t2" value="SSgt" <?=($type==1?"checked":"");?> >
    
     TSgt <input name="type" type="radio" id="t1" value="TSgt" <?=($type==2?"checked":"");?>> 
    
     MSgt <input name="type" type="radio" id="t3" value="MSgt" <?=($type==3?"checked":"");?>>
    </div>
    
    
    <form method="post" action="uploadQuestion.php" enctype="multipart/form-data">
    <p> Enter the question :</p> <input name="question" type="text"> <br><br>
    Select question type : <br><br>
    
    
    
    <p> Enter options :</p>
    Enter option 1 : <input name="opt1" type="text"> <br><br>
    Enter option 2 : <input name="opt2" type="text"> <br><br>
    Enter option 3 : <input name="opt3" type="text"> <br><br>
    Enter option 4 : <input name="opt4" type="text"> <br><br>
    
    <p> Enter correct answer :</p>
    
    <input name="ans" type="input"> <br><br>
    
    <input type="submit" value = "Submit">
    
    </form>
    </body>
    </html>
    

    【讨论】:

    • 500 错误主要由 .htaccess 发生,它不是我的代码。 :)
    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    相关资源
    最近更新 更多