title: How to get dynamic buttons value when button is click in PHP?
body: <p>I have created dynamic buttons in php. I need to get the value of that button when particular button is click. And when we click the button, all information is fetch in to the form.</p>
  
  <p>Here is my code for buttonclick</p>
  
  <pre><code>&lt;?php
  
  function dash()
  {
      include 'config.php';
      $sql = "SELECT  roomno FROM roombook";
  
      if($result = mysqli_query($db, $sql))
      {
              $str = '';
  
              while($row = mysqli_fetch_array($result))
              {
                  // generate array from comma delimited list
                  $rooms = explode(',', $row['roomno']);
  
                  //create the dynamic button and set the value
                  foreach ( $rooms as $k=&gt;$v )
                  {
                      $str .= '&lt;input type="button"  onClick="showDiv()" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/&gt;';
                  }
              }
  
              return $str;
          }
          else {
              echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
              }
  
      mysqli_close($db);
  }   
  ?&gt;
  
  &lt;!Doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;room boking&lt;/title&gt;
  
    &lt;link href="css/bootstrap1.min.css" rel="stylesheet"&gt;
          &lt;link rel="stylesheet" href="css/front.css"&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="post"  action=""&gt;
  &lt;div class ="  row box col-md-4" &gt;
  &lt;div style="color:black"&gt;&lt;?php echo dash();?&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Here fetching the value in form code</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;?php
  include 'config.php';
      if(isset($_POST['book']))
      {
      $roomno=$_POST['roomno'];
      $roomtype=$_POST['roomtype'];
      $location=$_POST['location'];
      $charges=$_POST['charges'];
      $firstname=$_POST['firstname'];
      $lastname=$_POST['lastname'];
      $address=$_POST['address'];
      $city=$_POST['city'];
      $state=$_POST['state'];
      $pincode=$_POST['pincode'];
      $mobilenumber=$_POST['mobilenumber'];
      $idtype=$_POST['idtype'];
      $idnumber=$_POST['idnumber'];
      $Adate=$_POST['Adate'];
      $Ldate=$_POST['Ldate'];
      //echo  $roomno."&lt;br&gt;".$roomtype."&lt;br&gt;".$location."&lt;br&gt;".$charges."&lt;br&gt;".$firstname."&lt;br&gt;".$lastname."&lt;br&gt;";
      if(strlen($firstname)&lt;3)
      {
          echo "&lt;script&gt;alert('First name is to short')&lt;/script&gt;";
  
  
      }
      else if(strlen($lastname)&lt;3)
      {
          echo "&lt;script&gt;alert('Last name is to short')&lt;/script&gt;";
  
  
      }
      else if(strlen($address)&lt;3)
      {
          echo "&lt;script&gt;alert('UserName name is to short')&lt;/script&gt;";
  
  
      }
      else
      {
          $query=" INSERT INTO customerbook(roomno,roomtype,location,charges,firstname,lastname,address,city,state,pincode,mobilenumber,idtype,idnumber,Adate,Ldate) VALUES('$roomno','$roomtype','$location','$charges','$firstname','$lastname','$address','$city','$state','$pincode','$mobilenumber','$idtype','$idnumber','$Adate','$Ldate')";
      //$query="INSERT INTO customerbook(roomno,roomtype,location,charges,firstname,lastname,address,state,city,pincode,mobilenumber,idtype,idnumber,Adate,Ldate) VALUES('$roomno','$roomtype','$location','$charges','$firstname','$lastname','$address','$state,','$city','$pincode','$mobilenumber','$idtype','$idnumber','$Adate','$Ldate')";
      if(mysqli_query($db, $query))
      {
          echo "&lt;script&gt;alert('booking  seccessfully')&lt;/script&gt;";
          //header("Location:login.php"); 
          }
      else echo "error";
      }
  }
  
  $sql = "SELECT  roomno,roomtype,location,charges FROM roombook where roomno='101'";
  
  if($result = mysqli_query($db, $sql)){
      if(mysqli_num_rows($result) &gt; 0){    
          while($row = mysqli_fetch_array($result)){
  
                     $roomno=$row['roomno'];
                     $roomtype=$row['roomtype'];
                     $location=$row['location'];
                     $charges=$row['charges'];
  
          }
  
          // Free result set
          mysqli_free_result($result);
      } else{
          echo "No records matching your query were found.";
      }
  } else{
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
  }
  
  // Close connection
  mysqli_close($db);
  
  ?&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --&gt;
      &lt;title&gt;booking Form&lt;/title&gt;
  
      &lt;!-- Bootstrap --&gt;
      &lt;link href="css/bootstrap1.min.css" rel="stylesheet"&gt;
      &lt;link href="css/book.css" rel="stylesheet"&gt;
  
  &lt;/style&gt;
      &lt;!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --&gt;
      &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
      &lt;!--[if lt IE 9]&gt;
        &lt;script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"&gt;&lt;/script&gt;
        &lt;script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
      &lt;!-- jQuery (necessary for Bootstrap's JavaScript plugins) --&gt;
      &lt;script src="js/jquery-1.12.4.js"&gt;&lt;/script&gt;
      &lt;script src="js/book1.js"&gt;&lt;/script&gt;
      &lt;!-- Include all compiled plugins (below), or include individual files as needed --&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div class="head" id="link"&gt;
  &lt;div class="panel panel-primary" style="margin:20px;"&gt;
      &lt;div class="panel-heading"&gt;
              &lt;center&gt;&lt;h3 class="panel-title"&gt;booking Form&lt;/h3&gt;&lt;/center&gt;
      &lt;/div&gt;
  &lt;div class="panel-body"&gt;
      &lt;form method="post" action=""&gt;
  &lt;div class="col-md-12"&gt;
      &lt;div class="form-group col-md-12 "&gt;
              &lt;label for="roomno"&gt;Room Number*    &lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="roomno" value='&lt;?php echo $roomno; ?&gt;'  placeholder=""&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-md-6"&gt;
              &lt;label for="type"&gt;Room Type*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="roomtype" value='&lt;?php echo $roomtype; ?&gt;' placeholder=""&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-md-6"&gt;
              &lt;label for="location"&gt;Location*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="location" placeholder=""&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group col-md-12"&gt;
              &lt;label for="charges"&gt;charges*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="charges" placeholder=""&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group col-md-6"&gt;
              &lt;label for="customer name"&gt;First Name*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="firstname" placeholder=""&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-md-6"&gt;
              &lt;label for="customer name"&gt;Last Name*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="lastname" placeholder=""&gt;
          &lt;/div&gt;
      &lt;div class="form-group col-md-12"&gt;
            &lt;label for="address"&gt;Address*&lt;/label&gt;
            &lt;textarea class="form-control input-sm" name="address" rows="3"&gt;&lt;/textarea&gt;
         &lt;/div&gt;
  
      &lt;div class="form-group col-md-4"&gt;
              &lt;label for="city"&gt;City*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="city" placeholder=""&gt;
          &lt;/div&gt;
  
      &lt;div class="form-group col-md-4"&gt;
              &lt;label for="state"&gt;State*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="state" placeholder=""&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-md-4"&gt;
              &lt;label for="pincode"&gt;Pincode&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="pincode" placeholder=""&gt;
          &lt;/div&gt;
  
      &lt;div class="form-group col-md-12"&gt;
              &lt;label for="mobile"&gt;Mobile Number*&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="mobilenumber" placeholder=""&gt;
          &lt;/div&gt;
      &lt;div class = "form-group col-md-12"&gt;
            &lt;label for="years"&gt;Id Type *&lt;/label&gt;   
  
  
            &lt;select class="form-control input-sm" name="idtype"&gt;
          &lt;option&gt;-- Select Id Card --&lt;/option&gt;
          &lt;option&gt;Pancard&lt;/option&gt;
          &lt;option&gt;Adhar Card&lt;/option&gt;
          &lt;option&gt;voting card&lt;/option&gt;
  
  
            &lt;/select&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group col-md-12"&gt;
              &lt;label for="idnumber"&gt;Id Number&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm" name="idnumber" placeholder=""&gt;
          &lt;/div&gt;
  
      &lt;div class="form-group col-md-6"&gt;
              &lt;label for="arrival"&gt;Arrival Date&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm datepicker" name="Adate" placeholder=""&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-md-6"&gt;
              &lt;label for="arrival"&gt;Leaving  Date&lt;/label&gt;
              &lt;input type="text" class="form-control input-sm datepicker" name="Ldate" placeholder=""&gt;
          &lt;/div&gt;
  &lt;/div&gt;
  
  
  &lt;div class="col-md-12"&gt;
      &lt;div class="form-group col-md-12 " &gt;
              &lt;center&gt;&lt;input type="submit" class="btn btn-primary" value="Submit" name="book"/&gt;&lt;center&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>