SelectIDNameAgeDegreeEntry TimeAction
75Sivasaran saravanan23Engineering2024-08-10 10:43:37
45NAVEENMANI S21Engineering2024-02-04 20:14:29
1John Dow30Computer Science2024-02-02 23:34:31
CRUD projects

Create Form



				
					<?php
global $wpdb;
// I stored as a php snippet by adding one plugins that convert the entire php code to one line snippet eg: [xyz-ips snippet="show-sample-db"] and I used this code for showcase of saved database table 	
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
    // Retrieve values from the form
    $name = sanitize_text_field($_POST['name']);
    $gender = sanitize_text_field($_POST['gender']);

    // Insert data into the user_details table
    $wpdb->insert(
        'user_details',
        array(
            'name' => $name,
            'gender' => $gender,
        ),
        array('%s', '%s')
    );
}

// Query to select data from the user_details table
$query = "SELECT * FROM user_details ORDER BY id DESC";
$results = $wpdb->get_results($query);

// Display the results in a table
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Gender</th><th>Entry Time</th></tr>";

foreach ($results as $row) {
    echo "<tr>";
    echo "<td>" . $row->id . "</td>";
    echo "<td>" . $row->name . "</td>";
    echo "<td>" . $row->gender . "</td>";
    echo "<td>" . $row->entry_time . "</td>";
    echo "</tr>";
}

echo "</table>";
?>
				
			
				
					<!DOCTYPE html>
<!-- Here is the Code for creating the HTML, i created by using elementor editor html snippet -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Submit Details</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        form {
            max-width: 400px;
            margin: 0 auto;
        }

        label {
            display: block;
            margin-bottom: 8px;
        }

        input, select {
            width: 100%;
            padding: 8px;
            margin-bottom: 16px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>

    <h2>Submit Form</h2>

    <form action="/wp-content/themes/woodmart/sample_process_form.php" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required>

        <label for="gender">Gender:</label>
        <select id="gender" name="gender" required>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
            <option value="Other">Other</option>
        </select>

        <button type="submit">Submit</button>
    </form>
    <title> Submitted Details</title>

</body>
</html>

				
			
				
					<?php
// This code for getting the entered value and then stores it into the database table...
// location /home/aourzcom/public_html/wp-content/themes/woodmart/sample_process_form.php
// Include WordPress configuration
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $name = sanitize_text_field($_POST['name']);
    $gender = sanitize_text_field($_POST['gender']);

    // Insert data into the database
    global $wpdb;
    $table_name = 'user_details';
    $result = $wpdb->insert(
        $table_name,
        array(
            'name' => $name,
            'gender' => $gender,
        ),
        array('%s', '%s')
    );

    // Check for errors
    if ($result === false) {
        echo $wpdb->last_error;
    } else {
        // Output JavaScript for displaying the success message and redirecting
        echo '
            <script>
                // Show success message
                alert("Data added successfully. Please wait...");

                // Redirect to the specified page after 4 seconds
                setTimeout(function() {
                    window.location.href = "https://www.aourz.com/database/";
                }, 1000);
            </script>
        ';
        exit;
    }
}
?>