Ajax Post Not Sending Array
Hello,
Back again with another question lol
So I have been trying to send an array of a UL:LI to a PHP page. The array is made and through jQuery can be read just fine. The problem is when I use the $.ajax() function the array is not sent.
jQuery.html
- <html>
- <head>
- <title>jQuery Testing</title>
- <script type="text/javascript" src="templates/js/jquery.js"></script>
-
- <script>
- $(document).ready(function() {
- $('.list1 li').dblclick(function() {
- $(this).clone().appendTo('.list2');
- });
-
- $('#test').click(function() {
- var test_list = [];
-
- $("ul.list2 li").each(function() { test_list.push($(this).attr('id')) });
-
- $.each(test_list, function(index, value) {
- alert(index + ': ' + value);
- });
-
- $.ajax({
- url: "post.php",
- type: "POST",
- dataType: "html",
- data: test_list,
- //data: { 0: 'test' },
- success: function(data) {
- alert(data);
- }
- });
-
- });
- });
- </script>
- </head>
- <body>
- <ul class="list1">
- <li id="1">List 1</li>
- <li id="2">List 2</li>
- <li id="3">List 3</li>
- <li id="4">List 4</li>
-
- </ul>
- <br />
- <br />
- <br />
- <ul class="list2">
- <li id="1">List 1</li>
- <li id="2">List 2</li>
- <li id="3">List 3</li>
- <li id="4">List 4</li>
-
- </ul>
- <br />
- <br />
- <input type="button" id="test" value="Test" />
Everything about cloning and appending works (10-12).
Lines (14-34) Is what is giving me the problem.
As I said, the array is made from the list. list2 to be exact. That works fine, and using the .each() function does what I want it to do (this will be deleted after it works).
Line 27 seems to be the problem. When I comment it out and uncomment Line 28 everything works fine and the php file returns the right value.
Just for you to see here is the post.php file.
- <?php
- if($_POST[0]) {
- echo 'yes<br />'.$_POST[0];
- } else {
- echo 'no';
- }
- ?>
Any help as to why?