www.webdeveloper.com
Results 1 to 3 of 3

Thread: Updating database with php/ajax/jquery

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    1

    Updating database with php/ajax/jquery

    I am using Jquery sortable, but I'm not able to update the database with the position user has chosen, maybe somebody can help me

    Here is jquery and ajax :

    Code:
    $(function () {
        $('.drag').sortable({
        connectWith: '.drag',
        update : function(e, ui) {
            var id=$(this).attr('id');
            var ul = $(ui.item).closest('.drag');
            var index = 0;
            var toPost = {};
            ul.find('> li').each(function() {
            index++;
            $(this).find('input').val(index);               
            toPost[$(this).find('input').attr('name')] = index;
            });                  
            $.ajax({
            url : 'UpdateLink.php',
            data : id,
            type : 'POST',
            dataType : 'json',
            success : function(resp) {
                alert(resp);
            },
            error : function() {
                alert('There was a problem');
            }
            });
        }
        });
    });
    HTML and some PHP :
    HTML Code:
    <ul style="list-style-type: none;" class="drag"><li>
       <input type="hidden" value="sortNO_<?php echo $lingi_sort_no; ?>" />
        <input type="hidden" name="<?php echo $title_id; ?>"><a href="<?php echo $lingi_aadress; ?>"><?php echo $lingi_nimi;?></a>
              </li></ul>
    PHP that I want to get updating the database
    PHP Code:
    foreach($_POST['id'] as $title_id => $order) { 
        
    $sql "UPDATE db_links SET title_id = '$title_id' WHERE id = '$order'";
        
    mysql_query($sql) or die (mysql_error());

    I need to update two values in database: $lingi_sort_no and $title_id. I would appreciate some help

  2. #2
    Join Date
    Aug 2012
    Posts
    15

    You can try this code

    HTML Code:
    //The UL List
    /*
    	<ul>
    		<li>
    			<input type="hidden" name="" value="<?php echo $value1;?>" class="first"/>
    			<input type="hidden" name="" value="<?php echo $value2;?>" class="second"/>
    			<a></a>
    		</li>
    	</ul>
    	
    */
    
    
    $(function () {
        $('.drag').sortable({
        connectWith: '.drag',
        update : function(e, ui) {
            		
    		var index = 0;
    		var _data = []; // array of values
    		ul.find('> li').each(function() {
    			
    			index++;			
    			var _val1 = $(this).find('input.first').val();
    			var _val2 = $(this).find('input.second').val();
    			
    			var _str = index+','+val1+','+val2; // save data as comma separated string
    			
    			_data.push(_str);
    			
    		});
    
    		//Conver array of data to string to send it to php
    		var data = _data.join(';'); //this will create a string like this  data='index1,data1,data2;...;indexn,datan1,datan2'
    		
    		//You dont need dataType json if you only spect a confirmation from php
    			$.ajax({
    				url : 'UpdateLink.php',
    				data : 'data='+data,
    				type : 'POST',
    				success : function(data,status) {
    					
    					if(status == 'success'){
    						alert(data);
    					}
    					
    				},
    				error : function() {
    					alert('There was a problem');
    				}
    			});
        }
        
    	});
    });
    
    //The PHP file
    
    <?php 
    	
    	$data = $_POST['data'];
    	
    	//Create first array
    	$items = explode(';',$data); // This create the first array with avery item from the UL
    	
    	foreach($items as $item){
    		
    		$values = explode(',',$item); // This create the second array to separate the values
    		
    		$id = $values[0]; // The order of the li item from the ul list
    		$title = $values[1]; //The title of the item, Here is better to use the unique id of your table
    		
    		
    		//I changed the MySQL sentence, but I think you need a unique id to refer to, instead the title
    		
    		 $sql = "UPDATE db_links SET order ='$id' WHERE title_id= '$title'"; 
    		mysql_query($sql) or die (mysql_error());
    	}
    	
    ?>	
    

  3. #3
    Join Date
    Aug 2012
    Posts
    15

    Correction

    hi again and sorry is all messed up.

    Cleaning the post you can try this

    This is the HTML code for your UL list:
    HTML Code:
    <ul>
    		<li>
    			<input type="hidden" name="" value="<?php echo $value1;?>" class="first"/>
    			<input type="hidden" name="" value="<?php echo $value2;?>" class="second"/>
    			<a></a>
    		</li>
    	</ul>
    This is the javascript code:

    Code:
    $(function () {
        $('.drag').sortable({
        connectWith: '.drag',
        update : function(e, ui) {
            		
    		var index = 0;
    		var _data = []; // array of values
    		ul.find('> li').each(function() {
    			
    			index++;			
    			var _val1 = $(this).find('input.first').val();
    			var _val2 = $(this).find('input.second').val();
    			
    			var _str = index+','+val1+','+val2; // save data as comma separated string
    			
    			_data.push(_str);
    			
    		});
    
    		//Conver array of data to string to send it to php
    		var data = _data.join(';'); //this will create a string like this  data='index1,data1,data2;...;indexn,datan1,datan2'
    		
    		//You dont need dataType json if you only spect a confirmation from php
    			$.ajax({
    				url : 'UpdateLink.php',
    				data : 'data='+data,
    				type : 'POST',
    				success : function(data,status) {
    					
    					if(status == 'success'){
    						alert(data);
    					}
    					
    				},
    				error : function() {
    					alert('There was a problem');
    				}
    			});
        }
        
    	});
    });
    And finally the PHP code:

    PHP Code:
    <?php 
        
        $data 
    $_POST['data'];
        
        
    //Create first array
        
    $items explode(';',$data); // This create the first array with avery item from the UL
        
        
    foreach($items as $item){
            
            
    $values explode(',',$item); // This create the second array to separate the values
            
            
    $id $values[0]; // The order of the li item from the ul list
            
    $title $values[1]; //The title of the item, Here is better to use the unique id of your table
            
            
            //I changed the MySQL sentence, but I think you need a unique id to refer to, instead the title
            
             
    $sql "UPDATE db_links SET order ='$id' WHERE title_id= '$title'"
            
    mysql_query($sql) or die (mysql_error());
        }
        
    ?>
    I hope this will help you out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles