i want to have a multiple update record function using ajax/php, but i cannot got it correctly..
here is my code:
admin_edit.php
session_start();
$lnk=mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db('world', $lnk) or die (mysql_error());
$eesn2 = $_POST['eesn2'];
$eename2 = $_POST['eename2'];
$eename3 = $_POST['eename3'];
$eename4 = $_POST['eename4'];
$eelevel2 = $_POST['eelevel2'];
$eecollege2 = $_POST['eecollege2'];
$eecourse2 = $_POST['eecourse2'];
$checkbox = $_SESSION['checkbox'];
for ($i=0; $i<count($checkbox); $i++){
$std_kors_en = array('CE','COE','EE','ECE','IE','ME','CS','IS','IT');
$std_kors_ba = array('BSA-CAS','BSA','BSBA-FM','BSBA-BEC','BSBA-MGE','BSBA-MKM','BSBA-MA','BSENT');
$std_kors_as = array('AB-Comm Arts','Tourism','HRM', 'Sec. Mgt.');
$std_kors_fa = array('BFA','BFA-Int Design','BFA-Painting','BID');
foreach ($std_kors_en as $course){
if ($eecourse2[$i] == $course)
$eecollege2 = "Engineering";
}
foreach ($std_kors_ba as $course){
if ($eecourse2[$i] == $course)
$eecollege2 = "Business Administration";
}
foreach ($std_kors_as as $course){
if ($eecourse2[$i] == $course)
$eecollege2 = "Arts and Science";
}
foreach ($std_kors_fa as $course){
if ($eecourse2[$i] == $course)
$eecollege2 = "Fine Arts";
}
$edit = $checkbox[$i];
$update="UPDATE student SET sn='$eesn2[$i]', lname='$eename2[$i]', fname='$eename3[$i]', mname='$eename4[$i]', level='$eelevel2[$i]', course='$eecourse2[$i]' WHERE sn='$edit'";
$result = mysql_query($update);
}
if($result){
echo "<form action='admin_search.php' method='post' name='edform'>";
echo "Record Updated... ";
echo "<input type='submit' name='editOK' value='OK'>";
echo "</form>";
}
?>
admin_confirm.php
session_start();
$lnk=mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db('world', $lnk) or die (mysql_error());
$_SESSION['checkbox'] = @$_POST['checkbox'];
if (!empty($_SESSION['checkbox'])){
$lnk=mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db('world', $lnk) or die (mysql_error());
$checkbox = $_SESSION['checkbox'];
echo "<form action='javascript:ajaxeditstd()' name='form5' method='post'>
<table border=3 cellspacing=5 cellpadding=3><tr><td >Student No.</td><td>Last Name</td><td>First Name</td><td>Middle Name</td><td>Level</td><td>College</td><td>Course</td></tr>";
for ($i=0; $i<count($checkbox); $i++){
$del = $checkbox[$i];
$sql = "SELECT * from student WHERE sn = '$del'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
$sn3 = $row['sn'];
$name3 = $row['lname'];
$name4= $row['fname'];
$name5= $row['mname'];
$level3 = $row['level'];
$college3 = $row['college'];
$course3 = $row['course'];
echo "<tr><input name='checkbox[]' type='checkbox' id='checkbox' value=$sn3>
<td><input type='text' name='eesn2[]' id='eesn2' value='$sn3' size=13 maxlength='11'></td>
<td><input type='text' name='eename2[]' id='eename2' value='$name3' size=20 maxlength='15'></td>
<td><input type='text' name='eename3[]' id='eename3' value='$name4' size=25 maxlength='20'></td>
<td><input type='text' name='eename4[]'id='eename4' value='$name5' size=20 maxlength='15'></td>
<td><select name='eelevel2[]' id='eelevel2' style='width:100px'>
<option value='$level3' selected>$level3</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option></select></td>
<td><input type='text' name='eecollege2[]' id='eecollege2' value='$college3' size=25></td>
<td><select name='eecourse2[]' id='eecourse2' style='width:130px'>
<option value=$course3 selected>$course3</option>
<optgroup label='College of Engineering'>
<option label='CE'>CE</option>
<option label='COE'>COE</option>
<option label='EE'>EE</option>
<option label='ECE'>ECE</option>
<option label='IE'>IE</option>
<option label='ME'>ME</option>
<option label='CS'>CS</option>
<option label='IS'>IS</option>
<option label='IT'>IT</option></optgroup>
<optgroup label='College of Business Administration'>
<option label='BSA-CAS'>BSA-CAS</option>
<option label='BSA'>BSA</option>
<option label='BSBA-FM'>BSBA-FM</option>
<option label='BSBA-BEC'>BSBA-BEC</option>
<option label='BSBA-MGE'>BSBA-MGE</option>
<option label='BSBA-MKM'>BSBA-MKM</option>
<option label='BSBA-MA'>BSBA-MA</option>
<option label='BSENT'>BSENT</option></optgroup>
<optgroup label='College of Arts and Science'>
<option label='AB-Comm Arts'>AB-Comm Arts</option>
<option label='Tourism'>Tourism</option>
<option label='HRM'>HRM</option>
<option label='Sec. Mgt.'>Sec. Mgt.</option></optgroup>
<optgroup label='College of Fine Arts'>
<option label='BFA'>BFA</option>
<option label='BFA-Int Design'>BFA-Int Design</option>
<option label='BFA-Painting'>BFA-Painting</option>
<option label='BID'>BID</option></optgroup></select></td></tr>";
}
}
echo "<tr><td colspan=7><input type='submit' name='update' value='Update'>
<input type='submit' name='back' value='Back' onClick=form5.action='admin_search.php'></td></tr>
</table></form><br>";
?>
<div id="myDiv"></div>
<?php
}
else {
echo "Please select the record you want to edit.. ";
echo "<form action='admin_search.php' method='post'><input type='submit' value='Back' name='back'></form>";
}
}
ajax_add.php
function getin(){
var objXMLHttp=null;
if (window.XMLHttpRequest){
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject){
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
var xmlhttp = getin();
var i = 0;
function ajaxeditstd(){
for (i=0; i<(document.element('checkbox[]').length); i++){
var eesn2 = [encodeURI(document.getElementsByName('eesn2[]')[i].value)];
var eename2 = [encodeURI(document.getElementsByName('eename2[]')[i].value)];
var eename3 = [encodeURI(document.getElementsByName('eename3[]')[i].value)];
var eename4 = [encodeURI(document.getElementsByName('eename4[]')[i].value)];
var eelevel2 = [encodeURI(document.getElementsByName('eelevel2[]')[i].value)];
var eecourse2 = [encodeURI(document.getElementsByName('eecourse][]')[i].value)];
var queryString = "eesn2[]=" + eesn2 + "&eename2[]=" + eename2 + "&eename3[]=" + eename3 + "&eename4[]=" + eename4 + "&eelevel2[]=" + eelevel2 + "&eecourse2[]=" + eecourse2;
xmlhttp.open("POST", "admin_edit.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange=stchange;
xmlhttp.send(queryString);
}
}
function stchange(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("myDiv").innerHTML= xmlhttp.responseText;
}