如何使用php从html形式的多个单选按钮中获取选定的值


How to get the selected values from multiple radio buttons in html form using php

我是PHP新手。我正在开发一个在线考试系统。在HTML页面中,我安排了问题,为了选择答案,我使用了四个单选按钮作为4个选项。所有20个问题也是如此。现在这个问题是。我想从单选按钮中获得选定的值,我想将其存储在avariable中,我需要计算分数I。没有正确的选项。最后我想把它存储到数据库中。有人能给我做这件事的示例代码吗。。。这是我的html代码

<html>
<head>
<body>
<font size=+1 color=blue><u><h1>C Language exam</h1></u></font><p>
<form  method="POST" action="validate1.php">
<pre>
<font face="verdana">

1.In C, if you pass an array as an argument to a function, what actually gets passed?
<p>
<input type=radio name=1 value=a>A. Value of elements in array<br>
<input type=radio name=1 value=b>B. First element of the array<br>
<input type=radio name=1 value=c>C. Base address of the array<br>
<input type=radio name=1 value=d>D. Address of the last element of array<br>

2.How will you print 'n on the screen?<p>
<input type=radio name=2 value=a>A. printf("'n");<br>
<input type=radio name=2 value=b>B. echo "''n";<br>
<input type=radio name=2 value=c>C. printf(''n');<br>
<input type=radio name=2 value=d>D. printf("''n");<br>

3.C  is what type of language?<p>
<input type=radio name=3 value=a>A. Low leval language<br>
<input type=radio name=3 value=b>B. high level language<br>
<input type=radio name=3 value=c>C. middle level language<br>
<input type=radio name=3 value=d>D. medium language<br>

4.Integer type char storage size.<p>
<input type=radio name=4 value=a>A. 2 bytes<br>
<input type=radio name=4 value=b>B. 1 byte<br>
<input type=radio name=4 value=c>C. 4 byte<br>
<input type=radio name=4 value=d>D. 8 bytes<br>

5.What is the output of given program
#include <stdio.h>  
int main()
 {    /* my first program in C */    
printf("Hello, World! 'n");      
  return 0;
 }<p>
<input type=radio name=5 value=a>A. first program<br>
<input type=radio name=5 value=b>B. world<br>
<input type=radio name=5 value=c>C. hello world<br>
<input type=radio name=5 value=d>D. hello<br>

6.How many times the program will print "welcome" ?
#include<stdio.h>
int main()
{
    printf(“welcome”);
    main();
    return 0;
}<p>
<input type=radio name=6 value=a>A. Infinite times<br>
<input type=radio name=6 value=b>B. 32767 times<br>
<input type=radio name=6 value=c>C. 65535 times<br>
<input type=radio name=6 value=d>D. Till stack overflows<br>

7.Symbol of LOGICAL AND operation<p>
<input type=radio name=7 value=a>A. &<br>
<input type=radio name=7 value=b>B. &&<br>
<input type=radio name=7 value=c>C. ><br>
<input type=radio name=7 value=d>D. == <br>

8.What are the different types of real data type in C ?<p>
<input type=radio name=8 value=a>A. float, double<br>
<input type=radio name=8 value=b>B. short int, double, long int<br>
<input type=radio name=8 value=c>C. short int, double, long int<br>
<input type=radio name=8 value=d>D. double, long int, float<br>

9.Which statement will you add in the following program to work it correctly?
#include<stdio.h>
int main()
{
    printf(“%f'n”, log(36.0));
    return 0;
}<p>
<input type=radio name=9 value=a>A. #include<conio.h><br>
<input type=radio name=9 value=b>B. #include<math.h><br>
<input type=radio name=9 value=c>C. #include<stdlib.h><br>
<input type=radio name=9 value=d>D. #include<dos.h><br>

10.What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i && ++j || ++k;
    printf(“%d,%d,%d,%d'n”, i, j, k, m);
    return 0;
}<p>
<input type=radio name=10 value=a>A.  1,2,0,1<br>
<input type=radio name=10 value=b>B. -3,2,0,1<br>
<input type=radio name=10 value=c>C. -2,3,0,1<br>
<input type=radio name=10 value=d>D.  2,3,1,1<br>

11.C closely associated with the which operating system?<p>
<input type=radio name=11 value=a>A. windows<br>
<input type=radio name=11 value=b>B. linux<br>
<input type=radio name=11 value=c>C. unix<br>
<input type=radio name=11 value=d>D. mac<br>

12.C was developed at Bell Laboratories in which year?<p>
<input type=radio name=12 value=a>A. 1943<br>
<input type=radio name=12 value=b>B. 1954<br>
<input type=radio name=12 value=c>C. 1972<br>
<input type=radio name=12 value=d>D. 1975<br>

13.In the following code, the P2 is Integer Pointer or Integer?
Typedef int *ptr;
ptr p1, p2;<p>
<input type=radio name=13 value=a>A. Integer<br>
<input type=radio name=13 value=b>B. Integer pointer<br>
<input type=radio name=13 value=c>C. Error in declaration<br>
<input type=radio name=13 value=d>D. None of above<br>

14.In the following code what is 'P'?
Typedef char *charp;
Const charp P;<p>
<input type=radio name=14 value=a>A. P is a constant<br>
<input type=radio name=14 value=b>B. P is a character constant<br>
<input type=radio name=14 value=c>C. P is character type<br>
<input type=radio name=14 value=d>D. None of above<br>

15.What is the similarity between a structure, union and enumeration?<p>
<input type=radio name=15 value=a>A. All of them let you define new values<br>
<input type=radio name=15 value=b>B. All of them let you define new data types<br>
<input type=radio name=15 value=c>C. All of them let you define new pointers<br>
<input type=radio name=15 value=d>D. All of them let you define new structures<br>

16.How will you free the allocated memory ?<p>
<input type=radio name=16 value=a>A. remove(var-name);<br>
<input type=radio name=16 value=b>B. free(var-name);<br>
<input type=radio name=16 value=c>C. delete(var-name);<br>
<input type=radio name=16 value=d>D. dalloc(var-name);<br>

17.Which header file should be included to use functions like malloc()  and  calloc()?<p>
<input type=radio name=17 value=a>A. memory.h<br>
<input type=radio name=17 value=b>B. stdlib.h<br>
<input type=radio name=17 value=c>C. string.h<br>
<input type=radio name=17 value=d>D. dos.h<br>

18.Specify the 2 library functions to dynamically allocate memory?<p>
<input type=radio name=18 value=a>A. Malloc() and memalloc()<br>
<input type=radio name=18 value=b>B. calloc() and memalloc()<br>
<input type=radio name=18 value=c>C. Malloc() and calloc()<br>
<input type=radio name=18 value=d>D. alloc() and calloc()<br>

19.What are the types of linkages?<p>
<input type=radio name=19 value=a>A. Internal and External<br>
<input type=radio name=19 value=b>B. External, Internal and None<br>
<input type=radio name=19 value=c>C. External and None<br>
<input type=radio name=19 value=d>D. Internal<br>

20.who created the new system's first higher-level language?<p>
<input type=radio name=20 value=a>A. McClure's<br>
<input type=radio name=20 value=b>B. McIlroy<br>
<input type=radio name=20 value=c>C. Thompson's <br>
<input type=radio name=20 value=d>D. Doug McIlroy<br>

<input type="submit" value="submit">
</font>
</pre>
</body>
</html>

从表单接收值

一般样本

$value = $_POST['name'];

适合您情况的特定代码
由于您有18个输入,全部以数字命名,因此您可以使用:

for($i=1;$i<19;$i++){
$value[$i] = $_POST[$i];
//$value[1] = $_POST['1']
}

检查值

一般样本

if($value=="correctanswer"){$correct++;}

适合您情况的特定代码

$correctanswer[1] = "correctanswer1"; //correct answer for input with name 1
$correctanswer[2] = "correctanswer2";
//Do this for every input
for($i=1;$i<19;$i++){
if($value[$i]==$correctanswer[$i]){$correct++;}
}

在数据库中存储$correct

假设您有一个名为"correct"的表,列为"number":

$connect = mysqli_connect("localhost","root","","db");
if(!$connect){echo "Couldn't connect.";}
$query = "INSERT INTO correct(number) VALUES(".$correct.")";
$execute = mysqli_query($connect,$query);
if(!$execute){echo "Couldn't execute query. Please open PhpMyAdmin and try to execute the following query: <br>".$query." <br>. If it works, the problem is in the PHP code (or there was a temporary error); otherwise, there's a mistake in the query: please post it as a comment to my answer.";}