简体   繁体   中英

How to combine php in html or access the php script variable in html?

I am creating two pages in php, in one page I am storing a session variable, this variable I want to access in another page and show the radio button checked according to the value of radio button.

I am getting the value of session variable, storing in the php variable, now I want to show the radio button checked, if type 1 then radio button of type SSgt will be checked and like that for others.

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>

I am a beginner in web development, can anyone help with this please? Thank you..

Do it in this way

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM