473,386 Members | 1,842 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,386 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 4565
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.