Home >Backend Development >PHP Tutorial >How to create a new note using form and request methods in PHP?

How to create a new note using form and request methods in PHP?

王林
王林Original
2024-07-17 03:11:00394browse

How to create a new note using form and request methods in PHP?

Introduction to Forms and Request Methods in PHP

Forms are a crucial part of web development, allowing users to interact with your application. In PHP, handling form data is essential to create dynamic and interactive web pages. In this post, I'll explore the basics of forms and request methods in PHP.

Diving in code

In a fresh vs code project (version 1.90 at the time of working), we need two different files to easily learn the working of code.

On VS Code Side

- Creating a New Note

To create a new note, I'll start by creating a form in note-create.view.php. This form will submit to note-create.php, which will handle the form data.

<?php require('partials/head.php') ?>
<?php require('partials/nav.php') ?>
<?php require('partials/banner.php') ?>
<main>
  <div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
    <div class="md:grid md:grid-cols-3 md:gap-6">
      <div class="mt-5 md:col-span-2 md:mt-0">
        <form method="POST">
          <div class="shadow sm:overflow-hidden sm:rounded-md">
            <div class="space-y-6 bg-white px-4 py-5 sm:p-6">
              <div>
                <label for="body" class="block text-sm font-medium text-gray-700">Body</label>
                <div class="mt-1">
                  <textarea id="body" name="body" rows="3" 
                            class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" 
                            placeholder="Here's an idea for a note..."></textarea>
                </div>
              </div>
            </div>
            <div class="bg-gray-50 px-4 py-3 text-right sm:px-6">
              <button type="submit" 
                      class="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
                Save
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</main>
<?php require('partials/footer.php') ?>

- Using Request Methods

In note-create.php, I'll check if the form has been submitted using the $_SERVER['REQUEST_METHOD'] superglobal. If the form has been submitted, I'll display a message.

<?php
$heading = 'Create Note';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  dd('I submitted the form');
}
require 'views/note-create.view.php';

- Routes

To link the form to the note-create.php script, I'll define a route in routes.php.

<?php
return [
  '/' => 'controllers/index.php',
  '/about' => 'controllers/about.php',
  '/notes' => 'controllers/notes.php',
  '/note' => 'controllers/note.php',
  '/notes/create' => 'controllers/note-create.php',
  '/contact' => 'controllers/contact.php',
];

- Adding Forms Layout Link

To add the forms layout link, I'll include it in the head.php file.

<script src="https://cdn.tailwindcss.com?plugins=forms "></script>

- Conclusion

In this post, I covered the basics of forms and request methods in PHP. I created a simple form to get a note from the user and handled the form data using PHP. I also explored how to check the request method used to submit the form. This is just the beginning of working with forms and request methods in PHP.

I hope that you have clearly understand this.

The above is the detailed content of How to create a new note using form and request methods in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn