2017-05-20 8 views
0

Ich habe dynamische Tasten in PHP erstellt. Ich brauche den Wert dieser Schaltfläche, wenn eine bestimmte Schaltfläche geklickt wird. Und wenn wir auf die Schaltfläche klicken, werden alle Informationen in das Formular abgerufen.Wie erhält man einen dynamischen Wert, wenn man in PHP auf die Schaltfläche klickt?

Hier ist mein Code für Button

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

hier den Wert in Form Code Abrufen

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

** ACHTUNG: ** Wenn Sie 'mysqli' verwenden, sollten Sie [parametrisierte Abfragen] (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) und [bind_param] (http://php.net/manual/en/mysqli-stmt.bind-param.php), um Benutzerdaten zu Ihrer Abfrage hinzuzufügen. Verwenden Sie NICHT die String-Interpolation oder Verkettung, um dies zu erreichen, weil Sie einen schwerwiegenden [SQL-Injektionsfehler] (http://bobby-tables.com/) erstellt haben. ** NIEMALS ** $ _POST ',' $ _GET' oder ** beliebige ** Benutzerdaten direkt in eine Abfrage einfügen, da es sehr schädlich sein kann, wenn jemand versucht, Ihren Fehler auszunutzen. – FrankerZ

+0

Speichern Sie Ihren Wert in Daten-Attribut und holen Sie dann mit ihm – CyberAbhay

+0

bitte erklären Sie im Detail – Yogesh

Antwort

0

Sie können Ihre Schaltfläche wie folgt

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

Script sollte wie

sein erstellen
$("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

Sie können diese verwenden:

// 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(); 
}); 

Hoffnung, die helfen können.

Verwandte Themen