How can I permanently disable a button after filling out a form?

I am currently creating a reservation page for our company. What I would like to do is to indicate where, when the user clicks the Book Now button , a form is filled out for them. After the user fills out the form, the backup button is constantly disabled for everyone who accesses the site. How can i do this? I am not very good at java script or php. Any help would be greatly appreciated.

    <div class="container">
      <div class="row">
        <div class="col-lg-12">
           <table class="table table-striped">
             <tr>
                <td>Product</td>
                <td><a href="#" class="btn btn-primary ">RESERVE NOW</a></td>
             </tr>
           </table>
           <form>
             First name:<br>
             <input type="text" name="firstname" value="Mickey">
             <br>
             Last name:<br>
             <input type="text" name="lastname" value="Mouse">
             <br><br>
             <input type="submit" value="Submit">
           </form>  
       </div>
     </div>
   </div> 

I mocked an example that can be found here:
  https://jsfiddle.net/rman215/gw2m53w9/1/

+4
source share
4

, -, 1 , ?

, (, disabled_at [timestamp, default = null]). - ( ajax ), disabled_at NOW(). disabled_at IS NULL. - .

disabled , disabled_at IS NOT NULL

EDIT:

disable.php

<?php

function reserveItem($name = null, $value = null)
{
    $name .= '.reserved';
    $value = $value ? $value : 1;
    return file_put_contents($name, $value);
}

function isDisabled($name = null)
{
    return file_exists($name . '.reserved');
}

$disabled = isDisabled('item1');

if (isset($_POST['submit'])) {
    $name = isset($_POST['firstname']) ? $_POST['firstname'] : '';
    $lastName = isset($_POST['lastname']) ? $_POST['lastname'] : '';
    if (!$disabled) {
        reserveItem('item1', $name . $lastName);
    }
}

?>

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
 </head>
 <body>
   <div class="container">
      <div class="row">
        <div class="col-lg-12">
           <table class="table table-striped">
             <tr>
                <td>Product</td>
                <td><a href="#" class="btn btn-primary ">RESERVE NOW</a></td>
             </tr>
           </table>
           <form method="post" action="disable.php">
             First name:<br>
             <input type="text" name="firstname" value="Mickey">
             <br>
             Last name:<br>
             <input type="text" name="lastname" value="Mouse">
             <br><br>
             <input type="submit" value="Submit" <?= $disabled ? 'disabled' : ''; ?>>
           </form>  
       </div>
     </div>
   </div> 
 </body> 
</html>

: .

0

id

function disableButton() {
     $("#idOfButton").attr("disabled","disabled")
}
0

. " " :

ajax , : " " id: reserve_btn_id

$.ajax({
  // your code....
}).done(function() {
  $("#reserve_btn_id").attr("disabled","disabled");
});
0

JavaScript , , JavaScript , . MySQL, - :

CREATE TABLE Items(
   ItemID INTEGER AUTO_INCREMENT PRIMARY KEY,
   Description VARCHAR(255),
   IsReserved TINYINT(1) //this works like a boolean        
);

, :

SELECT * FROM Items ORDER BY ItemID

:

$server = "localhost";
$username = "root";
$password = "root";
$database = "mydatabase";
$connection = mysqli_connect($server, $username, $password, $database);

$result = mysqli_query($connection, "SELECT * FROM Items ORDER BY ItemID");

if (mysqli_num_rows($result) > 0){
   echo("<form action=\"mypage.php\" method=\"post\">");
   while($row = $result->fetch_assoc()){
      echo("input name=\"id\" type=\"hidden\" value=" . $row['ItemID'] . ">"); //this input type hidden isn't shown on the page but we use it to store the id of the item so that we can retrieve it on the PHP page on submit
      echo("ID: " . $row['ItemID']);
      echo("Description: " . $row['Description']);
      if ($row['IsReserved'] == 1){
         echo("<button class=\"mybutton\" type=\"submit\" disabled>Reserve</button>");
      }
      else{
         echo("<button class=\"mybutton\" type=\"submit\">Reserve</button>");
      }
   }
   echo("</form>");
}
else{
   echo("No data");
}

, , , โ€‹โ€‹ PHP, - :

$server = "localhost";
$username = "root";
$password = "root";
$database = "mydatabase";
$connection = mysqli_connect($server, $username, $password, $database);

$id = (int) $_POST['id']; // here you get the id of the hidden input

 // $data = mysqli_query($connection, "SELECT * FROM Items WHERE ItemID = $id");

/* $data now contains all info stored about the item, you can use it if you
   need to insert those info in another table, etc. */ 

// UPDATING STATUS
mysqli_query($connection, "UPDATE Items SET IsReserved = 1 WHERE ItemID = $id");

mysqli_close($connection);

, , , :)

I will leave you this guide on how to process forms and their data using PHP.

0
source

Source: https://habr.com/ru/post/1612785/


All Articles