Been struggling with this since about 10pm (2am now), and I'm sick of googling and error messages - time to ask the pros.
What I'm trying to do:If entry exists in mysql database, fill table with data
otherwise, start with a blank table and add rows dynamically with text boxes.
when user submits form, should perform validation on data (needs to be a valid code from a list)
then it should send to mysql database (if it already exists it should edit/remove as required)
What I've got so far:Javascript:
var initial_count = new Array();
var rows_limit = 0; // Set to 0 to disable limitation
function addRow(table_id)
{
var tbl = document.getElementById(table_id);
// counting rows in table
var rows_count = tbl.rows.length;
if (initial_count[table_id] == undefined)
{
// if it is first adding in this table setting initial rows count
initial_count[table_id] = rows_count;
}
// determining real count of added fields
var tFielsNum = rows_count - initial_count[table_id];
if (rows_limit!=0 && tFielsNum >= rows_limit) return false;
var text = 'Unit code:';
var input = '<input type="text" name="postvar[]" style="width:100%;"/>';
var remove= '<input type="button" value="Remove" onclick="removeRow(\''+table_id+'\',this.parentNode.parentNode)" style="width:100%;"/>';
try {
var newRow = tbl.insertRow(rows_count);
var newCell = newRow.insertCell(0);
newCell.innerHTML = text;
var newCell = newRow.insertCell(1);
newCell.innerHTML = input;
var newCell = newRow.insertCell(2);
newCell.innerHTML = remove;
} catch (ex) {
//if exception occurs
alert(ex);
}
}
function removeRow(tbl,row)
{
var table = document.getElementById(tbl);
try {
table.deleteRow(row.rowIndex);
} catch (ex) {
alert(ex);
}
}
- var initial_count = new Array();
- var rows_limit = 0; // Set to 0 to disable limitation
- function addRow(table_id)
- {
- var tbl = document.getElementById(table_id);
- // counting rows in table
- var rows_count = tbl.rows.length;
- if (initial_count[table_id] == undefined)
- {
- // if it is first adding in this table setting initial rows count
- initial_count[table_id] = rows_count;
- }
- // determining real count of added fields
- var tFielsNum = rows_count - initial_count[table_id];
- if (rows_limit!=0 && tFielsNum >= rows_limit) return false;
- var text = 'Unit code:';
- var input = '<input type="text" name="postvar[]" style="width:100%;"/>';
- var remove= '<input type="button" value="Remove" onclick="removeRow(\''+table_id+'\',this.parentNode.parentNode)" style="width:100%;"/>';
-
- try {
- var newRow = tbl.insertRow(rows_count);
- var newCell = newRow.insertCell(0);
- newCell.innerHTML = text;
- var newCell = newRow.insertCell(1);
- newCell.innerHTML = input;
- var newCell = newRow.insertCell(2);
- newCell.innerHTML = remove;
- } catch (ex) {
- //if exception occurs
- alert(ex);
- }
- }
- function removeRow(tbl,row)
- {
- var table = document.getElementById(tbl);
- try {
- table.deleteRow(row.rowIndex);
- } catch (ex) {
- alert(ex);
- }
- }
index.php:
<form id="form1" name="form1" method="post" action="setunits.php">
<h3>Set up units:</h3>
<table width="400" border="1" cellspacing="0" cellpadding="4" id="mytable">
<tr><th colspan="3"><h3>Units</h3></th></tr>
<tr><th colspan="3"><input type="button" name="Button" value="Add unit" onClick="addRow(\'mytable\')"></th></tr>
</table><br>
<input name="submitbutton" type="submit" value="Submit"/>
</form>
- <form id="form1" name="form1" method="post" action="setunits.php">
- <h3>Set up units:</h3>
- <table width="400" border="1" cellspacing="0" cellpadding="4" id="mytable">
- <tr><th colspan="3"><h3>Units</h3></th></tr>
- <tr><th colspan="3"><input type="button" name="Button" value="Add unit" onClick="addRow(\'mytable\')"></th></tr>
- </table><br>
- <input name="submitbutton" type="submit" value="Submit"/>
- </form>
setunits.php:
(no idea what this should be)
Having trouble with:
Populating the table from mysql (do I just make the table with php? not sure what to do with the javascript)
Validation - checking the data against a list in the database
Getting the array from the form (no idea here)
If I understand everything by the end of this I'll do a full tutorial on it.
In action:
http://uwadb.com/timetable/setup.php?id=unitsThanks in advance!