here is my form:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
function formValidate(frm)
{
alert("come to form validate");
var errorFlag = false;
var errorMsg = "";
var tbl = document.getElementById('tblTimesheet');
var numOfRows = tbl.tBodies[0].rows.length;
alert("num of rows = " + numOfRows);
for (i=1; i<=numOfRows; i++)
{
var empNameValue = document.getElementById('empName' + i).value;
var noteValue = document.getElementById('note' + i).value;
var hoursValue = document.getElementById('hours' + i).value;
if( empNameValue == "" || noteValue == "" || hoursValue == "" )
{
errorMsg = "can not leave any field empty.";
errorFlag = true;
}
}
if(errorFlag == true)
{
alert(errorMsg);
return false;
}
else
{
document.getElementById('numOfRowsSubmit').setAttribute("value",numOfRows);
//return true;
//submit form
frm.submit();
}
}
}
</script>
</head>
<body>
<form action="dosomthing.php" method="post" name="myfrm">
<input type="hidden" name="check_submit" value="1" />
<input type="hidden" name="numOfRowsSubmit" id="numOfRowsSubmit" value="" />
<input type="button" value="Save" onclick="formValidate(this.form)" />
<table id="tblTimesheet">
<thead>
<tr><th>name</th><th>note</th><th>hours</th></tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="empName1" id="empName1" size="30" /></td>
<td><input type="text" name="note1" id="note1" size="30" /></td>
<td><input type="text" name="hours1" id="hours1" size="30" /></td>
</tr>
<tr>
<td><input type="text" name="empName2" id="empName2" size="30" /></td>
<td><input type="text" name="note2" id="note2" size="30" /></td>
<td><input type="text" name="hours2" id="hours2" size="30" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Code: Select all
<?php
$frmSubmitted = $_REQUEST['check_submit'];
if (isset($frmSubmitted))
{
$message = "data valid and form posted.";
$numOfRows = $_REQUEST['numOfRowsSubmit'];
$message .= "<br />number of rows submitted: " . $numOfRows;
//for($i=1; $i <= $numOfRows; $i++)
for($i=1; $i <= 2; $i++)
{
$nameRow = 'empName' . $i;
$noteRow = 'note' . $i;
$hoursRow = 'hours' . $i;
$message .= "<br />name Row " . $i . " = " . $_REQUEST[$nameRow];
$message .= "<br />note Row " . $i . " = " . $_REQUEST[$noteRow];
$message .= "<br />hours Row " . $i . " = " . $_REQUEST[$hoursRow];
}
}
else
{
$message = "form not posted.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<!--begin right main information -->
<!-- retrieve time sheet information from database -->
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="1">
<tr>
<td>
<?php echo $message; ?>
</td></tr>
</table>
<!-- end retrive time sheet information -->
<!-- end right main information -->
</body>
</html>