2017-05-20 7 views
0

私はPHPで動的ボタンを作成しました。特定のボタンがクリックされたときに、そのボタンの値を取得する必要があります。ボタンをクリックすると、すべての情報がフォームに取り込まれます。ここで PHPでボタンがクリックされたときの動的ボタンの値の取得方法

<!DOCTYPE html> 
<?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."<br>".$roomtype."<br>".$location."<br>".$charges."<br>".$firstname."<br>".$lastname."<br>"; 
    if(strlen($firstname)<3) 
    { 
     echo "<script>alert('First name is to short')</script>"; 


    } 
    else if(strlen($lastname)<3) 
    { 
     echo "<script>alert('Last name is to short')</script>"; 


    } 
    else if(strlen($address)<3) 
    { 
     echo "<script>alert('UserName name is to short')</script>"; 


    } 
    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 "<script>alert('booking seccessfully')</script>"; 
     //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) > 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); 

?> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <title>booking Form</title> 

    <!-- Bootstrap --> 
    <link href="css/bootstrap1.min.css" rel="stylesheet"> 
    <link href="css/book.css" rel="stylesheet"> 

</style> 
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
     <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
    <![endif]--> 
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="js/jquery-1.12.4.js"></script> 
    <script src="js/book1.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 

    </head> 
    <body> 
    <div class="head" id="link"> 
<div class="panel panel-primary" style="margin:20px;"> 
    <div class="panel-heading"> 
      <center><h3 class="panel-title">booking Form</h3></center> 
    </div> 
<div class="panel-body"> 
    <form method="post" action=""> 
<div class="col-md-12"> 
    <div class="form-group col-md-12 "> 
      <label for="roomno">Room Number* </label> 
      <input type="text" class="form-control input-sm" name="roomno" value='<?php echo $roomno; ?>' placeholder=""> 
     </div> 
     <div class="form-group col-md-6"> 
      <label for="type">Room Type*</label> 
      <input type="text" class="form-control input-sm" name="roomtype" value='<?php echo $roomtype; ?>' placeholder=""> 
     </div> 
     <div class="form-group col-md-6"> 
      <label for="location">Location*</label> 
      <input type="text" class="form-control input-sm" name="location" placeholder=""> 
     </div> 

     <div class="form-group col-md-12"> 
      <label for="charges">charges*</label> 
      <input type="text" class="form-control input-sm" name="charges" placeholder=""> 
     </div> 

     <div class="form-group col-md-6"> 
      <label for="customer name">First Name*</label> 
      <input type="text" class="form-control input-sm" name="firstname" placeholder=""> 
     </div> 
     <div class="form-group col-md-6"> 
      <label for="customer name">Last Name*</label> 
      <input type="text" class="form-control input-sm" name="lastname" placeholder=""> 
     </div> 
    <div class="form-group col-md-12"> 
      <label for="address">Address*</label> 
      <textarea class="form-control input-sm" name="address" rows="3"></textarea> 
     </div> 

    <div class="form-group col-md-4"> 
      <label for="city">City*</label> 
      <input type="text" class="form-control input-sm" name="city" placeholder=""> 
     </div> 

    <div class="form-group col-md-4"> 
      <label for="state">State*</label> 
      <input type="text" class="form-control input-sm" name="state" placeholder=""> 
     </div> 
     <div class="form-group col-md-4"> 
      <label for="pincode">Pincode</label> 
      <input type="text" class="form-control input-sm" name="pincode" placeholder=""> 
     </div> 

    <div class="form-group col-md-12"> 
      <label for="mobile">Mobile Number*</label> 
      <input type="text" class="form-control input-sm" name="mobilenumber" placeholder=""> 
     </div> 
    <div class = "form-group col-md-12"> 
      <label for="years">Id Type *</label> 


      <select class="form-control input-sm" name="idtype"> 
     <option>-- Select Id Card --</option> 
     <option>Pancard</option> 
     <option>Adhar Card</option> 
     <option>voting card</option> 


      </select> 
    </div> 

    <div class="form-group col-md-12"> 
      <label for="idnumber">Id Number</label> 
      <input type="text" class="form-control input-sm" name="idnumber" placeholder=""> 
     </div> 

    <div class="form-group col-md-6"> 
      <label for="arrival">Arrival Date</label> 
      <input type="text" class="form-control input-sm datepicker" name="Adate" placeholder=""> 
     </div> 
     <div class="form-group col-md-6"> 
      <label for="arrival">Leaving Date</label> 
      <input type="text" class="form-control input-sm datepicker" name="Ldate" placeholder=""> 
     </div> 
</div> 


<div class="col-md-12"> 
    <div class="form-group col-md-12 " > 
      <center><input type="submit" class="btn btn-primary" value="Submit" name="book"/><center> 
    </div> 
</div> 
</form> 
</div> 
</div> 

</script> 
</body> 
</html> 
+1

**警告:** mysqliを使用する場合は、[パラメータ化されたクエリ](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)と[bind_param]( http://php.net/manual/en/mysqli-stmt.bind-param.php)を使用してクエリにユーザーデータを追加します。厳しい[SQLインジェクションのバグ](http://bobby-tables.com/)を作成したため、これを行うには文字列補間や連結を使用しないでください。 **誰かがあなたの間違いを悪用しようとすると非常に危険なので、 '$ _POST'、' $ _GET'、**任意の**ユーザデータを直接クエリに入れないでください。 – FrankerZ

+0

データ属性に値を保存してからフェッチする – CyberAbhay

+0

詳細を説明してください – Yogesh

答えて

0

はあなたが好きなあなたのボタンを作成することができbuttonclickここ

<?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=>$v) 
       { 
        $str .= '<input type="button" onClick="showDiv()" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/>'; 
       } 
      } 

      return $str; 
     } 
     else { 
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($db); 
      } 

    mysqli_close($db); 
} 
?> 

<!Doctype html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>room boking</title> 

    <link href="css/bootstrap1.min.css" rel="stylesheet"> 
     <link rel="stylesheet" href="css/front.css"> 

</head> 
<body> 
<form method="post" action=""> 
<div class =" row box col-md-4" > 
<div style="color:black"><?php echo dash();?></div> 
</div> 

</form> 

フォームコードの値を取得するための私のコードです。この

$str .= '<input type="button" onClick="showDiv()" data-atribute-id="valueyouwant" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/>'; 

スクリプトは

ようにする必要があります
$("button").click(function() { 
    alert(this.id); OR 
    alert($(this).attr('id')); OR 
    alert($(this).attr('data')); OR 
    var btnid = $(this).attr('data');//which give you current clicked btn id 
}); 
0

あなたはこれを使用することができます助けることができる

// is going to select all elements that its name start with *btn_* (your dynamic buttons) 

$('[name^="btn_"]').click(function() { 
    //get the value of the button that was clicked 
    var buttonValue = $(this).val(); 

    // send data to the other file using the action attribute of the form 
    $('form').submit(); 
}); 

希望を。

関連する問題