Hướng dẫn js post array to php - js đăng mảng lên php

7

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang cố gắng gửi một mảng JavaScript đến một tệp PHP qua bài đăng.

JS:

var songlist = ['song1', 'song2', 'song3'];

var sendData = function[]{
    var data = songList.join[',']
    $.post['test.php', {data: data}].always[function[] {
        window.location = 'test.php';
    }];
}
sendData[];

test.php:


Khi sendData []; Chỉ đạo tôi đến Test.php Tôi nhận được:

Thông báo: Chỉ mục không xác định: Dữ liệu

Tại sao biến dữ liệu không có bất kỳ giá trị nào khi tôi cố gắng in hoặc sử dụng nó?

B. Desai

16.3k5 Huy hiệu vàng25 Huy hiệu bạc47 Huy hiệu đồng5 gold badges25 silver badges47 bronze badges

Hỏi ngày 23 tháng 4 năm 2017 lúc 10:43Apr 23, 2017 at 10:43

1

Đó không phải là cách yêu cầu POST hoạt động. Đọc thêm về Ajax, nhưng bây giờ, đó là cách bạn nên làm điều đó.

var songlist = ['song1', 'song2', 'song3'];

var sendData = function[] {
  $.post['test.php', {
    data: songlist
  }, function[response] {
    console.log[response];
  }];
}
sendData[];
// test.php

Đã trả lời ngày 23 tháng 4 năm 2017 lúc 10:48Apr 23, 2017 at 10:48

Oen44Oen44Oen44

2.9981 Huy hiệu vàng21 Huy hiệu bạc27 Huy hiệu đồng1 gold badge21 silver badges27 bronze badges

1

1] $ .POST ['url'] - Yêu cầu AJAX được thực hiện bằng phương thức


0 và bạn đã cho

1 dưới dạng URL không hợp lệ.
- Ajax request is done by

0 method and you have given

1 as url which is invalid.

2] Window.location = 'test.php' - Điều này được sử dụng để chuyển hướng đến trang cụ thể và bạn đã chuyển hướng đến


2 mà không có bất kỳ tham số/dữ liệu nào. Đó là lý do tại sao nó hiển thị "Thông báo: Chỉ số không xác định: Dữ liệu" - This is used for redirecting to specific page and you have redirected to

2 without any parameter/data. Thats why its showing "Notice: Undefined index: data"

3] Cố gắng hiểu cách hoạt động của Ajax. Theo dõi nó -

var songlist = ['song1', 'song2', 'song3'];

var sendData = function[] {
  $.post['test.php', {
    data: songlist
  }, function[response] {
    console.log[response];
  }];
}
sendData[];
// test.php

Đã trả lời ngày 23 tháng 4 năm 2017 lúc 10:59Apr 23, 2017 at 10:59

AGM Tazimagm TazimAGM Tazim

2.1743 huy hiệu vàng15 Huy hiệu bạc25 Huy hiệu đồng3 gold badges15 silver badges25 bronze badges

2

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Chuyển các mảng PHP cho JavaScript rất dễ dàng bằng cách sử dụng ký hiệu đối tượng JavaScript [JSON].
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Chuyển các mảng PHP cho JavaScript rất dễ dàng bằng cách sử dụng ký hiệu đối tượng JavaScript [JSON].

    Phương pháp 1: Sử dụng hàm json_encode []: hàm json_encode [] được sử dụng để trả về biểu diễn JSON của một giá trị hoặc mảng. Hàm có thể lấy cả mảng đơn chiều và đa chiều. The json_encode[] function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.

    Steps:

    • Tạo một mảng trong PHP: ________ 5
    • Sử dụng hàm json_encode [] để truy xuất các phần tử mảng
      var passedArray = ;

    Example:

    
    
    3
    
    
    4

    
    
    5

    
    
    6
    
    
    7
    
    
    8
    
    
    9
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    0

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    1
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    2

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    1
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    4

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    1
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    6

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    7

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    8

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    9
    // test.php
    
    0
    // test.php
    
    1

    // test.php
    
    2

    // test.php
    
    3

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    1
    
    
    3
    
    
    4
    // test.php
    
    7

    // test.php
    
    8

    // test.php
    
    9
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    // test.php
    
    
    0
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    // test.php
    
    
    1

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    1
    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    // test.php
    
    
    3

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    // test.php
    
    
    4

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    // test.php
    
    
    5

    Output:

    GeeksforGeeks

    Phương pháp 2: Sử dụng hàm Implode [] của Php Implode []: Cụ nổ [] được sử dụng để tham gia các phần tử của một mảng. Hàm Ilifrode [] là hàm bí danh của hàm nối [] và hoạt động giống hệt như hàm của hàm nối []. Hàm Implode [] được sử dụng để xây dựng một chuỗi trở thành một mảng theo nghĩa đen trong javascript. Vì vậy, nếu chúng ta có một mảng trong PHP, chúng ta có thể chuyển nó cho JavaScript như sau: The implode[] is used to join the elements of an array. The implode[] function is the alias of join[] function and works exactly same as that of join[] function.
    The implode[] function is used to build a string that becomes an array literal in JavaScript. So, if we have an array in PHP, we can pass it to JavaScript as follows:

    var passedArray = ;
    

    Example:

    
    
    3
    
    
    4

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    // test.php
    
    
    8

    
    
    6
    
    
    7
    
    
    8
    
    
    9
    3

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    8

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    9
    // test.php
    
    0
    7
    8
    9
    // test.php
    
    1

    var passedArray = ;
    1

    // test.php
    
    3

    var songlist = ['song1', 'song2', 'song3'];
    
    var sendData = function[] {
      $.post['test.php', {
        data: songlist
      }, function[response] {
        console.log[response];
      }];
    }
    sendData[];
    1
    
    
    3
    
    
    4
    var passedArray = ;
    6

    var passedArray = ;
    7

    var passedArray = ;
    8

    var passedArray = ;
    9
    // test.php
    
    0
    // test.php
    
    1

    Output:

    Car, Bike, Boat

    PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo hướng dẫn PHP và các ví dụ PHP này.


    Chủ Đề