Multiple insert from dynamically added textbox array in php

While working on my E-learning project I have to face a problem how to multiple insert from dynamically added textbox array. I have an Assessment form through which a teacher give assessment to students studying a perticular course. When select semester related courses are display course combobox as well as exam and related marks fields are dynamically added from exam_schedules tables. Because based on courses types of exams and assessment policy are different. One course have 2 Assignments where another may contain 3 assignments, Class tests, midterm etc. Instructor then select student and give related marks that student obtained and when click on Save button all exams and related marks insert into casa_assessment table.


I done all functions within single page:

<form name=”frmAddAssessment” method=”post” action=”addAssessment.php”>

<input type=”hidden” name=”btnSubmit” id=”btnSubmit” value=””>

To add exams and marks field dynamically I write following codes:

$query_disp=”SELECT * FROM casa_exam_schedule WHERE swco_id

=”.”$swcoID”.” order by examschedule_id asc”; //echo $query_disp.”<br>”;
$result_disp = mysql_query($query_disp, $conn);
while($query_data_exam = mysql_fetch_array($result_disp))
<TR height=”25″>
<TD align=”right”><b>Exam</b></TD>
<TD width=”1%” align=”right”></TD>
<TD><input type=”textbox” name=”txtExamName<?php echo $k;?>”

id=”txtExamName<?php echo $k;?>” size=”35″ value=”<? echo

<TD width=”3%” align=”right”><b>Marks</b></TD>
<TD width=”3%” align=”right”></TD>
<TD><input type=”textbox” name=”txtMarks<?php echo $k;?>”

id=”txtMarks<?php echo $k;?>” size=”35″ value=””></TD>

I have a hidden input to store total rows

<input type=”hidden” name=”hidTotal” id=”hidTotal” value=”<? echo $k; ?>”>

When Instructor press Save button I write following codes to save all data with multiple insert into mysql table:

Javascript function to call save :

function submitAdd(){

And here is my php codes:

<? include(“connection.php”) ?>
$total = $_REQUEST[‘hidTotal’];
if(($_REQUEST[‘btnSubmit’]) == “Add”)
$swcoID = $_REQUEST[‘salCourseID’];
$upuser_id = $_REQUEST[‘salStudentID’];

for($i=0; $i<$total; $i++){
$exam_name = $_POST[‘txtExamName’.$i];
$marks = $_POST[‘txtStartDate’.$i];
$query = “INSERT INTO casa_assessment ( assessment_id, swco_id, upuser_id,

exam_name, marks, modified_by, mod_date) VALUES(”, “.$swcoID.”, “.$upuser_id.”,'”.$exam_name.”‘,

“.$marks .”, ‘”.$modi_by.”‘, NOW())”;
//echo $query . “<br>”;
$result = mysql_query($query, $conn) or die(mysql_error());
<script language=”javascript”>
}//end for

Now my works are completed. When all marks entered user can save all data by clicking save button.


2 Responses

  1. it is amazing, I liked very much , I was using the same idea in my project it is not e-learning , but I can’t add marks successfully , because I was used subject id, while you define new variable which is I didn’t .

  2. Excellent… just what i needed… thanks a lot champ

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: