当我更新文本框中的字段时,代码从数据库中检索数据并更新它。但当我将其更改为dropdownlist时,它不会检索数据,也不会更新它们的问题。
<form>
<div class="modal-body">
<input type="hidden" id="<?php echo $row['P_id'] ?>" value="<?php echo $row['P_id'] ?>">
<!-- Select parking type -->
<div class="form-group">
<label for="type">Parking Type</label>
<?php $connect=m ysqli_connect( "localhost", "root", "root", "pms"); $dbQuery="SELECT * FROM parking_type" ; $dbResult=m ysqli_query($connect,$dbQuery); echo '<select id="typ-'.$row[ 'P_id']. '">'; while($rw=m ysqli_fetch_array($dbResult)) { echo
'<option value="'.$rw[0]. '" > '.$rw[1]. ' </option>'; } echo "</select>"; ?>
</div>
<!-- Edit parking code -->
<div class="form-group">
<label for="pc">Parking Code</label>
<input type="text" class="form-control" id="pno-<?php echo $row['P_id'] ?>" value="<?php echo $row['P_code'] ?>">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" onclick="updateData(<?php echo $row['P_id'] ?>)" class="btn btn-primary">Update</button>
</div>
</form>
答案 0 :(得分:0)
好的,我已经更改了你的代码,请看这里尝试一下:
您的表单代码:
<form id="jsForm" action="pserver.php" type="POST">
<div class="modal-body">
<input type="hidden" name="P_id" value="<?php echo $row['P_id']; ?>">
<input type="hidden" name="action" value="edit">
<!-- Select parking type -->
<div class="form-group">
<label for="<?php echo 'typ-'.$row[ 'P_id']; ?>">Parking Type</label>
<?php
$connect = mysqli_connect( "localhost", "root", "root", "pms");
$dbQuery = "SELECT * FROM parking_type" ;
$dbResult = mysqli_query($connect,$dbQuery);
echo '<select name="typ-'.$row[ 'P_id']. '">';
while($rw = msqli_fetch_array($dbResult)) {
echo '<option value="'.$rw[0]. '" > '.$rw[1]. ' </option>';
}
echo "</select>";
?>
</div>
<!-- Edit parking code -->
<div class="form-group">
<label for="pno-<?php echo $row['P_id']; ?>">Parking Code</label>
<input type="text" class="form-control" name="pno-<?php echo $row['P_id']; ?>" value="<?php echo $row['P_code']; ?>">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
<script>
//hang on event of form with id=myform
$("form#jsForm").on('submit', function(e) {
//prevent Default functionality
e.preventDefault();
//get the url of the form
var url = $(this).attr('action');
//get form values
var values = $(this).serialize();
//do your own request an handle the results
$.ajax({
url: 'pserver.php', // or set "url" var, if form action defined
type: 'POST',
//dataType: 'json',
data: values,
success: function(resp) {
//alert(resp);
viewData();
}
});
});
</script>
你的PHP代码的一部分:
<?php
if(isset($_POST['action'], $_POST['P_id']) && $_POST['action'] == "edit" && !empty($_POST['P_id'])) {
$P_id = $_POST['P_id'];
$type = isset($_POST["typ-$P_id"]) ? $_POST["typ-$P_id"] : false;
$p_no = isset($_POST["pno-$P_id"]) ? $_POST["pno-$P_id"] : false;
if($type && $p_no) {
$stmt = $db->prepare("update parking_details set t_id=?, P_code=? where P_id=?");
$stmt->bindParam(1,$type); // you can add pdo validation here (read more http://php.net/manual/en/pdostatement.bindparam.php)
$stmt->bindParam(2,$p_no); // and here
$stmt->bindParam(3,$P_id); // and here
$stmt->execute();
}
}
?>