[英]Posting data when text input is not empty PHP
我有一個表格,提供3列,產品代碼,產品名稱和復選框。
它目前適用於它基本上將發布已經檢查過的任何產品,但我想將功能更改為文本框而不是復選框,並且只發布文本框中包含某些內容的數據,我一直在修補它徒勞無功。
這是我的表:
<form id="frm-example" action="price-quote.php" method="POST">
<table id="datatable-price" class="table table-striped table-bordered">
<thead>
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Add to quote</th>
</tr>
</thead>
<tbody>
<?php while($res = sqlsrv_fetch_array($def, SQLSRV_FETCH_ASSOC)) : ?>
<tr>
<td><?php echo $res['ItemCode']; ?></td>
<td><?php echo $res['ItemName']; ?></td>
<td>
<input type="checkbox" name="checkbox[]" value="<?php echo $res['ItemCode'];?>"></input>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<input type="submit" id="submit" name="submit" class="btn btn-success pull-right" value="Quote"></input>
</form>
這是與它一起使用的javascript:
<script>
$(document).ready(function (){
var table = $('#datatable-price').DataTable({
});
// Handle click on "Select all" control
$('#datatable-price-select-all').on('click', function(){
// Get all rows with search applied
var rows = table.rows({ 'search': 'applied' }).nodes();
// Check/uncheck checkboxes for all rows in the table
$('input[type="checkbox"]', rows).prop('checked', this.checked);
});
// Handle click on checkbox to set state of "Select all" control
$('#datatable-price tbody').on('change', 'input[type="checkbox"]', function(){
// If checkbox is not checked
if(!this.checked){
var el = $('#datatable-price-select-all').get(0);
// If "Select all" control is checked and has 'indeterminate' property
if(el && el.checked && ('indeterminate' in el)){
// Set visual state of "Select all" control
// as 'indeterminate'
el.indeterminate = true;
}
}
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
// Iterate over all checkboxes in the table
table.$('input[type="checkbox"]').each(function(){
// If checkbox doesn't exist in DOM
if(!$.contains(document, this)){
// If checkbox is checked
if(this.checked){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', this.name)
.val(this.value)
);
}
}
});
});
});
</script>
這是我發布的頁面:
<?php
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check){
$abc = "SELECT ItemCode, ItemName
FROM OITM
WHERE ItemCode = '$check'
ORDER BY ItemName";
$def = sqlsrv_query($sapconn, $abc);
$res = sqlsrv_fetch_array($def, SQLSRV_FETCH_ASSOC);
$prodCode = $res['ItemCode'];
$prodName = $res['ItemName'];
echo "
<tr>
<td>$prodCode</td>
<td>$prodName</td>
</tr>
";
}
}
?>
任何人都可以通過文本框而不是復選框來修改此文本。 這將是偉大的,非常感謝!
提前致謝
我沒有得到你想做什么,但有很多方法可以做。
首先,您可以更改復選框的值以執行多重值字段(不推薦)。 作品。
像這樣的東西:
<input type="checkbox" name="checkbox[]" value="<?php echo $res['ItemCode'];?>|<?php echo $res['ItemName'];?>"></input>
在PHP中,您獲得並分割價值。
其次,您可以添加隱藏字段。 當復選框標記為您創建動態隱藏的值和文本是可取的。
您可以繼續使用復選框傳遞代碼。 發布后你使用內爆 。 您需要這樣做,因為您可以收到超過1個項目。
<?php
if(!empty($_POST['checkbox'])) {
$in = implode(",",$_POST['checkbox']);
foreach($_POST['checkbox'] as $check){
$abc = "SELECT ItemCode, ItemName FROM OITM WHERE ItemCode in ('. $in . ') ORDER BY ItemName";
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.