Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

2

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 đã tạo một JavaScript để kiểm tra các hộp văn bản trống. Nếu một trong hộp văn bản trống thì trả về sai. Vậy làm thế nào tôi có thể nhận được giá trị trả về đó thành một biến PHP?

Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

Krish r

22.3K7 Huy hiệu vàng 50 Huy hiệu bạc57 Huy hiệu Đồng7 gold badges50 silver badges57 bronze badges

Đã hỏi ngày 25 tháng 12 năm 2013 lúc 8:11Dec 25, 2013 at 8:11

Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

3

Nếu bạn đang muốn gán giá trị trả về trong cùng một trang




hoặc sử dụng dòng này trong nút gửi để đăng kết quả lên một trang khác

Kết quả = Kết quả xác thực của bạn

window.location.href="index.php?uid=result";

Gọi cái này trong trang khác để nhận kết quả trả lại

$somevar = $_GET["uid"]; //puts the uid varialbe into $somevar

Đã trả lời ngày 25 tháng 12 năm 2013 lúc 8:28Dec 25, 2013 at 8:28

Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

KirkkirkKirk

4.8772 Huy hiệu vàng31 Huy hiệu bạc56 Huy hiệu Đồng2 gold badges31 silver badges56 bronze badges

1

Để liên kết JavaScript với PHP cần sử dụng Ajax http://api.jquery.com/jquery.ajax/

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

Đã trả lời ngày 25 tháng 12 năm 2013 lúc 8:23Dec 25, 2013 at 8:23

Thực hiện việc gửi hoặc chuyển hướng URL với các tham số hoặc yêu cầu AJAX cho tập lệnh PHP của bạn và nếu truy vấn URL của bạn là ?foo=bar, thì trong PHP của bạn, bạn có thể nhận được nó như thế này:


Đã trả lời ngày 25 tháng 12 năm 2013 lúc 8:25Dec 25, 2013 at 8:25

LexlexLex

2.5361 Huy hiệu vàng20 Huy hiệu bạc37 Huy hiệu đồng1 gold badge20 silver badges37 bronze badges

1

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    Bàn luận This example uses form element and GET/POST method to pass JavaScript variables to PHP. The form of contents can be accessed through the GET and POST actions in PHP. When the form is submitted, the client sends the form data in the form of a URL such as:

    https://example.com?name=value

    Loại URL này chỉ hiển thị nếu chúng ta sử dụng hành động Get, hành động bài đăng ẩn thông tin trong URL.

    Phía khách hàng:

    <html____10

    <

    window.location.href="index.php?uid=result";
    
    2
    window.location.href="index.php?uid=result";
    
    0

    window.location.href="index.php?uid=result";
    
    4<
    window.location.href="index.php?uid=result";
    
    6
    window.location.href="index.php?uid=result";
    
    0

    window.location.href="index.php?uid=result";
    
    8
    window.location.href="index.php?uid=result";
    
    9

    window.location.href="index.php?uid=result";
    
    4
    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1
    window.location.href="index.php?uid=result";
    
    6
    window.location.href="index.php?uid=result";
    
    0

    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1
    window.location.href="index.php?uid=result";
    
    2
    window.location.href="index.php?uid=result";
    
    0

    <

    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    8
    window.location.href="index.php?uid=result";
    
    0

    window.location.href="index.php?uid=result";
    
    4<
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    2
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    3
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    4
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    5____10

    window.location.href="index.php?uid=result";
    
    8
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    8

    window.location.href="index.php?uid=result";
    
    4
    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    2
    window.location.href="index.php?uid=result";
    
    0

    window.location.href="index.php?uid=result";
    
    4<
    
    
    5
    
    
    6
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    4
    
    
    8

    window.location.href="index.php?uid=result";
    
    8<
    https://example.com?name=value
    8
    https://example.com?name=value
    9
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    4?foo=bar1

    window.location.href="index.php?uid=result";
    
    8<
    https://example.com?name=value
    8
    https://example.com?name=value
    9
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    44 5
    $.ajax({
      type: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
    
    477

    window.location.href="index.php?uid=result";
    
    4
    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1
    
    
    5
    window.location.href="index.php?uid=result";
    
    0

    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1
    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    8
    window.location.href="index.php?uid=result";
    
    0

    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1html
    window.location.href="index.php?uid=result";
    
    0

    Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

    Phía máy chủ (PHP): Trên trang PHP phía máy chủ, chúng tôi yêu cầu dữ liệu được gửi bởi biểu mẫu và hiển thị kết quả. On the server side PHP page, we request for the data submitted by the form and display the result.

    <9

    Is

    html6 html0html8

    html9

    Output:

    Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

    Phương pháp 2: Sử dụng cookie để lưu trữ thông tin: phía khách hàng: Sử dụng cookie để lưu trữ thông tin, sau đó được yêu cầu trong trang PHP. Một cookie có tên GFG được tạo trong mã bên dưới và giá trị GeekSforGeek được lưu trữ. Trong khi tạo cookie, thời gian hết hạn cũng nên được chỉ định, đó là 10 ngày cho trường hợp này.
    Client Side: Use Cookie to store the information, which is then requested in the PHP page. A cookie named gfg is created in the code below and the value GeeksforGeeks is stored. While creating a cookie, an expire time should also be specified, which is 10 days for this case.

    <

    window.location.href="index.php?uid=result";
    
    01
    window.location.href="index.php?uid=result";
    
    0

    window.location.href="index.php?uid=result";
    
    03

    window.location.href="index.php?uid=result";
    
    04

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    06

    window.location.href="index.php?uid=result";
    
    07

    window.location.href="index.php?uid=result";
    
    08

    window.location.href="index.php?uid=result";
    
    09

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    11

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    13

    window.location.href="index.php?uid=result";
    
    8
    window.location.href="index.php?uid=result";
    
    15

    window.location.href="index.php?uid=result";
    
    8
    window.location.href="index.php?uid=result";
    
    17

    window.location.href="index.php?uid=result";
    
    8
    window.location.href="index.php?uid=result";
    
    19

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    21

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    23

    window.location.href="index.php?uid=result";
    
    8
    window.location.href="index.php?uid=result";
    
    25

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    21

    window.location.href="index.php?uid=result";
    
    4
    window.location.href="index.php?uid=result";
    
    29

    window.location.href="index.php?uid=result";
    
    8
    window.location.href="index.php?uid=result";
    
    31

    window.location.href="index.php?uid=result";
    
    21

    $somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
    
    1
    window.location.href="index.php?uid=result";
    
    01
    window.location.href="index.php?uid=result";
    
    0

    Phía máy chủ (PHP): Ở phía máy chủ, chúng tôi yêu cầu cookie bằng cách chỉ định tên GFG và trích xuất dữ liệu để hiển thị nó trên màn hình. On the server side, we request for the cookie by specifying the name gfg and extract the data to display it on the screen.

    <9

    window.location.href="index.php?uid=result";
    
    4html6
    window.location.href="index.php?uid=result";
    
    39html3
    window.location.href="index.php?uid=result";
    
    41html5

    html9

    Output:

    Hướng dẫn return value from javascript to php - trả về giá trị từ javascript sang php

    JavaScript được biết đến nhiều nhất để phát triển trang web nhưng nó cũng được sử dụng trong nhiều môi trường không phải là trình duyệt. Bạn có thể tìm hiểu JavaScript từ cơ sở bằng cách làm theo các ví dụ JavaScript và JavaScript này.

    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.


    Tôi có thể sử dụng biến JavaScript trong PHP không?

    Cách để chuyển biến JavaScript sang PHP là thông qua yêu cầu.Loại URL này chỉ hiển thị nếu chúng ta sử dụng hành động Get, hành động bài đăng ẩn thông tin trong URL.Phía máy chủ (PHP): Trên trang PHP phía máy chủ, chúng tôi yêu cầu dữ liệu được gửi bởi biểu mẫu và hiển thị kết quả.$ result = $ _get ['dữ liệu'];. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

    Làm thế nào sử dụng biến JavaScript trên cùng một trang trong PHP?

    Bạn có thể dễ dàng nhận được giá trị biến JavaScript trên cùng một trang trong PHP.Hãy thử codel sau.var res = "thành công"; php echo "