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
- /*edit_process.php starts here*/
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <script type="text/javascript">
- function ajaxFunction()
- {
- var xml;
- try
- {
- xml=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
- }
- catch (e)
- {
- try
- {
- xml=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer 6.0+
- }
- catch (e)
- {
- try
- {
- xml=new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 5.5+
- }
- catch (e)
- {
- alert("Your browser does not support AJAX!");
- return false;
- }
- }
- }
- return xml; // Mandatory Statement returning the ajax object created
- } //ajaxFunction closes
- function postValues(params, div, xmlObj)
- {
- var url="edit_trailer.php";
- var flag;
- //alert(params);
- xmlObj.open("POST", url, true);
- xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xmlObj.setRequestHeader("Content-length", params.length);
- //xmlObj.setRequestHeader("Connection", "close");
- xmlObj.onreadystatechange = function()
- {
- alert("Ready state is: " + xmlObj.readyState);
- if(xmlObj.readyState != 4)
- {
- alert('processing for ' + div);
- document.getElementById(div).innerHTML = '<img src="http://bytes.com/images/loader.gif">';
- }
- if(xmlObj.readyState == 4 && xmlObj.status == 200)
- { //All below statements are executing twice for each record
- alert('processed for ' + div);
- document.getElementById(div).innerHTML = '<img src="http://bytes.com/images/loader2.gif">';
- alert(xmlObj.responseText);
- flag=true;
- alert('returning from callback ' + flag);
- return;
- }
- }
- xmlObj.send(params);
- alert('returning after send ' + flag);
- return flag;
- } //postValues closes
- function submit_data()
- {
- document.getElementById('update').disabled = true;
- var xmlHttp;
- xmlHttp=ajaxFunction();
- if (xmlHttp==null)
- {
- alert ("Browser does not support HTTP Request");
- return;
- }
- //alert("Ajax created");
- var name, id, synop, thumb, url, recID;
- name = document.edit.elements["name[]"];
- if (name.length==undefined) //this part is not working when there is one record
- {
- alert('only one element');
- name = document.edit.name;
- id = document.edit.id;
- synop = document.edit.synop;
- thumb = document.edit.thumb;
- url = document.edit.url;
- recID = document.edit.recID;
- alert('Name ' + name.value);
- alert('Id ' + id.value);
- alert('Syn ' + synop.value);
- alert('thumb ' + thumb.value);
- alert('url ' + url.value);
- }
- else //this part is working when there are more then one record
- {
- id = document.edit.elements["id[]"];
- synop = document.edit.elements["synop[]"];
- thumb = document.edit.elements["thumb[]"];
- url = document.edit.elements["url[]"];
- recID = document.edit.elements["recID[]"];
- flag=true;//set flag to true to call function for the first record
- for(i=0;i<name.length;i++)
- {
- var param="name="+name[i].value+"&id="+id[i].value+"&syn="+synop[i].value;
- param+="&thumb="+thumb[i].value+"&url="+url[i].value+"&recId="+recID[i].value;
- var div='div'+(i+1);
- if(flag)
- {
- alert(flag + ' bfore calling');
- flag=false;//set flag to false and get its value from the function
- flag=postValues(param, div, xmlHttp);//call function
- alert(flag + ' after calling');
- }
- } //for loop closes
- }// else closes
- return false;
- }
- </script>
- </HEAD>
- <BODY>
- <?php
- require("db_connect.php");
- require("db_open.php");
- if (!$conn)
- {
- exit("Error connecting to the database: " . $conn);
- }
- //This value is coming from FIRST page where admin select record for editing
- $record = $_POST['record'];
- $query = "select mov_name, mov_id, mov_synop, mov_thumb, page_name ";
- $query.= "FROM bhod_test_trailers WHERE mov_nos=";
- ?>
- <FORM method="post" id="edit" onSubmit="return submit_data()" NAME="edit" enctype="multipart/form-data">
- <TABLE border="0" cellspacing="0" cellpadding="0" align="center" width="500">
- <?php
- $divid=1;
- foreach ($record as $rec)
- {
- $sql = $query . $rec;
- $result = mysql_query($sql);
- if (!$result)
- {
- echo 'Could not run query: ' . mysql_error();
- exit;
- }
- $row = mysql_fetch_row($result);
- ?>
- <TR>
- <TD width="96" align="left" valign="top">
- <img src="trailers/<?php echo $row[3]; ?>.jpg" border="0" width="92" height="85">
- </TD>
- <TD width="254" align="left">
- <TABLE border="0" cellspacing="0" cellpadding="0" width="254">
- <input type="hidden" name="recID[]" value="<?php echo $row[1]; ?>" />
- <TR>
- <TD align="left" valign="top">
- <label for="mov_name">Movie Name:</label>
- <input type="text" name="name[]" value="<?php echo $row[0]; ?>" />
- </TD>
- </TR>
- <TR>
- <TD align="left" valign="top">
- <label for="mov_synop">Synopsis:</label>
- <textarea name="synop[]" cols="20" rows="3"><?php echo $row[2]; ?></textarea>
- </TD>
- </TR>
- <TR>
- <TD align="left" valign="top">
- <label for="mov_id">Clip ID:</label>
- <input type="text" name="id[]" value="<?php echo $row[1]; ?>" />
- </TD>
- </TR>
- <TR>
- <TD align="left" valign="top">
- <label for="page_name">URL:</label>
- <input type="text" name="url[]" value="<?php echo $row[4]; ?>" />
- </TD>
- </TR>
- <TR>
- <TD align="left" valign="top">
- <label for="mov_thumb">Thumbnail:</label>
- <input type="file" name="thumb[]" />
- </TD>
- </TR>
- </TABLE>
- </TD>
- <TD width="150" align="left" valign="top">
- <div id="div<?php echo $divid; ?>"></div> //this div will hold our process status
- </TD>
- </TR>
- <TR>
- <TD colspan="3"><BR></TD>
- </TR>
- <?php
- $sql = '';
- $divid++;
- }//for loop closes
- ?>
- <TR>
- <TD colspan="3">
- <input type="submit" id="update" value="Update" />
- </TD>
- </TR>
- </TABLE>
- </FORM>
- </BODY>
- </HTML>
- /*edit_process.php ends here*/