Hello i have a form that have an button to add more fields this fields will have same name so i use array for the name value her is the form code
[CODE]
<div class=”row”>
<div class=”col-md-12″>
<!– form to add charges –>
<form action=”<?php echo htmlspecialchars($_SERVER[‘PHP_SELF’]);?>” method=”post”>
<table id=”tb” class=”table table-striped table-bordered table-hover”>
<tr style=”border-bottom: 3px solid black;”>
<td>Year</td> <td><input type=”text” name=”year” id=”year”/></td>
<td>Month</td> <td><input type=”text” name=”month” id=”month”/></td>
</tr>
<tr>
<td>Charge</td> <td><input type=”text” name=”charge[]” id=”charge”/></td>
<td>descritpion </td> <td><input type=”text” name=”descritpion[]” id=”descritpion”/></td>
<td>value </td> <td><input type=”text” name=”value[]” id=”value”/></td>
</tr>
<tr id=”newf” >
<td> <button type=”button” name=”new” onClick=”addField()”>add field</button></td>
</tr>
<tr>
<td><input type=”submit” name=”sub” value=”submit”/></td>
</tr>
</table>
<!– script to add more fields –>
<script type=”text/javascript”>
function addField(){
var field =
“<tr><td>Charge</td> <td><input type=’text’ name=’charge[]’ id=’charge’/></td><td>descritpion </td> <td><input type=’text’ name=’descritpion[]’ id=descritpion’/></td><td>value </td> <td><input type=’text’ name=’value[]’ id=’value’/></td></tr>”;
$(“#newf”).before(field);
}
</script>
</form>
</div>
</div>
the problem is when i treat the post variable it doesn’t work for validation or even insert the values to data base
what i tried to do is validate the variables then isert the variables ‘year’ and ‘month’ to table ‘months’ and insert other variables to table ‘charges’
[code=php]
require_once(‘inc/connection/conn.php’);
//–set variables
$year = ”;
$month = ”;
$charge = ”;
$desc = ”; require_once(‘inc/connection/conn.php’);
//–set variables
$year = ”;
$month = ”;
$charge = ”;
$desc = ”;
$val = ”;
//–test POST vars
if($_SERVER[‘REQUEST_METHOD’]==’POST’) {
$year = test_input($_POST[‘year’]);
$month = test_input($_POST[‘month’]);
for ($i=0;$i<count($_POST[‘charge’]);$i++) {
$charge = test_input($_POST[‘charge’][$i]);
}
for ($i=0;$i<count($_POST[‘descritpion’]);$i++) {
$desc = test_input($_POST[‘descritpion’][$i]);
}
for ($i=0;$i<count($_POST[‘value’]);$i++) {
$val = test_input($_POST[‘value’][$i]);
}
$sql1 = “INSERT INTO months (year, month) VALUES ($year, $month)”;
$conn->query($sql1);
for ($i=0;$i<count($charge);$i++) {
$sql = “INSERT INTO charges (designation, value, description) VALUES ($charge[$i], $desc[$i], $val[$i])”;
}
if($conn->query($sql) === TRUE) {
$msg = ‘add to db succefuly’;
}else {
$msg = ‘failure’;
}
}
//–function test
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
require_once(‘inc/header.php’);
require_once(‘inc/sidebar.php’);
please help me thank you