PHP 2021 Old paper Solution
Q.no 11
1 2 3 4 5 6 7 8 |
<?php function ascendingOrder($arr){ sort($arr); foreach($arr as $value){ echo $value . " "; } } ?> |
Q.no 12
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 |
<html> <head> <title>Form Validation</title> <script> function validateForm() { var x = document.forms["form_data"]["name"].value; if (x == "") { alert("Name must be filled out"); return false; } var y = document.forms["form_data"]["age"].value; if (isNaN(y) || y < 1 || y > 100) { alert("Age must be a number between 1 and 100"); return false; } var z = document.forms["form_data"]["comments"].value; if (z == "") { alert("Please enter a comment"); return false; } } </script> </head> <body> <form name="form_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return validateForm()"> Name: <input type="text" name="name"><br><br> Age: <input type="text" name="age"><br><br> Comments: <textarea name="comments"></textarea><br><br> <input type="submit" value="Submit"> </form> </body> </html> |
Q.no 13
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 |
<?php // Assumption: You have a database table called "subjects" // with 3 columns: "subject_code", "subject_description", "credit" // Create connection $conn = new mysqli("localhost", "username", "password", "databaseName"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // insert data into subjects table $sql = "INSERT INTO subjects (subject_code, subject_description, credit) VALUES ('ENG101', 'English Composition', '3')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
Q.no 14
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 |
<?php if(isset($_POST['submit'])) { $name = $_POST['name']; $age = $_POST['age']; $gender = $_POST['gender']; $file = fopen("Person.txt", "a") or die("Unable to open file!"); fwrite($file, $name.",".$age.",".$gender. "\n"); fclose($file); echo "Data Saved successfully!"; } ?> <html> <head> <title>Person Form</title> </head> <body> <form action="" method="POST"> Name: <input type="text" name="name"/><br/> Age: <input type="text" name="age"/><br/> Gender: <input type="radio" name="gender" value="Male"/>Male <input type="radio" name="gender" value="Female"/>Female <br/> <input type="submit" name="submit" value="Submit"/> </form> </body> </html> |
Q.no 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // Start the session session_start(); // Set Session Variables $_SESSION['name'] = 'John'; $_SESSION['age'] = 25; $_SESSION['city'] = 'New York'; // Retrieve Session Variables echo "My name is " . $_SESSION['name'] . "<br>"; echo "My age is " . $_SESSION['age'] . "<br>"; echo "My city is " . $_SESSION['city'] . "<br>"; ?> |
Continue Reading