Я отправляю значение флажка php на другую страницу php, в свою очередь, сохраняет этот массив данных в mysql db.
Моя форма php выглядит следующим образом:
<input type='checkbox' name='gt[]' id='gt[]' value='Yes' $checked>
Страница PHP, которая сохраняет данные формы, выглядит следующим образом:
$size = count($_POST['unitprice']);
$i = 0;
while ($i < $size) {
if (isset($_POST['gt'][$i]) && $_POST['gt'][$i] == 'Yes') {
$gt = "Yes";
} else {
$gt = "No";
}
$i++;
}
Затем следует запрос на обновление
Вся форма PHP выглядит следующим образом:
<?php
//mysql_connect("localhost","root","");
//mysql_select_db("test") or die("Unable to select database");
include('config.php');
$tender_id=$_GET['tender_id'];
echo "<form name='cart' method='post' class='single' action='generate_quot_cust_edititems_save_complete.php?tender_id=$tender_id' >\n";
echo "<input type='hidden' name='sum_input' id='sum_input' value=''>";
$sql = "select id,slno,item_name,prod_description,slab_range,qty,qty_sup,item_units,item_units_sup,unitprice,currency,total,total_inr,add_percentage,add_value,addon_value,price_unit,currency_selected,grand_total,comm_amend_req,tech_amend_req,total1,margin_for,slab_checked,margin_total from quotation_items where tender_id='$tender_id' order by slno";
//$sql = "select id,slno,item_name,prod_description,qty,item_units,unitprice,currency,total,total_inr,add_percentage,add_value,addon_value,price_unit,currency_selected,grand_total,comm_amend_req,tech_amend_req,total1 from quotation_items where tender_id='$tender_id'";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$i = 0;
echo '<div align="center"><table width="100%" border="1" style="border-collapse: collapse;" cellpadding="1" cellspacing="1">';
echo '<tr bgcolor="#E6E6FA">';
//echo "<td width='3%'> </td>";
echo '<td width=4%>SlNo</td>';
echo '<td width=17%>Description</td>';
echo '<td width=5%>Slab</td>';
echo '<td width=4%>Qty</td>';
echo '<td width=4%>Units</td>';
echo '<td width=5%>Unit Price</td>';
echo '<td width=4%>Curr</td>';
echo '<td width=6%>Total</td>';
echo '<td width=7%>Total INR</td>';
echo '<td width=5%>Add (%)</td>';
echo '<td width=5%>Add Value</td>';
echo '<td width=7%>Total Value</td>';
echo '<td width=5%>Currency</td>';
echo '<td width=7%>Total</td>';
echo '<td width=7%>Price/Unit</td>';
echo '<td width=8%>Margin</td>';
echo '</tr>';
echo '</table></div>';
$num=0;
$slno1 = 0;
//if($list5['tech_amend_req'] === '1') echo checked='checked'
while ($list5 = mysql_fetch_array($result)) {
$slno1++;
if ($list5['slab_checked'] == '') $checked = '';
if ($list5['slab_checked'] == 'No') $checked = '';
if ($list5['slab_checked'] == 'Yes') $checked = 'checked=checked';
$currency_selected=$list5['currency_selected'];
//echo $checked;
//<a href='#' class='addNew'> +</a>
echo '<div align="center" class="base">';
echo '<table width="100%" border="1" style="border-collapse: collapse;" cellpadding="1" cellspacing="1">';
echo '<tr>';
echo "<td width='4%'><input size='1' type='hidden' name='id[$i]' value='{$list5['id']}' readonly/><input size='1' type='text' name='sl[$i]' value='{$slno1}' readonly/></td>";
echo "<td width='17%' id='addinput'><input type='text' size='22' id='item_name$i' name='item_name[$i]' placeholder='{$list5['prod_description']}' value='{$list5['prod_description']}' /></td>";
echo "<td width='5%'><input size='4' class='slab_range' type='text' name='slab_range[]' id='slab_range[]' value='{$list5['slab_range']}' readonly/></td>";
echo "<td width='4%'><input size='1' class='qty' type='text' name='qty[]' id='qty[]' value='{$list5['qty_sup']}' readonly/></td>";
echo "<td width='4%'><input size='2' type='text' name='item_units[$i]' value='{$list5['item_units_sup']}' readonly/></td>";
echo "<td width='5%'><input size='4' type='text' name='unitprice[$i]' value='{$list5['unitprice']}' readonly/></td>";
echo "<td width='4%'><input size='2' type='text' name='currency[$i]' value='{$list5['currency']}' /></td>";
echo "<td width='6%'><input size='5' type='text' name='total_old' value='{$list5['total']}' readonly/></td>";
//echo "<td width='8%'><input size='4' type='text' id='total_inr[]' name='total_inr[]' value='{$list5['total_inr']}'/></td>";
$cur = $list5['currency'];
$t= $list5['total'];
$sql_cur_inr = "select * from currency1 where currency='INR'";
$result_cur_inr = mysql_query($sql_cur_inr) or die($sql_cur_inr."<br/><br/>".mysql_error());
$list_cur_inr = mysql_fetch_array($result_cur_inr);
$sql_cur = "select * from currency1";
$result_cur = mysql_query($sql_cur) or die($sql_cur."<br/><br/>".mysql_error());
while ($row_cur = mysql_fetch_array($result_cur)) {
$rate = $row_cur['rate'];
$rate_inr = $list_cur_inr['rate'];
}
echo "<td width='7%'><input size='7' type='text' id='total_inr[]' name='total_inr[]' value='{$list5['total_inr']}'/></td>";
//echo "<td width='8%'><input size='10' type='text' id='total_inr[]' name='total_inr[]' value='{$total_inr}' /></td>";
echo "<td width='5%' ><input class='' size='2' type='text' id='add_percentage[]' name='add_percentage[]' value='{$list5['add_percentage']}' onchange='calcTotals()'></td>";
echo "<td width='5%'><input class='txt' type='text' size='3' id='addon_value[]' name='addon_value[]' value='{$list5['addon_value']}' onchange='calcTotals()' ></td>";
echo "<td width='7%'><input class='total' size='7' type='text' id='add_value[]' name='add_value[]' value='{$list5['add_value']}' ></td>";
?>
<?php
$currency_selected1=$list5['currency_selected'];
$selectedValue = "$currency_selected1"; // assign that value to this variable
//echo $selectedValue;
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change[]' name='currency_change[]'>";
echo "<option value=''>select</option>";
/*
* selected value
*/
$selectedValue = "$currency_selected1"; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
//echo "<option ".$selected." disabled hidden value=''>select</option>";
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>
<?php $sql_cur = "select * from currency1 where currency='INR' LIMIT 1";
$result_cur = mysql_query($sql_cur) or die($sql_cur."<br/><br/>".mysql_error());
$list_cur = mysql_fetch_array($result_cur);
?>
<input type="hidden" class="inrvalue" id="inrvalue" name="inrvalue" value="<?php echo $list_cur['rate']; ?>">
<input type="hidden" class="deptip" id="dept-input"/>
<input type="hidden" class="priceip" id="price-input"/>
<input type="hidden" class="cs" id="cs" name="cs" value="<?php echo $list5['currency_selected']; ?>">
<td width="7%"><input size="7" type="text" data-value="<?php $list5['total1'] ?>" name='total1[]' id='total1[]' value="<?php echo $list5['total1'] ?>" readonly class="total1"/></td>
<?php
echo "<td width='7%'><input class='price_unit' size='7' type='text' id='price_unit[]' name='price_unit[]' value='{$list5['price_unit']}'
readonly></td>";
echo "<td width='8%'><input type='checkbox' name='gt[]' id='gt[]' value='Yes'><input size='8' type='text' id='margin_for[]' name='margin_for[]' value='{$list5['margin_for']}' readonly></td>";
echo "</tr>";
echo '';
++$i;
}
$sql = "select grand_total,margin_total, sum(total_inr) as totalinr_total from quotation_items where tender_id='$tender_id' LIMIT 1";
$result_gt = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$list_gt = mysql_fetch_array($result_gt);
//echo '<table width="100%" border="1" style="border-collapse: collapse;" cellpadding="1" cellspacing="1">';
echo '<tr>';
echo '<td> </td>';
echo '<td> </td>';
echo '<td> </td>';
echo '<td> </td>';
echo '<td> </td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo "<td><input type='text' style='font-weight: bold' name='total_inr1' id='total_inr1' size='8' value='{$list_gt['totalinr_total']}' readonly /></td>";
echo "<td></td>";
echo '<td></td>';
echo "<td><input type='text' style='font-weight: bold' name='gTotal' id='grand_total' size='7' value='{$list_gt['grand_total']}' readonly /></td>";
echo "<td></td>";
echo '<td></td>';
echo "<td></td>";
echo "<td><input type='text' style='font-weight: bold' id='margin_total' name='margin_total' size='8' value='{$list_gt['margin_total']}' readonly /></td>";
echo '</tr>';
echo '</table></div>';
?>
<br>
<?php
$validity_chk=$list['validity_chk'];
$incoterm_chk=$list['incoterm_chk'];
$freight_chk=$list['freight_chk'];
$cdcterms_chk=$list['cdcterms_chk'];
$poto_chk=$list['poto_chk'];
$paymentterms_chk=$list['paymentterms_chk'];
$delivery_chk=$list['delivery_chk'];
?>
<input type="hidden" name="validity_chk" id="validity_chk" value="0" />
<input type="hidden" name="incoterm_chk" id="incoterm_chk" value="0" />
<input type="hidden" name="freight_chk" id="incoterm_chk" value="0" />
<input type="hidden" name="cdcterms_chk" id="cdcterms_chk" value="0" />
<input type="hidden" name="poto_chk" id="poto_chk" value="0" />
<input type="hidden" name="paymentterms_chk" id="paymentterms_chk" value="0" />
<input type="hidden" name="delivery_chk" id="delivery_chk" value="0" />
<?php include('note.php');?><br>
<?php include('terms.php');?>
<table width="100%">
<tr>
<td align="left" height="60">Authorized Signature</td>
</tr>
<tr>
<td align="left">
<?php
$t5 = $list['po_to'];
if ($t5 == "Umac Bangalore") {
echo 'Sandhya';
} else if ($t5 == "Umac Singapore") {
echo 'Arijith Das';
}
else {
echo "";
}?>
</td>
</tr>
</table>
</div>
<?php
echo "<table border='0' width='13%'><td><input type='submit' value='--Save Data--' /></td>";
echo '';
echo '';
echo '';
?>
И следующее — полная часть php, которая сохраняет данные формы:
<?php
include('config.php');
$tender_id=$_GET['tender_id'];
$size = count($_POST['unitprice']);
echo '<pre>' . print_r($_POST, 1) . '</pre>';
$i = 0;
while ($i < $size) {
$item_name= $_POST['item_name'][$i];
$currency= $_POST['currency'][$i];
//$add_percentage_v= $_POST['add_percentage_v'.$i];
$add_percentage= $_POST['add_percentage'][$i];
$add_value= $_POST['add_value'][$i];
$addon_value= $_POST['addon_value'][$i];
$price_unit= $_POST['price_unit'][$i];
$total_inr= $_POST['total_inr'][$i];
$margin= $_POST['margin_for'][$i];
//$gt=$_POST['gt'][$i];
//$gt = $_POST['gt'][$i];
//if ($gt != 'Yes') {
// $gt = 'No';
//}
if (isset($_POST['gt'][$i]) && $_POST['gt'][$i] == 'Yes') {
$gt = "Yes";
} else {
$gt = "No";
}
$total1=$_POST['total1'][$i];
$currency_selected=$_POST['currency_change'][$i];
$sum_input= $_POST['gTotal'];
$margin_total= $_POST['margin_total'];
$totalinr_total= $_POST['total_inr1'];
$id = $_POST['id'][$i];
$query = "UPDATE quotation_items SET prod_description = '$item_name',currency = '$currency',add_percentage = '$add_percentage',addon_value = '$addon_value',add_value = '$add_value',grand_total = '$sum_input', price_unit='$price_unit', currency_selected='$currency_selected', total1='$total1', total_inr='$total_inr', margin_for='$margin', slab_checked='$gt', margin_total='$margin_total', totalinr_total='$totalinr_total' WHERE id = '$id' and tender_id='$tender_id' ";
mysql_query($query) or die ("Error in query: $query");
echo "$item_name<br />$add_value<br />$margin<br /><em>Quot Updated!</em><br /><br />";
++$i;
}
?>
Если вы выполните следующий код, вы увидите, что $ result вернет массив выбранных значений флажков.
<?php
if (isset($_POST['submit'])) {
$result = $_POST['gt'];
print_r($result);
}
?>
Непроверенные значения флажков не представлены в массиве $ _POST, поэтому вы должны заменить значение флажка «Да» на фактическое значение.
Простой грязный пример, не относящийся к mvc (вы можете проверить это в файле index.php вашего webroot, чтобы прояснить ситуацию):
<?php
$data = array(
array('id' => 1, 'name' => 'coding'),
array('id' => 2, 'name' => 'database'),
array('id' => 3, 'name' => 'html/css'),
);
?>
<form action="" method="post">
<table>
<?php foreach ($data as $row) { ?>
<tr>
<td>
<input type="hidden" name="id[]" value="<?php echo $row['id'] ?>">
<input type="hidden" name="name[]" value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?>
</td>
<td>
<input type="checkbox" name="hobby[<?php echo $row['id'] ?>]" value="1">
</td>
</tr>
<?php } ?>
</table>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$l = count($_POST['id']);
for ($i = 0; $i < $l; $i++) {
$id = intval($_POST['id'][$i]);
$checked = isset($_POST['hobby'][$id]) ? 'checked' : 'not checked';
echo htmlspecialchars($_POST['name'][$i]) . " was $checked<br>";
}
}
?>
Или увидеть работоспособный