Jump to content

AJAX multiple edit..


pchsmvp
 Share

Recommended Posts

want to have a multiple update record function using ajax/php, but i cannot got it correctly.. nothing is happening when i click the "Update" button.. 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<b></b>: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.js

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;}

Link to comment
Share on other sites

so that's a No then. you should really learn to try and debug your applications. Depending on the browser you are using, you should be able to look up how to access its error console. This will tell you if you are encountering any Javascript errors. Firebug for Firefox is very good. You can also use it to see the status of AJAX calls. You can also use alert() statements in your javascript functions to track variables as they go through your script, to make sure you are handling values that you are expecting. In php you can use echo to do the same thing, albeit not in a pop-up.

Link to comment
Share on other sites

To check for PHP errors, in Firebug you could click on the ajax request that went out, and click on the request's response tab. The response is the output from the server, if PHP is printing regular error messages you'll see them listed in the response.With PHP called using ajax, it's often easier to use error logging instead. If you add this code to the top of your PHP script:

ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('display_errors', 0);error_reporting(E_ALL);

That will cause all PHP errors to be sent to a file called error.log in the same folder as the PHP script. You can check the error log to look for PHP errors. You may also want to just point your browser straight to the PHP files to see if they even display at all.The first step in solving this problem is figuring out what the error is, these steps and the suggestions from thescientist will help you do that.

Link to comment
Share on other sites

here is my new code ajax_add.js

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 ct = 0;function ajaxeditstd(){ for (ct=0; ct<2; ct++){var checkbox = [encodeURI(document.getElementsByName('checkbox[]').value)];var eesn2 = [encodeURI(document.getElementsByName('eesn2[]')[ct].value)];var eename2 = [encodeURI(document.getElementsByName('eename2[]')[ct].value)];var eename3 = [encodeURI(document.getElementsByName('eename3[]')[ct].value)];var eename4 = [encodeURI(document.getElementsByName('eename4[]')[ct].value)];var eelevel2 = [encodeURI(document.getElementsByName('eelevel2[]')[ct].value)];var eecourse2 = [encodeURI(document.getElementsByName('eecourse2[]')[ct].value)];var queryString = "checkbox[]=" + checkbox + "&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;}

here is the error..Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 62Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 66Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 70Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 70Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 70Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 70Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 74Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 74Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 74Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 74Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 80Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 80Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 80Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 80Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 80Notice: Undefined offset: 1 in C:\wamp\www\syspro\admin_edit.php on line 80the code is working if i only edit 1 record, but if i edit 2 record, i got the errors..i know the error is on the array but i don't know what to change in admin_edit.php

Edited by pchsmvp
Link to comment
Share on other sites

You should change the Javascript part, right now it's looping and sending 1 request per loop. It should be looping to gather all of the data, and then send the request with everything. So you should be looping to build the queryString variable, and then send the whole thing after it's done. You can use alert to print the queryString variable while you build it so that you can make sure it's being built right.

Link to comment
Share on other sites

You should change the Javascript part, right now it's looping and sending 1 request per loop. It should be looping to gather all of the data, and then send the request with everything. So you should be looping to build the queryString variable, and then send the whole thing after it's done. You can use alert to print the queryString variable while you build it so that you can make sure it's being built right.
can you edit my code, please?i'm just a newbie, sorry.. :)i already tried different things but i still can't do it..
Link to comment
Share on other sites

I could, but the only thing you would learn is to ask a question on a forum when you have a problem. I would rather teach you how to solve the problem yourself.So what the code needs to do is to build a string of data to submit, and then submit it. That's not real difficult, you're just collecting data. This page describes using the + operator to join strings:http://www.w3schools.com/js/js_operators.aspThat's the same thing you need to do, your string of data starts empty and every time through the loop you add more things to it. e.g.:

var data = '';for (var ct=0; ct<2; ct++){  data += 'more data ';}alert(data);

Your loop is more-or-less fine, there are some small issues with it but in general it's doing the right thing. You don't want to use var with variables inside the loop, that will keep redefining everything. You don't need to use var on any variable that is already defined. You also have these square brackets around all of the values:checkbox = [encodeURI(document.getElementsByName('checkbox[]').value)]Why do you have those brackets there, are you sure you need them? Do you know what they do? This page describes using the brackets, you use those when you are defining an array:http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

Link to comment
Share on other sites

that's what i want sir.. i want to pass an array, so that i can edit multiple records in mysql..

var data = '';for (var ct=0; ct<2; ct++){  data += 'more data ';}alert(data);

when i done that, the variables add up and come up with 1 value..thanks for helping sir.. sorry if i'm keep on asking.. :) i badly need it for my project..

Link to comment
Share on other sites

that's what i want sir.. i want to pass an array, so that i can edit multiple records in mysql..
var data = '';for (var ct=0; ct<2; ct++){  data += 'more data ';}alert(data);

when i done that, the variables add up and come up with 1 value..thanks for helping sir.. sorry if i'm keep on asking.. :) i badly need it for my project..

if you are trying to pass an array, you are going to have to construct an array. Have you read up on how array's are made?http://www.w3schools.com/js/js_obj_array.aspIt looks like though, that you are trying to get a number of inputs from the user, and then based on the user input (boxes checked) you handle some sort of database task. I'm not sure an array is necessarily what you might want though. It would seem you would just want to find out which boxes have been checked, and then pass the name of the checkboxes to your server-side script so that it knows which fields to manipulate in the database. (as JSG suggested through the use of the querystring) Edited by thescientist
Link to comment
Share on other sites

You can't really pass an array directly from Javascript to PHP. There are two tricks for doing so. The first is to use JSON and encode your array as a JSON string, send that to PHP, and PHP can decode it back into an array. That may be a little more than what you're looking for. The other way is the way that you can submit something like a series of checkboxes to PHP and it will show up as an array on the PHP side. To do that, you should just be able to specify all elements with a name like you're using that ends in brackets, and PHP will build the array from that data.But that's different than an array in Javascript though. You don't want to send PHP a Javascript array, you want to send it all of the data and have it build its own array. If you wanted to send an array of field values, you could do it like this:

var data = '';var val = '';for (ct=0; ct<2; ct++){  val = encodeURI(document.getElementsByName('field_name').value[ct]);  data += '&field[]=' + val;}alert(data);

After that, data may be a string like this:&field[]=one&field[]=two&field[]=threeThat will cause PHP to build an array called $_POST['field'] with the three values in it. It sounds like that's what you want. So you don't want Javascript arrays, you want PHP arrays. So you don't need the [] in the field names, because those names aren't being sent to PHP, only the names and values in the data string get sent. You can build the data string and then send it along and PHP will build the arrays for you to loop through.Also, for encoding the values it will be better to use encodeURIComponent instead of encodeURI.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...