-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.php
83 lines (70 loc) · 2.02 KB
/
form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
$questionErr = $optionErr=$ansErr=$checkErr="";
$question = $check=$status="";
$option = array();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "examonline";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT categoryname from category";
$result = $conn->query($sql);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["question"])) {
$questionErr = "*Question is required";
} else {
$question = $_POST["question"];
}
if (empty($_POST["optradio"])) {
$checkErr = "*Please Select one option";
} else {
$check =$_POST["optradio"];
echo "<script type=\"text/javascript\">alert(\"adf\");</script>";
}
for($i=0;$i<4;$i++)
{
$y=$i+1;
if (empty($_POST["op$y"])) {
$optionErr = "*Options are required";
} else {
$option[$i] = $_POST["op$y"];
}
}
if($questionErr == $optionErr)
{
$option[2] = $_POST["op3"];
$option[3] = $_POST["op4"];
$sql = "INSERT INTO questions (question,op1,op2,op3,op4,answer)
VALUES ('$question', '$option[0]', '$option[1]', '$option[2]', '$option[3]','$check')";
if ($conn->query($sql) === TRUE) {
$status= "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
showCategory($row["categoryname"]);
}
} else {
echo "0 results";
}
function showCategory($data){
echo "<script>
var div = document.getElementById(\"category\");
var myData = div.textContent;
var para = document.createElement(\"option\");
var t = document.createTextNode(\"$data\");
para.appendChild(t);
document.getElementById(\"category\").appendChild(para);
</script>";
}
?>