/    Sign up×
Community /Pin to ProfileBookmark

treat multiple inputs with same name prob

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>
[/CODE]

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’);
[/code]

please help me thank you

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 08.2015 — You need to put the database stuff inside a loop, which could just be one foreach() loop, something like (untested):
[code=php]
foreach($_POST['charge'] as $ix => $charge)
{
if(empty($_POST['descritpion'][$ix]) or empty($_POST['value'][$ix]))
{
// however you want to handle this condition
}
else
{
// here you can do your query using $charge, $_POST['descritpion'][$ix], and $_POST['value'][$ix]
}
}
[/code]
Copy linkTweet thisAlerts:
@rootApr 08.2015 — succefuly?
×

Success!

Help @channark spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...