如何在PHP中单击按钮时获取动态按钮值?

时间:2017-05-20 09:55:23

标签: javascript php html css

我在php中创建了动态按钮。单击特定按钮时,我需要获取该按钮的值。当我们点击按钮时,所有信息都会被提取到表单中。

这是我的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>

这里获取表单代码中的值

<!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>

2 个答案:

答案 0 :(得分:0)

您可以像这样创建按钮

$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
});

答案 1 :(得分: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();
});

希望可以提供帮助。