Counter for increment n decrement
import React, { useState } from 'react';
Php form validation function Counter() {
<?php const [count, setCount] = useState(0);
$name = $phone = $password = "";
$errors = []; return (
<div>
if ($_SERVER["REQUEST_METHOD"] == "POST") { <h2>Count: {count}</h2>
// Name validation <button onClick={() => setCount(count - 1)}>-</button>
if (empty($_POST["name"])) { <button onClick={() => setCount(count + 1)}>+</button>
$errors[] = "Name is required"; </div>
} else { );
$name = htmlspecialchars(trim($_POST["name"])); }
}
export default Counter;
Session to store page view count
// Phone number validation (digits only, 10-15 digits)
<?php
if (empty($_POST["phone"])) {
session_start();
$errors[] = "Phone number is required";
} elseif (!preg_match("/^\d{10,15}$/", $_POST["phone"])) { Form validation using js
if (isset($_SESSION['views'])) {
$errors[] = "Invalid phone number"; <form id="myForm">
$_SESSION['views']++;
} else { Name: <input type="text" id="name"><br>
} else {
$phone = htmlspecialchars(trim($_POST["phone"])); Phone: <input type="text" id="phone"><br>
$_SESSION['views'] = 1;
} Password: <input type="password" id="password"><br>
}
<button type="submit">Submit</button>
// Password validation (min 6 characters) </form>
echo "You have viewed this page " . $_SESSION['views'] . "
if (empty($_POST["password"])) {
times.";
$errors[] = "Password is required"; <script>
?>
} elseif (strlen($_POST["password"]) < 6) { document.getElementById("myForm").addEventListener("subm
$errors[] = "Password must be at least 6 characters"; it", function(e) {
} else { e.preventDefault(); // prevent form from submitting
$password = htmlspecialchars($_POST["password"]); Store html data into sql using php
} const name = document.getElementById("name").value.trim(); <?php
} const phone = // Database connection
?> document.getElementById("phone").value.trim(); $conn = new mysqli("localhost", "username",
const password = "password", "database");
document.getElementById("password").value;
// Check connection
Change background random colour let errors = []; if ($conn->connect_error) {
<html> die("Connection failed: " . $conn->connect_error);
<head> if (name === "") errors.push("Name is required"); }
<title>Background Color Changer</title> if (!/^\d{10,15}$/.test(phone)) errors.push("Phone must be
</head> 10-15 digits"); // Get and sanitize input
<body> if (password.length < 6) errors.push("Password must be at $name = $conn->real_escape_string($_POST['name']);
least 6 characters"); $email = $conn->real_escape_string($_POST['email']);
<button onclick="changeColor()">Change Background Color</button>
if (errors.length > 0) { // Insert into database
<script> alert(errors.join("\n")); $sql = "INSERT INTO users (name, email) VALUES
function changeColor() { } else { ('$name', '$email')";
document.body.style.backgroundColor = alert("Form submitted successfully!"); if ($conn->query($sql) === TRUE) {
'#' + Math.floor(Math.random() * 16777215).toString(16); // Optionally submit the form here echo "Data saved successfully!";
} } } else {
</script> }); echo "Error: " . $conn->error;
</script> }
</body>
</html> $conn->close();
?>