See comments in quoted code below.
> Inserting multiple record in MYSQL one at a time sequentially using
> Posted by: "javediq_143"
javediq_143@... javediq_143
> Date: Fri Jan 2, 2009 10:15 am ((PST))
>
> Hi All,
> This post is related to my earlier one where I have discussed my
> problem relating to uploading the THUMBNAIL image. Since I cant waste
> my time, so I've modified the requirement and carried on my work on
> the EDIT part of the CMS. But to my worse, again I'm stuck. Below is
> the details:
>
> 1)First page will list all the records in the table with Checkbox for
> each record to select for Editing. The Admin can select any number of
> records which he wants to change. This page is working fine.
> Lets move to second page.
> 2)Second page will list those record details in a FORM checked by the
> Admin for editing.
> 3)The first difficulty over here is, suppose there is only one record
> to get changed then the Javascript which I've written is not working
> but when there are more then one the script is working!!
> 4)Lets assume there are 2 records for editing and the Admin enters the
> new detail and hit the submit button. This will invoke a Javascript
> function which will FIRST CREATE AN AJAX OBJECT and then checks HOW
> MANY RECORDS ARE FOR EDITING and for each record it will call another
> function which will get 3 params as FORM VALUE, CURRENT DIV ID(to show
> the status of process for each record) and AJAX OBJECT.
> 5)To show the status of each record I'm using 2 images as PROCESSED
> and PROCESSING.
> 6)When I hit the submit button, the DIV of the first record continues
> show me the PROCESSING image whereas the DIV of the second record
> first shows me PROCESSING and then PROCESSED.
> 7)But the thing should be like this, when first record details are
> sent for processing then PROCESSING image should be shown in its DIV.
> After it gets completed it should show as PROCESSED and then second
> record should get into processing.
>
> I dont know whether I'm able to specify my requirement clearly or not.
> Below is the complete code of second and the page which will be called
> with AJAX. Please try to help me coz I'm getting short of dates.
>
> Thanks,
> Javed
>
> /*Second page starts here*/
> <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"); // IE 6.0+
> }
> catch (e)
> {
> try
> {
> xml=new ActiveXObject("Microsoft.XMLHTTP"); // IE 5.5+
> }
> catch (e)
> {
> alert("Your browser does not support AJAX!");
> return false;
> }
> }
> }
> return xml;
> } //ajaxFunction closes
>
> function postValues(params, div, xmlObj)
> {
> var url="edit_trailer.php";
>
> 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()
> {
> //if the request not completed show PROCESSING
> if(xmlObj.readyState != 4)
> {
> document.getElementById(div).innerHTML = '<img
> src="images/loader.gif">';
> return false;
> }
>
> if(xmlObj.readyState == 4 && xmlObj.status == 200)
> {
> document.getElementById(div).innerHTML = '<img
> src="images/loader2.gif">';
> alert(xmlObj.responseText);
> return true;
> }
> }
> xmlObj.send(params);
> } //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;
> }
>
> var name, id, synop, thumb, url, recID;
> name = document.edit.elements["name[]"];
>
> //this is not printing form values when there is one record
> if (name.length==undefined)
If there's only one record, then name is an array with a single element,
and name.length is equal to 1. IIRC, name.length would be undefined only
if there are *no* elements.
> {
> 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 is working when there are more then one record
If you get rid of the if-else, this should work for any number of
records >= 1.
> {
> 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[]"];
>
> 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);
>
> //call function with details of 1st records and its corresponding div
> var flag=postValues(param, div, xmlHttp);
>
> /*Here i want to halt for the next record until the previous been
> processed.I know I'm completely wrong over here but just to explain my
> needs here*/
> if(flag)
> {
> continue;
> }
> } //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);
> }
>
> $record = $_POST['record'];
> $query = "select mov_name, mov_id, mov_synop, mov_thumb, page_name ";
> $query.= "FROM 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 will hold the
> status info image
> </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>
> /*Second page ends here*/
>
> /*edit_trailer.php starts here*/
> //this page will insert the form values in the database and upload the
> thumbnail to the server
> //for time being I'm using some code to halt the server response
> <?php
> sleep(15);
> echo "Hi there";
> ?>
> /*edit_trailer.php ends here*/
--
This message has not been scanned for viruses.
Since I do not use a Microsoft operating
system or software, and use only plaintext
for email, there is little need for me to do so.