Can anyone point me to right direction? Im really confused (im a beginner :D). Example:
<form action="<?php $_server['php_self'] ?>" method=post>
<table width="50%" style="border: 1px solid #999;" cellspacing="0" cellpadding="0">
<tr>
<td width="35%">
<input type="button" value="Add Subitem">
<input type="button" value="Add Category">
</td>
<td width="65%">
<input type="hidden" name="row[]" value="1">
<input type="text" name="categ[]" value="Sample Category" >
</td>
</tr>
<tr>
<td>
<input type="button" value="Add Subitem">
<input type="button" value="Add Category">
</td>
<td>
<input type="hidden" name="row[]" value="2">
<input type="text" name="sub[]" value="Sample Subitem" >
</td>
</tr>
<tr>
<td>
<input type="button" value="Add Subitem">
<input type="button" value="Add Category">
</td>
<td>
<input type="hidden" name="row[]" value="3">
<input type="text" name="categ[]" value="Sample Category" >
</td>
</tr>
</table>
<input type="submit" name="insBut" value="Save">
</form>
how can i iterate in row[]? and find if that row has categ or sub? i tried this
$row = $_post['row'];
$categ = $_post['categ'];
$sub = $_post['sub'];
for ($i=0;$i<count($row);$ii){
if (isset($categ[$i])){
$insQuery = pg_query("insert into rptdata_categ (blah blah) values (blah blah)");
}elseif (isset($sub[$i])) {
$insQuery = pg_query("insert into rptdata_sub (blah blah) values (blah blah)");
}
}
but the values are wrong, i tried to echo it with
echo $row[$i]."=".$categ[$i];
and it gives
1=Sample Category
2=Sample Category
3=
i know its just right if you think about it, but how can i make it like
1=Sample Category
2=
3=Sample Category
oh and before i forget the user can add category or subitem on their own with button Add Category or Subitem.
Since you already have the desired row number (in row[]
), why not just put that in the name
of the text input, and drop the hidden one entirely?
<td>
<input type="text" name="categ[1]" value="Sample Category" />
</td>
...
<td>
<input type="text" name="sub[8]" value="Sample Subitem" />
</td>
And so on. Then the array indices of your choosing are populated already.
Any empty array field won't count on the final array. You should just add the row number to those fields, like sub[1]
, sub[2]
.. you know how many fields there are on the first place, so you can add the new fields (JS right?) with the right row-ID to it, like set the number of the base fields (3) as start point, and each time the user clicks on the button you advancing the var