Hướng dẫn how to call ajax function on page load in php - cách gọi hàm ajax khi tải trang trong php

First,

$("document")

nên là

$(document)

Bởi vì không có yếu tố và nếu có, bạn sẽ không gọi .ready() trên chúng.

Tiếp theo, lý do bạn đang thấy vấn đề này là vì $(document).ready() chỉ bắn một lần cho mỗi lần tải trang đầy đủ. Thay đổi thành một trang mới trong JQuery Mobile không kích hoạt tải trang đầy đủ, thay vào đó, nó tải trong trang mới với AJAX. Để giải quyết vấn đề này, JQuery Mobile có một sự kiện có tên "PageInit" được kích hoạt trên trang được tải vào. Đây là cách bạn sẽ liên kết với nó trong trường hợp của bạn:

Pre-JQuery 1.7

// yes, i know delegate is better, but the documentation specifically suggested using .live
$("#pageid").live("pageinit",function(){
    $.ajax({
        url: 'data.php',
        success: function(data) {    
            $("#result").html(data).trigger('create');
            $("#result").listview('refresh');
        }
    });
});

Post-JQuery 1.7

$(document).on("pageinit","#pageid",function(){
    $.ajax({
        url: 'data.php',
        success: function(data) {    
            $("#result").html(data).trigger('create');
            $("#result").listview('refresh');
        }
    });
});

Lý do làm mới trang hoạt động là vì khi bạn làm mới trang, sự kiện $(document).ready() xảy ra.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (1, 'Alvina', 'Champlin', '3605 Larson Lawn \ ncasandraville, VT 73844-0293', '', '+15718182740', 'Congo', 'North Brody');INTO`customers`(`id`,`first_name`,`last_name`,`address`, `email`,`phone`,`country`,`city`)VALUES(1,'Alvina','Champlin','3605 Larson Meadow\nCasandraville, VT 73844-0293','','+15718182740','Congo','North Brody');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (2, 'jeanette', 'johns', '38265 khóa học Stehr Apt.INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(2,'Jeanette','Johns','38265 Stehr Course Apt. 958\nSouth Berthafort, AK 47740','','1-761-330-5631 x8750','Isle of Man','North Josephine');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (3, 'felicity', 'walsh', '392 Núi Kautzer \ Naronstad, fl 34152', '', '1-528-589-0695', 'Belize', 'Katrineberg');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(3, 'Felicity','Walsh','392 Kautzer Mountain\nAronstad, FL 34152','','1-528-589-0695','Belize','Katrineberg');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (4, 'Alexandria', 'Goodwin', '7192 Công đoàn Dietrich Suite 178 \ Nnorth Fatimamouth, AL 54635-8353', '', '1-380-388-6370 x8648', 'Guam', 'Collierville');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(4,'Alexandria','Goodwin','7192 Dietrich Unions Suite 178\nNorth Fatimamouth, AL 54635-8353','','1-380-388-6370 x8648','Guam','Collierville');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (5, 'Francis', 'paucek', '19435 Cicero Well \ nwest Jarrell, MA 51035-4794', '', '1-995-393-1347 x986', 'Đảo Bouvet (Bouvetoya)', 'New Eliseohaven');INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(5,'Francis','Paucek','19435 Cicero Well\nWest Jarrell, MA 51035-4794','','1-995-393-1347 x986','Bouvet Island (Bouvetoya)','New Eliseohaven');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (6, 'jerrod', 'Kilback', '359 Roob Fall Suite 950 \ Neast Jaida, ME 49692-0730', '', '836.979.1645 x343', 'Ấn Độ', 'Boyerhaven');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(6, 'Jerrod','Kilback','359 Roob Fall Suite 950\nEast Jaida, ME 49692-0730','','836.979.1645 x343','India','Boyerhaven');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (7, 'Royal', 'Waelchi', '29651 Moen Shores Apt.INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(7,'Royal','Waelchi','29651 Moen Shores Apt. 710\nWest Ninabury, AL 37371-4514','','937-421-6387 x182','Netherlands Antilles','Lake Lorinemouth');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (8, 'Prudence', 'Padberg', '11889 Cathy Stravenue \ Nwalshfort, IA 12348-3147', '', '(802) 955-6535 x78249', 'Peru', 'Albinaborough');INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(8,'Prudence','Padberg','11889 Cathy Stravenue\nWalshfort, IA 12348-3147','','(802) 955-6535 x78249', 'Peru','Albinaborough');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (9, 'wilmer', 'stoltenberg', '324 Tòa án Izaiah \ nshyannville, Mn 58488', '', '+1.425.306.7890', 'Anh', 'East Helmer');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(9, 'Wilmer','Stoltenberg','324 Izaiah Courts\nShyannville, MN 58488','','+1.425.306.7890','UK','East Helmer');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (10, 'Bulah', 'Rohan', '4859 Quần đảo Blake Suite 518 \ Nkleinton, hoặc 86307', '', '(412) 934-6791', 'Georgia', 'Adahview');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(10,'Bulah','Rohan','4859 Blake Islands Suite 518\nKleinton, OR 86307', '','(412) 934-6791','Georgia','Adahview');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (11, 'Roselyn', 'Lockman', '4345 Nitzsche Mountains Suite 107 \ Neast Lavonne, PA 92446', '', '1-710-786-9448', 'Monaco', 'Eugeniahaven');INTO`customers`(`id`,`first_name`, `last_name`,`address`,`email`,`phone`,`country`,`city`)VALUES (11,'Roselyn','Lockman','4345 Nitzsche Mountains Suite 107\nEast Lavonne, PA 92446','','1-710-786-9448','Monaco','Eugeniahaven');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (12, 'lora', 'Ryan', '51854 Stefan Mission Apt.INTO`customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(12,'Lora','Ryan','51854 Stefan Mission Apt. 834\nNew Adolfotown, HI 18148', '','(280) 692-7491','Bulgaria','New Wilhelmineville');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (13, 'công chúa', 'sanford', '816 Welch Land Suite 770 \ nluciusshire, MS 42451-5941', '', '937.706.9143', 'Comoros', 'Uptonton');INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(13,'Princess','Sanford','816 Welch Land Suite 770\nLuciusshire, MS 42451-5941','','937.706.9143', 'Comoros','Uptonton');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (14, 'yadira', 'luettgen', '16318 Bonita Bridge Apt.INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(14, 'Yadira','Luettgen','16318 Bonita Bridge Apt. 709\nAthenaport, IL 26511','','+1-294-375-1907','Vanuatu','Zulaufborough');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (15, 'treva', 'jones', '10151 Shawna Shore Apt.INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(15,'Treva','Jones','10151 Shawna Shore Apt. 033\nMarjoriemouth, KY 22030-1574','','1-391-670-3346 x25463','Cayman Islands','Lake Flavio');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (16, 'lincoln', 'bode', '1414 Reinger Rest Apt.INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(16,'Lincoln','Bode','1414 Reinger Rest Apt. 707\nSouth Kayachester, AR 39004-3831','','+1.963.935.0710','Turks and Caicos Islands','North Adam');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (17, 'ilene', 'Marks', '4231 Minnie Ferry \ Nmedhurstburgh, WY 70923-2667', '', '896.479.9846 x3199', 'Đảo nghe và Quần đảo McDonald', 'New Yesseniaburgh');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(17, 'Ilene','Marks','4231 Minnie Ferry\nMedhurstburgh, WY 70923-2667','','896.479.9846 x3199','Heard Island and McDonald Islands','New Yesseniaburgh');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (18, 'Francisca', 'Rowe', '853 Albina Groves \ Nasamouth, NM 27737-1890', '', '528.595.8020', 'Myanmar', 'Murazikfort');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(18,'Francisca','Rowe','853 Albina Groves\nAsamouth, NM 27737-1890', '','528.595.8020','Myanmar','Murazikfort');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (19, 'Alexie', 'Langosh', '80539 Barrows luồng Apt. 651 \ nnorth Joshuahaven, CO 01023-3558', '', '(658) 878-7250 x92671', 'Cocos (Keeling) Islands', 'Pollyton');INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(19,'Alexie','Langosh','80539 Barrows Stream Apt. 651\nNorth Joshuahaven, CO 01023-3558','','(658) 878-7250 x92671', 'Cocos (Keeling) Islands','Pollyton');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (20, 'Macey', 'Towne', '3383 Andrew Lights \ NSouth Lilianechester, DC 59528-5129', '', '217.941.6891 x200', 'Monaco', 'Wolfshire');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(20, 'Macey','Towne','3383 Andrew Lights\nSouth Lilianechester, DC 59528-5129','','217.941.6891 x200','Monaco','Wolfshire');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (21, 'Chanel', 'Metz', '46394 Henri Ridge Suite 479 \ Ncarterburgh, WY 68717', '', '791-339-7627 x997', 'Botswana', 'New Willhaven');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(21,'Chanel','Metz','46394 Henri Ridge Suite 479\nCarterburgh, WY 68717', '','791-339-7627 x997','Botswana','New Willhaven');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (22, 'amy', 'kihn', '9103 dãy núi Colt \ NSouth Virgieburgh, NM 94218', '', '1-767-419-4318 x078', 'Honduras', 'Port Jermaineville');INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(22,'Amy','Kihn','9103 Colt Mountains\nSouth Virgieburgh, NM 94218','','1-767-419-4318 x078', 'Honduras','Port Jermaineville');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` address`, `email`,` điện thoại`, `quốc gia`,` City`) (23, 'Bell', 'Considine', '757 Aurelia Dam Suite 479 \ norisside, TN 21421-1341', '', '267-277-9949', 'Đan Mạch', 'West Jannie');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(23, 'Bell','Considine','757 Aurelia Dam Suite 479\nOtisside, TN 21421-1341','','267-277-9949','Denmark','West Jannie');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (24, 'Bessie', 'Marvin', '8656 Vivienne Expressway \ Ncrooksshire, hoặc 27293', '', '(886) 260-6447 x45131', 'Saint Helena', 'McClureland');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(24,'Bessie','Marvin','8656 Vivienne Expressway\nCrooksshire, OR 27293', '','(886) 260-6447 x45131','Saint Helena','McClureland');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (25, 'Wilmer', 'Fahey', '815 ned cầu cạn apt.INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(25,'Wilmer','Fahey','815 Ned Viaduct Apt. 575\nNew Agustinmouth, IN 62033','','647-912-3180 x0905','Saint Vincent and the Grenadines','South Abbiefurt');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (26, 'jettie', 'leffler', '54351 Johanna Radial Apt. 305 \ NKEELELFORT, AL 64645-3086', '', '662.543.9041 x0938', 'Western Sahara', 'New LaVernaview');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(26, 'Jettie','Leffler','54351 Johanna Radial Apt. 305\nKeelingfort, AL 64645-3086','','662.543.9041 x0938','Western Sahara','New Lavernaview');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (27, 'serena', 'trantow', '7674 Hirthe Street \ nschroederton, DE 90488-5532', '', '563.692.9141 x701', 'Guinea-Bissau', 'Hồ Ambrose');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(27,'Serena','Trantow','7674 Hirthe Street\nSchroederton, DE 90488-5532', '','563.692.9141 x701','Guinea-Bissau','Lake Ambrose');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (28, 'Bradly', 'Wiegand', '646 Yost Landing Apt.INTO`customers`(`id`, `first_name`,`last_name`,`address`,`email`,`phone`,`country`, `city`)VALUES(28,'Bradly','Wiegand','646 Yost Landing Apt. 211\nHagenesborough, KS 13712','','+1 (227) 935-1583', 'Uzbekistan','Daneland');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` city`) (29, 'ford', 'zboncak', '9406 Akeem Harbors Suite 742 \ Nfarrellfort, WV 29735', '', '557-538-5973 x165', 'Iraq', 'New Darienhaven');INTO`customers`(`id`,`first_name`,`last_name`, `address`,`email`,`phone`,`country`,`city`)VALUES(29, 'Ford','Zboncak','9406 Akeem Harbors Suite 742\nFarrellfort, WV 29735','','557-538-5973 x165','Iraq','New Darienhaven');

Chèn vào`customers` (`id`,` first_name`, `last_name`,` địa chỉ`, `email`,` điện thoại`, `quốc gia`,` City`) (30, 'Daphne', 'Corkery', '339 Angel Cliffs Suite 830 \ nlake Chaunceyburgh, NY 07754-0015', '', '259.748.0377', 'Indonesia', 'Powlowskifurt');INTO `customers`(`id`,`first_name`,`last_name`,`address`,`email`, `phone`,`country`,`city`)VALUES(30,'Daphne','Corkery','339 Angel Cliffs Suite 830\nLake Chaunceyburgh, NY 07754-0015','','259.748.0377','Indonesia','Powlowskifurt');

Làm cách nào để tải một trang trong cuộc gọi Ajax?

$ (bộ chọn) .Load (url, dữ liệu, gọi lại); Tham số URL cần thiết chỉ định URL bạn muốn tải ...
Trả lời - chứa nội dung kết quả nếu cuộc gọi thành công ..
Statustxt - chứa trạng thái của cuộc gọi ..
XHR - Chứa đối tượng xmlhttprequest ..

Làm thế nào Ajax gọi chuyển hướng đến một trang khác trong PHP?

Nếu bạn quan tâm đến việc sử dụng AJAX, thì mã là đây:..
$.Ajax ({.
Loại: "Bài",.
URL: Requrl,.
Dữ liệu: Reqbody,.
Kiểu dữ liệu: "JSON",.
Thành công: Hàm (dữ liệu, TextStatus) {.
if (dữ liệu. chuyển hướng) {.
// dữ liệu.Chuyển hướng chứa URL chuỗi để chuyển hướng đến ..

AJAX có thể được sử dụng với PHP không?

Bắt đầu sử dụng AJAX ngay hôm nay trong hướng dẫn PHP của chúng tôi, chúng tôi sẽ chứng minh cách AJAX có thể cập nhật các phần của trang web, mà không cần tải lại toàn bộ trang.Tập lệnh máy chủ sẽ được viết bằng PHP.Nếu bạn muốn tìm hiểu thêm về Ajax, hãy truy cập hướng dẫn Ajax của chúng tôi.The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.

Tôi có thể sử dụng Ajax mà không cần URL không?

Nếu không có URL, bạn sẽ cần sử dụng địa chỉ IP của máy chủ để gọi API bằng AJAX.Nói cách khác, bạn phải có một cái gì đó sẽ định tuyến cuộc gọi AJAX của bạn đến máy chủ thích hợp.you would need to use the server's IP address to call an API using Ajax. In other words, you have to have something that will route your Ajax call to the proper server.