Tulis skrip php untuk menampilkan output ini setelah pengguna mengklik tombol kirim

Kami terutama menggunakan formulir HTML saat mengumpulkan input pengguna di aplikasi berbasis web. Mulai dari formulir kontak, formulir login, dan juga formulir pendaftaran. Formulir adalah antarmuka mendasar antara pengguna dan server. Membuat formulir pada aplikasi web dicapai dengan menggunakan HTML. PHP menghubungkan aplikasi web dengan database server

Melalui artikel ini, Anda akan belajar caranya

  • Buat formulir HTML
  • Pelajari tentang metode permintaan Hypertext Transfer Protocol (HTTP) (GET, POST, dan PUT)
  • Pahami metode PHP POST dan GET

Prasyarat

Sebelum kita mulai, Anda harus memiliki pengetahuan sebelumnya tentang HTML, PHP, dan MySQL

Sekilas tentang formulir

Formulir adalah dokumen dengan bidang kosong bagi pengguna untuk memasukkan atau memperbarui data. Data pengguna disimpan dalam database dan diambil kapan saja

Menggunakan formulir untuk mengumpulkan data dalam aplikasi web adalah tugas sederhana

Beberapa bentuk populer termasuk

  • Formulir Kontak
  • Formulir Pencarian
  • Formulir Masuk
  • Formulir Pendaftaran

Formulir disajikan kepada pengguna yang memasukkan data dan mengirimkan formulir menggunakan tombol

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
2. Sebagian besar waktu, data formulir dikirim ke server untuk diproses. Memproses input pengguna melibatkan validasi input, operasi basis data, dan memberikan umpan balik kepada pengguna. Ada empat operasi basis data yang terlibat, yaitu membuat, membaca, memperbarui, dan menghapus. Pola ini umumnya dikenal dengan akronim
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
3 operasi

Hypertext Transfer Protocol (HTTP) memungkinkan komunikasi antara klien (browser) dan server. Permintaan HTTP dikirim oleh klien ke server yang kemudian mengembalikan respons. Meskipun HTTP mendukung beberapa metode, kami akan fokus pada

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4,
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5, dan
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
6. Data diproses berdasarkan metode yang dipilih

Metode

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 mengambil data dari server. Metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 mengirimkan data dari formulir HTML ke server untuk membuat sumber daya. Metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
6 mengirim data ke server untuk membuat atau memperbarui sumber daya. Beberapa pengembang tidak dapat membedakan antara metode POST dan PUT

Metode PUT adalah

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
_0. Ini berarti memanggil metode PUT berkali-kali tidak akan mempengaruhi database karena data sudah diperbarui. Sebaliknya, memanggil metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 memengaruhi database karena Anda membuat banyak objek

Cara membuat formulir HTML

Formulir HTML dapat berisi elemen khusus seperti tombol, tombol radio, dan kotak centang. Oleh karena itu, menjadi mudah bagi pengguna untuk berinteraksi dengan halaman web. Formulir harus ramah pengguna. Ini berarti bahwa pengguna yang tidak memiliki keterampilan teknis harus dapat menggunakannya

Formulir ditentukan oleh tag

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
_2. Tag
<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
3 mengelilingi semua input. Itu juga memberikan instruksi tentang bagaimana dan di mana untuk mengirimkan formulir. Formulir HTML mengirimkan data ke skrip PHP Anda menggunakan metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 atau
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4

Berikut adalah contoh formulir yang mengirimkan data ke file bernama

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
6. Untuk mendapatkan kode sumber yang lengkap, gunakan kode HTML ini dan ubah metode menjadi POST atau GET jika perlu

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML Form</title>
</head>
<body>
    <h1>HTML Form</h1>
    <form method="" action="index.php">
        Name: <input type="text" name="name"><br><br/>
        Email: <input type="text" name="email"><br/>
        <br/>
        <input type="submit" value="submit" >
    </form>
</body>
</html>

Output untuk kode di atas seperti yang ditunjukkan pada tangkapan layar di bawah ini

Tulis skrip php untuk menampilkan output ini setelah pengguna mengklik tombol kirim

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
7 mengidentifikasi halaman tempat input formulir dikirimkan. Data dapat dikirimkan pada halaman yang sama dengan formulir atau pada halaman yang berbeda.
<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
8 menentukan bagaimana data diproses. Ini bisa berupa
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5,
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4, atau
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
6. Metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 mengumpulkan data dari server dan mengirimkannya di URL. Data yang dikirimkan melalui metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
_5 disimpan di badan permintaan HTTP dan tidak dapat dilihat di URL

metode POST

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 adalah metode superglobal, yang mengumpulkan data formulir dan mengirimkannya ke server HTTP. Data yang dimasukkan dikodekan, dan kontennya disembunyikan. Metode POST memiliki cakupan global, dan data diakses dari skrip apa pun

Metode POST lebih disukai karena data yang dikirim melaluinya tidak terlihat di URL. Metode POST juga penting karena data tidak dapat didekodekan dengan melihat log server web

POST tidak membatasi jumlah data yang dikirim dari formulir. Ini karena data dikirimkan melalui badan permintaan HTTP. Metode

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 cocok untuk formulir
<?php 
    $servername = "localhost";
    $username = "root"; # MySQL user
    $password = ""; # MySQL Server root password
    $dbname='crud'; # Database name
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        # Display an error mesage if the connection fails
        die("Connection failed: " . $conn->connect_error);
    }
?>
6

Memproses data formulir (skrip PHP)

Kode PHP di bawah ini dapat digunakan untuk memproses input dari form HTML dengan metode POST. Kode harus dimasukkan ke dalam skrip, yang merupakan target formulir

Itu bisa di skrip yang sama di mana bentuk HTML berada, atau bisa di skrip yang berbeda. Dalam hal ini, kita akan memiliki kode PHP dalam skrip yang sama dengan bentuk HTML

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>

Output dari kode di atas adalah seperti yang ditunjukkan pada animasi di bawah ini

metode DAPATKAN

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 adalah metode super global default yang mengumpulkan atau mengambil data dari server. Ini memiliki ruang lingkup global. Dengan demikian, data diakses dari skrip apa pun dalam program. Metode GET mengirimkan data di URL

Data yang ditransfer melalui metode ini terlihat di URL permintaan HTTP. Permintaan HTTP dapat di-cache dan disimpan dalam riwayat browser. Kerugian dari metode GET adalah tidak boleh digunakan dengan data sensitif seperti kata sandi karena tidak aman

Metode GET memiliki batasan jumlah data yang dikirim dari formulir. Data yang dikirim ke URL bergantung pada sistem operasi server web dan jenis browser

Sebagian besar sistem memiliki batas

<?php 
    $servername = "localhost";
    $username = "root"; # MySQL user
    $password = ""; # MySQL Server root password
    $dbname='crud'; # Database name
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        # Display an error mesage if the connection fails
        die("Connection failed: " . $conn->connect_error);
    }
?>
_8 karakter. Contoh terbaik menggunakan metode GET adalah dengan formulir mesin pencari. Kode di bawah ini dapat digunakan untuk memproses formulir HTML dengan metode yang ditetapkan sebagai
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
_

Berikut adalah output dari contoh metode GET

Perbandingan tabel metode GET dan POST

Metode POST Metode GETMembookmark hasil tidak dimungkinkan. Hasil dapat di-bookmark. Data tidak tersimpan dalam riwayat browser. Itu tersembunyi. Data tetap ada di riwayat browser. Performanya rendah karena POST tidak dapat mendekode data. Performanya tinggi karena sifat sederhana dalam menampilkan data. Lebih aman Kurang aman Jangan batasi jumlah data yang dikirim ke server. Batasi jumlah data yang dikirim ke server. Ia bekerja dengan data sensitif. Itu tidak dapat bekerja dengan data sensitif

Formulir HTML dan operasi CRUD database MySQL

Kita akan belajar bagaimana melakukan operasi CRUD pada database MySQL menggunakan form HTML. Kita akan belajar bagaimana menggunakan formulir HTML untuk membuat, membaca, memperbarui, dan menghapus data dari database MySQL

Pertama, buat database MySQL dan beri nama

include("connect.php")
0. Buat tabel dengan
include("connect.php")
1kolom, beri nama
include("connect.php")
2

Kolom adalah

  1. Indo
  2. nama
  3. surel

Buat koneksi server database

Setelah membuat database dan tabel, kita membutuhkan skrip PHP yang terhubung ke server database MySQL. Buat file bernama

include("connect.php")
_3 dan tempatkan di dalamnya kode berikut. Skrip membuat koneksi ke server database MySQL

<?php 
    $servername = "localhost";
    $username = "root"; # MySQL user
    $password = ""; # MySQL Server root password
    $dbname='crud'; # Database name
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        # Display an error mesage if the connection fails
        die("Connection failed: " . $conn->connect_error);
    }
?>

Kami nantinya akan menyertakan file ini menggunakan fungsi

include("connect.php")
4

include("connect.php")

Membuat

Untuk membuat record di database, gunakan kode di bawah ini. Setelah formulir dikirimkan melalui metode

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
_5, formulir tersebut diproses, dan catatan dibuat di tabel
include("connect.php")
2

<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>

Ini formulirnya

<!DOCTYPE html>
<html lang="en">
<head>
    <title>FORMS</title>
</head>
<body>
    <h1>Form</h1>
    <form method="post" action="form-post.php">
        Name: <input type="text" name="name"><br><br/>
        Email: <input type="text" name="email"><br/>
        <br/>
        <input type="submit" name="save" value="submit" >
    </form>
</body>
</html>

Video di bawah ini menunjukkan cara kerja kode di atas

Catatan. Setelah kami mengirimkan input formulir, catatan baru dibuat di database dan ditampilkan dalam tabel

Membaca

Kode di bawah mengambil data yang disisipkan dan menampilkannya dalam tabel HTML

<?php
# Include script to make a database connection
include("connect.php")
$ Read From the database and display in the table
$query2 = "SELECT * FROM user";
$result = $conn->query($query2);
if ($result->num_rows > 0) {
    # Output data for each row
    echo "<table id='tsa' border='1' id='example' class='table table-striped responsive-utilities table-hover'>
              <thead>
                <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Action 1</th>
                <th>Action 2</th>
                </tr>
              </thead>
              ";
    while($row = $result->fetch_assoc()) {
       echo "<tr id='green' ",">",
            "<td>", $row["id"],"</td>",
            "<td>", $row["name"],"</td>",
            "<td>", $row["email"],"</td>",
            "<td>",
                "<form action='update.php' method='post'>
                 <input name='id' value='",$row["id"],"' hidden>
                 <button type='submit' name='update' value='update'>Edit</button>
                </form>",
            "</td>",
            "<td>",
                "<form action='form-post.php' method='post'>
                 <input name='id' value='",$row["id"],"' hidden>
                 <button type='submit' name='delete' value='delete'>Delete</button>
                </form>",
            "</td>",
            "</tr>";
    }
    echo  "</table>";
}else {
 echo "No Records!";
}
?>

Di bawah ini adalah animasi tentang cara kerja operasi baca

Memperbarui

Formulir HTML digunakan untuk memperbarui data yang ada di database. Dalam hal ini, kami akan mengimplementasikan fungsi pembaruan.

include("connect.php")
_7 ditampilkan saat kita mengklik tombol
include("connect.php")
8 di sel tabel

Catat kode yang digunakan untuk membuat tombol edit di atas meja. Tombol

include("connect.php")
_9 adalah tombol
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
2 untuk formulir dengan kolom input tersembunyi

Setelah tombol

include("connect.php")
8 diklik,
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 item yang akan diedit dikirim ke skrip
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
3. Metode
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 atau
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 dapat digunakan

    <td>
        <form action='update.php' method='post'>
            <input name='id' value='",$row["id"],"' hidden>
            <button type='submit' name='update' value='update'>Edit</button>
        </form>
    </td>

Dalam skrip

<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
_3, formulir dengan data yang cocok dengan informasi yang dikirimkan ditampilkan untuk diedit. Setelah pengeditan selesai, data yang diperbarui dikirim kembali ke skrip untuk diproses. Dalam hal ini, kami menggunakan skrip yang sama untuk memproses
include("connect.php")
9 permintaan

Script di bawah ini digunakan untuk memperbarui data

<?php
// Include script to make a database connection
include("connect.php");
// Empty string to be used later
$name='';
$email='';
$id='';

// Button click to update using POST method
if(!empty($_POST['update']) && !empty($_POST['id']) )  {
  $id=$_POST['id'];
  // Fetch record with ID and populate it in the form
  $query2 = "SELECT * FROM user WHERE id='".$_POST['id']."' ";
  $result = $conn->query($query2);
  if ($result->num_rows > 0) {
    // Output data for each row
    while($row = $result->fetch_assoc()) {
      $name=$row["name"];
      $email=$row["email"];
    }
    echo "Current Details: " ."<b> - Name:</b> " . $name. " <b>Email:</b>" . $email. "<br>";
  } else {
    echo "Error updating";
  }
}

// Button click to update using GET method
if(!empty($_GET['update']) && !empty($_GET['id']) )  {
  $id=$_GET['id'];
  // Fetch record with id and populate it in the form
  $query2 = "SELECT * FROM user WHERE id='".$_GET['id']."' ";
  $result = $conn->query($query2);
  if ($result->num_rows > 0) {
    // Output data for each row
    while($row = $result->fetch_assoc()) {
      $name=$row["name"];
      $email=$row["email"];
    }
    echo "Current Details: " ."<b> - Name:</b> " . $name. " <b>Email:</b>" . $email. "<br>";
  } else {
    echo "Error updating";
  }
}
// Check that the input fields are not empty and process the data
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['id']) ){
    // Insert into the database
  $query = "UPDATE user SET name='".$_POST['name']."', email='".$_POST['email']."' WHERE id='".$_POST['id']."' ";
  if (mysqli_query($conn, $query)) {
      echo "Record updated successfully!<br/>";
      echo '<a href="form-get.php">Get Form</a><br/>
            <a href="form-post.php">Post Form</a>';
      die(0);
  } else {
      // Display an error message if unable to update the record
       echo "Error updating record: " . $conn->error;
       die(0);
  }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>FORM</title>
</head>
<body>
    <h1>Form</h1>
    <p>Edit the record</p>
    <form method="POST" action="update.php">
        ID: <input type="text" name="id" value="<?php echo($id); ?>" required><br><br/>
        Name: <input type="text" name="name" value="<?php echo($name); ?>" required><br><br/>
        Email: <input type="text" name="email" value="<?php echo($email); ?>" required><br/>
        <br/>
        <input type="submit" value="update">
    </form>
</body>
</html>

Video di bawah ini menunjukkan bagaimana fungsi pembaruan akan bekerja di browser

Menghapus

Untuk menghapus catatan di tabel, pengguna mengklik tombol

<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
8 di tabel HTML

Catat kode yang digunakan untuk menampilkan tombol di dalam sel tabel

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
0

Tombol

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
2 memiliki bidang
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 tersembunyi. Data dikirim ke formulir dan diproses oleh skrip PHP di bawah ini. Jika
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 tidak kosong, maka catatan dengan
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 yang dikirimkan akan dihapus

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
_1

Fungsi hapus berfungsi seperti yang ditunjukkan dalam video di bawah ini

Anda dapat mengunduh kode sumber dari sini

Kesimpulan

Kesimpulannya, permintaan HTTP memungkinkan komunikasi antara klien dan server. Kami telah belajar cara membuat formulir HTML dan memproses data menggunakan PHP. Kita juga sudah belajar tentang metode POST, PUT dan GET

Kami mengetahui bahwa

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 membuat data,
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 membaca data dari server, dan PUT memperbarui data di server. Kami belajar bagaimana melakukan operasi CRUD pada database MySQL menggunakan PHP

Saya harap artikel ini akan menjelaskan dan memberi Anda pemahaman saat bekerja dengan formulir HTML di PHP

Bagaimana cara menampilkan data klik tombol di PHP?

Memanggil fungsi PHP menggunakan tombol HTML. Buat dokumen formulir HTML yang berisi tombol HTML. Saat tombol diklik, metode POST dipanggil. Metode POST menjelaskan cara mengirim data ke server. Setelah mengklik tombol, fungsi array_key_exists() dipanggil.

Bagaimana menampilkan data setelah dikirim dalam PHP?

Gunakan metode isset() di PHP untuk menguji apakah form berhasil atau tidak dikirim . Dalam kode, gunakan fungsi isset() untuk memeriksa metode $_POST['submit']. Ingat di tempat kirim, tentukan nama tombol kirim. Setelah mengklik tombol kirim, tindakan ini akan berfungsi sebagai metode POST.

Apa yang akan menjadi output ketika tombol kirim formulir diklik?

Formulir akan dikirimkan ke server dan browser akan dialihkan ke alamat browser saat ini dan menambahkan sebagai parameter string kueri nilai bidang masukan.

Bagaimana cara menampilkan output dalam PHP?

echo adalah pernyataan, yang digunakan untuk menampilkan output
echo dapat digunakan dengan atau tanpa tanda kurung
gema tidak mengembalikan nilai apa pun
Kita dapat melewati banyak string yang dipisahkan oleh koma (,) di gema
gema lebih cepat dari pernyataan cetak