473,231 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Inserting multiple record in MYSQL one at a time sequentially using AJAX

Hi All,
This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in Database but I'm stuck in the EDIT. I'm getting 2 problems over here. Below is the description:

1)The FIRST page will list all the records from the table which Admin can EDIT with CHECKBOX for each record to select. He can select one or more than one record to EDIT.
2)Now if he selects ONLY ONE record, then in the SECOND page where he will enter NEW values for record and submit the form then the JavaScript which I had written there in is not working. It is not getting the FORM values in variables.
3)But if he selects more than one then the script is working fine and giving me correct values for all the records.
4)Suppose he selects 2 records and enters new values for them and hit the submit button then both the record should get processed sequentially, means, first Record A should get into processing and for this a message should get displayed as "Processing" and when it gets done then the message should appear as "Processed" and then starts the cycle for the next record in line. In between if any error occurs for any record the Erros message should get displayed but processing should carry on for subsequent records, if present.

As far as I've understood my requirement I know I have to use AJAX for this and below is my attempt to this but here also I'm confused about the strange behaviour. Whats happening here is, though the Ajax function is called once for each record but in the function the statements inside
"if(xmlObj.readyState == 4 && xmlObj.status == 200)" is executing twice for each record. Below is my code. Can anyone point me out what mistake am I doing.

Expand|Select|Wrap|Line Numbers
  1. /*edit_process.php starts here*/
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  3. <HTML>
  4. <script type="text/javascript">
  5. function ajaxFunction()
  6. {
  7.     var xml;
  8.     try
  9.     {  
  10.         xml=new XMLHttpRequest();  // Firefox, Opera 8.0+, Safari
  11.     }
  12.     catch (e)
  13.     {  
  14.         try
  15.         {
  16.             xml=new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 6.0+
  17.         }
  18.         catch (e)
  19.         {
  20.             try
  21.             {
  22.                 xml=new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 5.5+
  23.             }
  24.             catch (e)
  25.             {
  26.                 alert("Your browser does not support AJAX!");
  27.                 return false;
  28.             }
  29.         }
  30.     }
  31.      return xml;  // Mandatory Statement returning the ajax object created
  32. } //ajaxFunction closes
  33.  
  34. function postValues(params, div, xmlObj)
  35. {
  36.     var url="edit_trailer.php";
  37.     var flag;    
  38.     //alert(params);    
  39.  
  40.     xmlObj.open("POST", url, true);        
  41.     xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  42.     xmlObj.setRequestHeader("Content-length", params.length);
  43.     //xmlObj.setRequestHeader("Connection", "close");    
  44.  
  45.     xmlObj.onreadystatechange = function()
  46.     {    
  47.         alert("Ready state is: " + xmlObj.readyState);
  48.         if(xmlObj.readyState != 4)
  49.         {
  50.             alert('processing for ' + div);
  51.             document.getElementById(div).innerHTML = '<img src="http://bytes.com/images/loader.gif">';    
  52.         }
  53.  
  54.         if(xmlObj.readyState == 4 && xmlObj.status == 200)
  55.         { //All below statements are executing twice for each record
  56.             alert('processed for ' + div);
  57.             document.getElementById(div).innerHTML = '<img src="http://bytes.com/images/loader2.gif">';
  58.             alert(xmlObj.responseText);
  59.             flag=true;
  60.             alert('returning from callback ' + flag);
  61.             return;
  62.         }
  63.     }
  64.     xmlObj.send(params);
  65.     alert('returning after send ' + flag);
  66.     return flag;
  67. } //postValues closes
  68.  
  69. function submit_data()
  70. {    
  71.     document.getElementById('update').disabled = true;
  72.  
  73.     var xmlHttp;
  74.     xmlHttp=ajaxFunction();
  75.     if (xmlHttp==null)
  76.     {
  77.         alert ("Browser does not support HTTP Request");
  78.         return;
  79.     }
  80.     //alert("Ajax created");    
  81.  
  82.     var name, id, synop, thumb, url, recID;
  83.     name = document.edit.elements["name[]"];
  84.  
  85.     if (name.length==undefined) //this part is not working when there is one record
  86.     {
  87.         alert('only one element');
  88.         name = document.edit.name;
  89.         id = document.edit.id;
  90.         synop = document.edit.synop;
  91.         thumb = document.edit.thumb;
  92.         url = document.edit.url;
  93.         recID = document.edit.recID;
  94.  
  95.         alert('Name ' + name.value);
  96.         alert('Id ' + id.value);
  97.         alert('Syn ' + synop.value);
  98.         alert('thumb ' + thumb.value);
  99.         alert('url ' + url.value);        
  100.     }
  101.     else  //this part is working when there are more then one record
  102.     {
  103.         id = document.edit.elements["id[]"];
  104.         synop = document.edit.elements["synop[]"];
  105.         thumb = document.edit.elements["thumb[]"];
  106.         url = document.edit.elements["url[]"];
  107.         recID = document.edit.elements["recID[]"];        
  108.  
  109.         flag=true;//set flag to true to call function for the first record
  110.         for(i=0;i<name.length;i++)
  111.         {
  112.             var param="name="+name[i].value+"&id="+id[i].value+"&syn="+synop[i].value;
  113.             param+="&thumb="+thumb[i].value+"&url="+url[i].value+"&recId="+recID[i].value;    
  114.  
  115.             var div='div'+(i+1);            
  116.  
  117.             if(flag)
  118.             {
  119.                 alert(flag + ' bfore calling');
  120.                 flag=false;//set flag to false and get its value from the function
  121.                 flag=postValues(param, div, xmlHttp);//call function    
  122.                 alert(flag + ' after calling');
  123.             }
  124.  
  125.         } //for loop closes
  126.  
  127.     }// else closes
  128.  
  129.     return false;
  130. }
  131. </script>
  132. </HEAD>
  133.  
  134. <BODY>
  135. <?php    
  136. require("db_connect.php");
  137. require("db_open.php");
  138. if (!$conn)
  139. {
  140.     exit("Error connecting to the database: " . $conn);
  141. }
  142.  
  143. //This value is coming from FIRST page where admin select record for editing
  144. $record = $_POST['record']; 
  145. $query = "select mov_name, mov_id, mov_synop, mov_thumb, page_name ";
  146. $query.= "FROM bhod_test_trailers WHERE mov_nos=";
  147. ?>
  148. <FORM method="post" id="edit" onSubmit="return submit_data()" NAME="edit" enctype="multipart/form-data">
  149. <TABLE border="0" cellspacing="0" cellpadding="0" align="center" width="500">
  150. <?php
  151.     $divid=1;
  152.     foreach ($record as $rec)
  153.     {
  154.         $sql = $query . $rec;
  155.         $result = mysql_query($sql);
  156.         if (!$result)
  157.         {
  158.             echo 'Could not run query: ' . mysql_error();
  159.             exit;
  160.         }
  161.         $row = mysql_fetch_row($result);
  162. ?>
  163.     <TR>
  164.         <TD width="96" align="left" valign="top">
  165.             <img src="trailers/<?php echo $row[3]; ?>.jpg" border="0" width="92" height="85">
  166.         </TD>
  167.         <TD width="254" align="left">            
  168. <TABLE border="0" cellspacing="0" cellpadding="0" width="254">
  169. <input type="hidden" name="recID[]" value="<?php echo $row[1]; ?>" />
  170.     <TR>
  171.         <TD align="left" valign="top">
  172.     <label for="mov_name">Movie Name:</label>
  173.     <input type="text" name="name[]" value="<?php echo $row[0]; ?>" />
  174.         </TD>
  175.     </TR>
  176.  
  177.     <TR>
  178.         <TD align="left" valign="top">
  179.             <label for="mov_synop">Synopsis:</label>
  180.             <textarea name="synop[]" cols="20" rows="3"><?php echo $row[2]; ?></textarea>
  181.         </TD>
  182.     </TR>
  183.  
  184.     <TR>
  185.         <TD align="left" valign="top">
  186.             <label for="mov_id">Clip ID:</label>
  187.             <input type="text" name="id[]" value="<?php echo $row[1]; ?>" />
  188.         </TD>
  189.     </TR>
  190.  
  191.     <TR>
  192.         <TD align="left" valign="top">
  193.         <label for="page_name">URL:</label>
  194.         <input type="text" name="url[]" value="<?php echo $row[4]; ?>" />
  195.         </TD>
  196.     </TR>
  197.  
  198.     <TR>
  199.         <TD align="left" valign="top">
  200.             <label for="mov_thumb">Thumbnail:</label>
  201.             <input type="file" name="thumb[]" />
  202.         </TD>
  203.     </TR>
  204. </TABLE>
  205.         </TD>
  206.         <TD width="150" align="left" valign="top">
  207.             <div id="div<?php echo $divid; ?>"></div> //this div will hold our process status
  208.         </TD>
  209.     </TR>
  210.  
  211.     <TR>
  212.         <TD colspan="3"><BR></TD>
  213.     </TR>
  214.  
  215. <?php    
  216.         $sql = '';
  217.         $divid++;
  218.     }//for loop closes
  219. ?>
  220.     <TR>
  221.         <TD colspan="3">            
  222.             <input type="submit" id="update" value="Update" />
  223.         </TD>
  224.     </TR>
  225. </TABLE>
  226. </FORM>
  227. </BODY>
  228. </HTML>
  229. /*edit_process.php ends here*/
  230.  
Jan 3 '09 #1
1 4555
acoder
16,027 Expert Mod 8TB
It's executed twice because you're calling the Ajax function twice.
Jan 6 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ryan Hubbard | last post by:
I'm inserting a record into MySQL 4.0 using Visual Basic ADO. When using the AddNew and Update method I am unable to retrieve the value of a Auto incrment field (Yes I know I can MoveLast but this...
4
by: Greg Ofiesh | last post by:
Anyone who can help, I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign key relating to K2. My...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
10
by: Roger Withnell | last post by:
I'm using ASP, VBScript and SQL Server. I'm also using UTF-8 character set and so my codepage is 65001 and SQL Server datatype nvarchar. I can insert unicode characters correctly into the...
2
by: csgraham74 | last post by:
Hello, im using MS Access as a front end to link to MYSQL. although this is possibly an Access issue i thought someone would be able to help me. the error message "ODBC--insert on a linked table...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: ebindia0041 | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, Asp.net 1.1 with c# I'm inserting simple records into a table. But one...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
1
by: djmeltdown | last post by:
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky. I've tried the mysql_query statement without a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.