USER INTERFACE DESIGN AND DEVELOPMENT PROGRAMS

1. Working with AngularJS Expressions

Aim

To write an AngularJS program that uses expressions ({{ }}) and ng-init to display data in a web page.

Algorithm

  1. Start.
  2. Create an HTML page and include the AngularJS library using the <script> tag.
  3. Add the ng-app directive to a <div> to define the AngularJS application.
  4. Use ng-init to create an object sort with three properties: one, two and three.
  5. Display the values inside a list using the expressions {{ sort.one }}, {{ sort.two }} and {{ sort.three }}.
  6. Open the page in a browser and view the output.
  7. Stop.

Check result

Your code was correct. No syntax errors. (Only a note: it needs internet access to load AngularJS from the Google CDN.)

Code

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
  <title>AngularJS Expression</title>
</head>
<body>
  <h1 style="color:green">GeeksforGeeks</h1>
  <h3>AngularJS Expressions</h3>

  <div ng-app=""
       ng-init="sort={one:'quick sort', two:'merge sort', three:'bubble sort'}">
    <p>Sorting Algorithms:</p>
    <ul>
      <li>{{ sort.one }}</li>
      <li>{{ sort.two }}</li>
      <li>{{ sort.three }}</li>
    </ul>
  </div>
</body>
</html>

Output

GeeksforGeeks
AngularJS Expressions

Sorting Algorithms:

Result

The AngularJS expressions were evaluated and the three sorting algorithm names were displayed successfully in the list.

2. Working with AJAX

Aim

To write a program that uses AJAX (XMLHttpRequest) to fetch data from the server and update part of a web page without reloading it.

Algorithm

  1. Start.
  2. Create an HTML page with a button (id="button1") and an empty <div id="output">.
  3. Write a server file ajax.php that returns some text.
  4. In JavaScript, add a click event listener to the button.
  5. On click, create an XMLHttpRequest object and call open("GET","ajax.php",true).
  6. In onload, check if xhr.status is 200. If yes, put xhr.responseText inside the output div; otherwise log an error.
  7. Call xhr.send() to send the request.
  8. Run it on a local server (XAMPP/WAMP) and click the button.
  9. Stop.

Check result

Your code had errors. Corrections:

Code

index.html

<!DOCTYPE html>
<html>
<head>
  <title>AJAX Example</title>
</head>
<body>
  <button id="button1">Click me!</button>
  <div id="output"></div>

  <script>
    document.getElementById("button1").addEventListener("click", function () {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "ajax.php", true);
      xhr.onload = function () {
        if (xhr.status === 200) {
          document.getElementById("output").innerHTML = xhr.responseText;
        } else {
          console.log("There was an error.");
        }
      };
      xhr.send();
    });
  </script>
</body>
</html>

ajax.php (keep in the same folder)

<?php
  echo "<p>Hello! This text was loaded using AJAX from ajax.php</p>";
  echo "<p>Server time: " . date("H:i:s") . "</p>";
?>

Output

Before clicking the button:

Click me!

After clicking the button (page is not reloaded):

Click me!

Hello! This text was loaded using AJAX from ajax.php

Server time: 10:42:15

(The time shown will be your server's current time.)

Result

The AJAX request was sent to the server and the response was displayed inside the page without reloading it.

3. Working with jQuery Selectors

Aim

To write a program that uses jQuery selectors (element, id and class selectors) to select HTML elements and change their style.

Algorithm

  1. Start.
  2. Include the jQuery library using the <script> tag.
  3. Create three paragraphs: two normal ones, one with id="special", and one with class="note".
  4. Inside $(document).ready(), use the element selector $("p") to set a pink background on all paragraphs.
  5. Use the id selector $("#special") to change the text colour to blue.
  6. Use the class selector $(".note") to make the text bold.
  7. Open the page in a browser and view the output.
  8. Stop.

Check result

Your code had errors. Corrections:

Code

<!DOCTYPE html>
<html>
<head>
  <title>First jQuery Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function () {
      $("p").css("background-color", "pink");   // element selector
      $("#special").css("color", "blue");       // id selector
      $(".note").css("font-weight", "bold");    // class selector
    });
  </script>
</head>
<body>
  <p>This is first paragraph</p>
  <p id="special">This is second paragraph</p>
  <p class="note">This is third paragraph</p>
</body>
</html>

Output

This is first paragraph

This is second paragraph

This is third paragraph

Result

The jQuery element, id and class selectors selected the required elements and applied the styles successfully.

4. Demonstrate Form Handling (PHP)

Aim

To write a PHP program that accepts data from an HTML form, validates it, and displays the result.

Algorithm

  1. Start.
  2. Create an HTML form with the POST method, containing Name and Email fields and a Submit button.
  3. Set the form action to the same page using htmlspecialchars($_SERVER["PHP_SELF"]).
  4. When the page loads, check if $_SERVER["REQUEST_METHOD"] is "POST".
  5. Read the name and email from $_POST and remove extra spaces using trim().
  6. Validate the name: only letters and spaces allowed (preg_match), and not empty.
  7. Validate the email using filter_var(..., FILTER_VALIDATE_EMAIL).
  8. If there are errors, display the error messages. Otherwise display the name and email (using htmlspecialchars for safety).
  9. Stop.

Check result

Your code worked partly, but had problems. Corrections:

Code

form.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>PHP Form Handling</title>
</head>
<body>
  <h2>PHP Form Handling Example</h2>

  <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <label for="name">Name:</label>
    <input type="text" name="name" id="name"><br><br>

    <label for="email">Email:</label>
    <input type="text" name="email" id="email"><br><br>

    <input type="submit" name="submit" value="Submit">
  </form>

  <?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
      $name  = trim($_POST['name']);
      $email = trim($_POST['email']);
      $errors = [];

      if ($name === "" || !preg_match("/^[a-zA-Z ]*$/", $name)) {
          $errors[] = "Invalid name format. Only letters and white spaces allowed.";
      }
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $errors[] = "Invalid email format.";
      }

      if (count($errors) > 0) {
          foreach ($errors as $e) {
              echo "<p style='color:red'>$e</p>";
          }
      } else {
          echo "<h3>Form submitted successfully</h3>";
          echo "<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>";
          echo "<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>";
      }
  }
  ?>
</body>
</html>

Output

Case 1: Valid input (Name: John Doe, Email: john@example.com)

PHP Form Handling Example

Name:

Email:

Submit

Form submitted successfully

Name: John Doe

Email: john@example.com

Case 2: Invalid input (Name: John123, Email: john@)

PHP Form Handling Example

Name:

Email:

Submit

Invalid name format. Only letters and white spaces allowed.

Invalid email format.

Result

The PHP program received the form data, validated the name and email, and displayed either the submitted details or the proper error messages.