By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

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

P: 3
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
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
It's executed twice because you're calling the Ajax function twice.
Jan 6 '09 #2

Post your reply

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