Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    Problem iterating Array Properly.

    Hi guys,

    I'm having a problem with an array. I am trying to implement the jquery sortable plugin. I downloaded the source code from here.

    http://www.wil-linssen.com/musings/e...th-ajax-mysql/

    The problem i am having is that when the array is in javascript it shows all rows in the array. For example if we have 4 total rows. On the javascript side it will show 4 rows and then when i use the ajax code shown below it only shows 3 rows. I am thinking it's a problem with the php, but i could be wrong. Any opinions.

    Here's the php code.
    PHP Code:
    foreach ($_GET['listItem'] as $position => $item){
        
    $sql[] = "UPDATE `table` SET `position` = $position WHERE `id` = $item";
    }

    print_r ($sql); 
    Thanks in advance

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    Without information on how the Ajax is formulated in Javascript, it's hard to tell what's up:

    Try print_r($_GET['listItem']) and print_r($_GET) to see what is inside those. If it only shows 3 options, then it's an issue with the Ajax/Javascript side of things... otherwise, post the results of the print_r's above, and see if that helps us to see what's going on!

    ~Jordan
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    how do you pass those 4 values to the URL?

    and your code is open to SQL Injection.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    This is the javascript that comes right out of the example.

    Code:
    <script type="text/javascript">
      // When the document is ready set up our sortable with it's inherant function(s)
      $(document).ready(function() {
        $("#test-list").sortable({
          handle : '.handle',
          update : function () {
    		  var order = $('#test-list').sortable('serialize');
    		  alert(order);
    		  var querystring = "?listItem=" + order;
      		$("#info").load("process-sortable.php"+querystring);
          }
        });
    });
    </script>
    process-sortable.php only contains the code above in my previous post.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    what is the value of querystring?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    First of all, I'm pretty sure Jquerys serialize function formulates the query string hence:

    Code:
    $("#info").load("process-sortable.php?"+order);
    should be sufficient therefore your querystring would be formulated as:

    Code:
    ?nameOfElement=valueOfElement&nameOfElement2=valueOfElement2 etc etc
    so atm your query string is:

    Code:
    listItem=nameofElement=valueofElement&nameOfElement2=valueOfElement2
    then try

    Code:
    $("#info").load("process-sortable.php"+querystring, function(response, status, xhr) {
         alert(response);
      }
    });
    and then change your PHP ajax code to:

    PHP Code:

    print_r
    ($_POST); 

    The ajax should then execute and on completion alert the contents of your post variable!

    Might help you debug!
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]


  •  

    Posting Permissions

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