need to insert values in database table

  •  03-19-2011, 12:53 AM

    need to insert values in database table

    Hi All
     
    I am using ajaxattachements in php file uploader. i have added file title(textbox),file description(textarea),keyword(textbox) and one checkbox for each file. i have added those all are using javascript. i need to insert those values for each file in database after successfully upload the file.
     
    i'm trying the get values in ajaxuploaderhandler.php. but i can't get the values after the object creation.
     
    $uploader=new PhpUploader();. after this line i can't able to get the values like title,description etc. i need to get the values and insert only if the file is successfully uploaded. i paste my code. and i
     
    ////////////////////////////********* this is for main file *****************************////////////////////////////////
    <?php session_start(); ?>
    <?php require_once "phpuploader/include_phpuploader.php"; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server"></script>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Multiple File Uploader</title>
    </head>
    <body>


        <form id="form1" name="form1" method="POST">
           <div>
               <button id='btnUpload' style="display: none;" onclick="Upload_Click();return false;">Start Upload</button>             
              
                  <?php         
                  
                             
                      $uploader=new PhpUploader();
                      $uploader->MaxSizeKB=10233340;
                      $uploader->Name="uploader";
                      $uploader->SaveDirectory="uploads";
                      //$uploader->UploadUrl="phpuploader/ajaxuploaderhandler.php";   
                      $uploader->InsertText="Add files";
                      $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.txt,*.zip,*.rar";  
                      $uploader->MultipleFilesUpload=true;
                      $uploader->ManualStartUpload=true;
                      $uploader->Render();
                  ?>
                  <input type="hidden" name="res" id="res" />   
               <table id='clientTable' style="display: none; font-size: 9pt; border-collapse: collapse"
                  border="1" cellspacing="0" cellpadding="5">
                  <tr>
                      <td>
                         FileName
                      </td>
                      <td>
                         Title
                      </td>
                      <td>
                         Description
                      </td>                   
                      <td>
                         Keywords
                      </td>
                      <td>
                         18+
                      </td>
                      
                     
                  </tr>
               </table>
               <br />       
           </div>
        </form>


    </body>
     
    <script>
        var btnUpload = document.getElementById("btnUpload");
        var clientTable = document.getElementById("clientTable");
        var uploader = document.getElementById('uploader');
        var buttonTellClicked = false;
     
        function Upload_Click() {
            var title=new Array();
            var title1;
            var desc=new Array();
            var desc1;
            var keyword=new Array();
            var keyword1;
            var filename=new Array();
            var filename1;
            var chk=new Array();
            var chk1;
            var i;
            var flag=true;    
            for (i=1; i<document.getElementById('clientTable').rows.length; i++)
            {
            filename[i] = document.getElementById('clientTable').rows[i].cells[0].innerHTML;
            title[i] = document.getElementById('clientTable').rows[i].cells[1].childNodes[0].value;
            desc[i] = document.getElementById('clientTable').rows[i].cells[2].childNodes[0].value;
            keyword[i] = document.getElementById('clientTable').rows[i].cells[3].childNodes[0].value;
            if (document.getElementById('clientTable').rows[i].cells[4].childNodes[0].checked==true)
            {
                chk[i]=1;
            }
            else
            {
                chk[i]=0;
            }
            
            
            }
            for (j=0;j<title.length;j++)
            {
                if (title[j]=='' || desc[j]=='' || keyword[j]=='')
                {
                    alert('Please Fill all values');
                    flag=false;
                    
                }
            }
            if (flag)
            {
            uploader.startupload();
            
            //alert(title);
        
        var ajaxRequest; // The variable that makes Ajax possible!
        
        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('res').value = ajaxRequest.responseText;
            }
        }
        
        filename.shift();
        filename1=filename.join("~");    
        title.shift();
        title1=title.join("~");
        desc.shift();
        desc1=desc.join("~");
        keyword.shift();
        keyword1=keyword.join("~");
        chk.shift();
        chk1=chk.join("~");
        var queryString = "?filename="+ filename1 +"&title=" + title1 + "&desc=" + desc1 + "&keyword=" + keyword1 + "&chk=" + chk1;
        ajaxRequest.open("GET", "phpuploader/ajaxuploaderhandler.php" + queryString, true);
        ajaxRequest.send(null);
    }
            }        
        
        
        function CuteWebUI_AjaxUploader_OnPostback() {       
        }
        function CuteWebUI_AjaxUploader_OnQueueUI(files) {
            btnUpload.style.display = files.length > 0 ? "" : "none";
            clientTable.style.display = files.length > 0 ? "" : "none";
            ShowMyClientTable(files);
            return false;
        }
        function ShowMyClientTable(files) {
            var map = {}
            var newlist = [];
            for (var i = 1; i < clientTable.rows.length; i++) {
                var row = clientTable.rows.item(i);
                row._scan = false;
                map[row._filekey] = row;
            }
            //update existing row
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                var row = map[file.InitGuid || file.FileName];
                if (row == null) {
                    newlist.push(file);
                    continue;
                }
                row._scan = true;
                UpdateToRow(row, file);
            }
            //delete removed row
            for (var i = 1; i < clientTable.rows.length; i++) {
                var row = clientTable.rows.item(i);
                if (!row._scan) {
                    clientTable.deleteRow(i);
                    i--;
                }
            }
            //add new row:
            for (var i = 0; i < newlist.length; i++) {
                var file = newlist[i];
                var row = clientTable.insertRow(-1);
                row.insertCell(-1);
                row.insertCell(-1);
                row.insertCell(-1);
                row.insertCell(-1);
                row.insertCell(-1);
                row.insertCell(-1);
                a=i+1;                    
                UpdateToRow(row, file,a);
                
            }
        }
        function UpdateToRow(row, file,a) {
            row._file = file;
     
            row._filekey = file.InitGuid || file.FileName;
             
            if (!row._textbox) {
                row._textbox = document.createElement("INPUT");
                row._textbox.type = "text";            
                row._textbox.name  ="title[]";            
                row.cells.item(1).appendChild(row._textbox);
                
                var y = document.createElement('textarea');
                y.setAttribute("name","desc[]");
                y.setAttribute("cols","30");
                y.setAttribute("rows","2");
                row.cells.item(2).appendChild(y);            

                row._textbox = document.createElement("INPUT");
                row._textbox.type = "text";            
                row._textbox.name  ="keyword[]";            
                row.cells.item(3).appendChild(row._textbox);
                
                var cb = document.createElement( "input" );
                 cb.type = "checkbox";
                cb.name = "chk[]";           
                row.cells.item(4).appendChild(cb);
                           
                
            }

     
            row._textbox.onchange = function() {
                file.SetClientData(row._textbox.value);
            }
     
            row.cells.item(0).innerHTML = file.FileName;
     
            switch (file.Status) {
                case "Queue":
                    row.cells.item(5).innerHTML = "<a href='#' onclick='CancelQueueItem(this);return false'>remove</a>";
                    break;
                case "Finish": //uploaded
                case "Upload": //uploading
                case "Error": //cancelled
                default:
                    row.cells.item(5).innerHTML = file.Status;
                    break;
            }
        }
        function CancelQueueItem(link) {
            var td = link.parentNode;
            var row = td.parentNode;
            var file = row._file;
            var a =confirm('Are you sure want to remove');
            if (a)
            {
            file.Cancel();
            }
        }
       
    </script>
    <script type="text/javascript">
        //prevent duplicated items:
        function CuteWebUI_AjaxUploader_OnSelect(files) {
            var sames = [];
            var items = uploader.getitems();
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                var exists = false;
                for (var j = 0; j < items.length; j++) {
                    var item = items[j];
                    if (item.FileName == file.FileName) {
                        exists = true;
                    }
                }
                if (exists) {
                    sames.push(file.FileName);
                    file.Cancel();
                }
            }
            if (sames.length > 0) {
                alert("These file(s) are already in the queue : \r\n\t" + sames.join('\r\n\t'));
            }
        }
        function Attachment_Remove(link)
        {
            var row=Attachment_FindRow(link);
            if(!confirm("Are you sure you want to delete '"+row.getAttribute("filename")+"'?"))
                return;
            
            var guid=row.getAttribute("fileguid");
            
            var xh=CreateAjaxRequest();
            xh.send("delete=" + guid);

            var table = document.getElementById("filelist");
            table.deleteRow(row.rowIndex);
            
            for(var i=0;i<fileArray.length;i++)
            {
                if(fileArray[i].FileGuid==guid)
                {
                    fileArray.splice(i,1);
                    break;
                }
            }
        }
        
    </script>

     
    </html>
    /*************************************************************************************************************************************************/
     
    ///////////////////*ajaxuploaderhandler.php*****************************////////////////////////////////////////////////////////////
    <?php
    require_once "include_phpuploader.php" ?>
    <?php require_once "config.php" ?>
    <?php
    if(isset($_REQUEST['title']) && isset($_REQUEST['desc']) && isset($_REQUEST['keyword']))
    {
        global $title;
        $title=$_REQUEST['title'];    
        $desc=$_REQUEST['desc'];    
        $keyword=$_REQUEST['keyword'];    
        $filename=$_REQUEST['filename'];    
        $chk=$_REQUEST['chk'];
        
        /*$title=explode('~',$title);    
        $desc=explode('~',$desc);    
        $keyword=explode('~',$keyword);    
        $filename=explode('~',$filename);    
        $chk=explode('~',$chk);
        $count=count($title);
        
        for($a=0;$a<$count;$a++)
        {
            $query="INSERT INTO file_details(file_name,title,description,keyword,chk_option)VALUES ('".$filename[$a]."','".$title[$a]."','".$desc[$a]."','".$keyword[$a]."','".$chk[$a]."')";
            mysql_query($query);
        }    */
            
    }
    set_time_limit(3600);
    $uploader=new PhpUploader();
    $uploader->PreProcessRequest();
    $mvcfile=$uploader->GetValidatingFile();

    if($mvcfile->FileName=="thisisanotvalidfile")
    {
        $uploader->WriteValidationError("My custom error : Invalid file name. ");
        exit(200);
    }


    if( $uploader->SaveDirectory )
    {

        
        if(!$uploader->AllowedFileExtensions)
        {
            $uploader->WriteValidationError("When using SaveDirectory property, you must specify AllowedFileExtensions for security purpose.");
            exit(200);
        }

        $cwd=getcwd();
        chdir( dirname($uploader->_SourceFileName) );
        if( ! is_dir($uploader->SaveDirectory) )
        {
            $uploader->WriteValidationError("Invalid SaveDirectory ! not exists.");
            exit(200);
        }
        chdir( $uploader->SaveDirectory );
        $wd=getcwd();
        chdir($cwd);    
        $targetfilepath=  "$wd/" .$mvcfile->FileName;
        if( file_exists ($targetfilepath) )
            unlink($targetfilepath);
            
            $mvcfile->CopyTo($targetfilepath);      /// i need to insert the values after this line. please help me.
                
    }

    $uploader->WriteValidationOK();

    ?>
     
     
View Complete Thread