I am posting the code again with little bit changes as you said. Please review my code and help me to solve my problem
Code:
<?php
$query = "SELECT *
FROM ss_income_category
ORDER BY icategory_id";
$result = mysql_query($query);
$categoryCount = mysql_num_rows($result);
?>
Here's the Javascript Code:
Code:
<script type="text/javascript">
function ajaxpage(category, containerid)
{
var strURL="getSourceList.php?category_name="+category;
var req = false;
if (window.XMLHttpRequest) // if Mozilla, Safari etc
req = new XMLHttpRequest();
else if (window.ActiveXObject)
{ // if IE
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
else
return false;
req.onreadystatechange=function()
{
loadpage(req, containerid);
}
req.open('GET', strURL, true);
req.send(null);
}
function loadpage(req, containerid)
{
if (req.readyState == 4 && (req.status==200))
document.getElementById(containerid).innerHTML = req.responseText;
}
function addRowToTable() {
var iTable = document.getElementById('addIncomeTable');
var rowNo = iTable.rows.length;
var iteration = rowNo;
var row = iTable.insertRow(-1);
/* Adding left most td */
var textCell = row.insertCell(0);
var textNode = document.createTextNode(iteration);
textCell.appendChild(textNode);
/* Adding category select box */
categoryCell = row.insertCell(1);
var categorySelect = document.createElement('select');
categorySelect.name = 'category_name' + iteration;
categorySelect.id = 'category_name' + iteration;
var rowName = 'sourcediv-' + iteration;
categorySelect.options[0] = new Option('-- <?php echo 'Select Category Name'; ?> --', '-1');
<?php
for($i=1; $i<=$categoryCount; $i++)
{
$record = mysql_fetch_array($result);
?>
categorySelect.options[<?php echo $i ?>] = new Option('<?php echo $record['description']; ?>', '<?php echo $record['description']; ?>');
<?php
}
?>
categorySelect.onchange = function(){ajaxpage(this.value, 'rowName');};
categoryCell.appendChild(categorySelect);
/* Adding source select box */
sourceCell = row.insertCell(2);
var sourceRow = document.createElement('td');
sourceRow.name = 'sourcediv-'+iteration;
var sourceSelect = document.createElement('select');
sourceSelect.name = 'source' + iteration;
sourceSelect.id = 'source' + iteration;
sourceSelect.options[0] = new Option('-- <?php echo 'Select Category First'; ?> --', '-1');
sourceRow.appendChild(sourceSelect);
sourceCell.appendChild(sourceRow);
/* Adding input box */
amountCell = row.insertCell(3);
var amountInput = document.createElement('input');
amountInput.type = 'text';
amountInput.name = 'amount'+ iteration;
amountInput.id = 'amount' + iteration;
amountCell.appendChild(amountInput);
}
</script>
Here, is the html cde:
Code:
<body>
<div id="content">
<div id="back">
<?php include("header.php"); ?>
<!-- content begins -->
<div id="main">
<div id="right">
<h3>Add New Income</h3>
<form id="addIncomeForm1" name="addIncomeForm1" method="post" action="">
<div class="tabber" >
<div class="tabbertab">
<h2>Add Income</h2>
<b><font color="#0033CC">Step 1:</font> Please select the income category to add</b><br /><br />
<input name="addRow" type="button" value="Add New Row" class="myFormButton" onclick="addRowToTable()" />
<table id="addIncomeTable" class="myTable">
<tr align='justify'>
<th> </th>
<th> Category Name</th>
<th> Source Name</th>
<th> Amount</th>
</tr>
<tr align='justify' style="background-color:<?php echo $row_color ?>" > <?php $k = 0; ?>
<td><?php echo $k+1; ?></td>
<td>
<select id="category_name-0>" name="category_name-0" onchange="ajaxpage(this.value, 'sourcediv')">
<option value="-1" selected="selected">-- Select Category Name --</option>
<?php
//Create query
$query1 = "SELECT * FROM ss_income_category ORDER BY icategory_id";
$result1 = mysql_query($query1);
while($row1 = mysql_fetch_array($result1))
{
?>
<option value="<?php echo $row1['description']; ?>">
<?php echo $row1['description'] ?>
</option>
<?php
}
?>
</select>
</td>
<td id="sourcediv">
<select id="source-0" name="source-0" >
<option value="-1" selected="selected">-- Select Category First--</option>
</select>
</td>
<td><input type="text" id="amount-0" name="amount-0" /></td>
</tr>
</table>
</div>
<br />
<div align="center">
<input name="btnSave" type="submit" value="Save" class="myFormButton" onClick="return addIncome();" />
<input name="btnReset" type="reset" value="Reset" class="myFormButton" />
<input name="btnCancel" type="button" value="Cancel" class="myFormButton" onClick="window.location.href='show_all_incomes.php';" />
</div>
</div>
</form>
</div>
<?php include("menu.php"); ?>
</div>
</div>
<!--content ends -->
</div>
<!-- footer ends-->
</body>
Bookmarks