Hướng dẫn call php function from href onclick - gọi hàm php từ href onclick

Đây là một giải pháp thay thế với Ajax nhưng không có jQuery, chỉ là JavaScript thông thường:

Thêm trang này vào trang PHP đầu tiên/chính, nơi bạn muốn gọi hành động từ đó, nhưng thay đổi nó từ thẻ a tiềm năng (siêu liên kết) thành phần tử button, vì vậy nó không được nhấp bởi bất kỳ bot hoặc ứng dụng độc hại nào (hoặc bất cứ điều gì) .







   

  
  

  

Responses from ajax-php-page.php:

Khi nhấp vào button, onclick sử dụng chức năng JavaScript của Heads để gửi $sendingValue qua AJAX đến một trang PHP khác, giống như nhiều ví dụ trước cái này. Trang khác, ajax-php-page.php, kiểm tra giá trị nhận và trả về với print_r:

");
  } else {
    print_r("The request didn´t pass correctly through the GET...");
  }

?>

Phản hồi từ print_r sau đó được trả về và hiển thị với

document.getElementById("results").innerHTML += this.responseText;

");
  } else {
    print_r("The request didn´t pass correctly through the GET...");
  }

?>
1 dân cư và thêm vào các phần tử HTML hiện có, xóa
");
  } else {
    print_r("The request didn´t pass correctly through the GET...");
  }

?>
2 chỉ cập nhật và thay thế các nội dung hiện có của phần tử HTML
");
  } else {
    print_r("The request didn´t pass correctly through the GET...");
  }

?>
3
");
  } else {
    print_r("The request didn´t pass correctly through the GET...");
  }

?>
4.

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

    Gọi chức năng PHP bằng nút HTML: Tạo tài liệu biểu mẫu HTML chứa nút HTML. Khi nút được nhấp, bài viết phương thức được gọi. Phương thức bài mô tả cách gửi dữ liệu đến máy chủ. Sau khi nhấp vào nút, hàm Array_Key_Exists () được gọi. Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

    Bàn luận

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    5

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    7
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    4
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    7

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    9

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1
    document.getElementById("results").innerHTML += this.responseText;
    
    4
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    a1

    document.getElementById("results").innerHTML += this.responseText;
    
    0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    Cho một tài liệu chứa mã HTML và PHP và nhiệm vụ là gọi chức năng PHP sau khi nhấp vào nút. Có nhiều phương pháp khác nhau để giải quyết vấn đề này. Ngoài ra, ngoài việc làm điều này với một nút bấm, một hàm PHP có thể được gọi bằng cách sử dụng Ajax, JavaScript và JQuery. Nhưng bài viết này chủ yếu tập trung vào cách tiếp cận định hướng nút của việc gọi hàm PHP.

    Chương trình 1:

    document.getElementById("results").innerHTML += this.responseText;
    
    6button1

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1button5
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6button8
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    7

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    9

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1button8
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    2onclick9$sendingValue0

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue2

    $sendingValue3$sendingValue4

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue6

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue8

    $sendingValue3ajax-php-page.php0

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue6

    document.getElementById("results").innerHTML += this.responseText;
    
    6ajax-php-page.php4

    $sendingValue3ajax-php-page.php6

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue6

    document.getElementById("results").innerHTML += this.responseText;
    
    6print_r0

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6a8 a9button0button1
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue6

    document.getElementById("results").innerHTML += this.responseText;
    
    2print_r6

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6button5 a9button0button8
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    $sendingValue3print_r2

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6print_r9
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    00button0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    022

    Các

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    13___

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1print_r9
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    a1

    document.getElementById("results").innerHTML += this.responseText;
    
    0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    Các

    Output:

    Hướng dẫn call php function from href onclick - gọi hàm php từ href onclick

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    13___

    a1

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    7
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8
    This program uses isset() function to call PHP function.

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    5

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    7
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    Chúng ta hãy xem xét các phương thức GET hoặc POST, vì hầu hết các nhà phát triển sử dụng phương thức POST do các vấn đề về quyền riêng tư, ví dụ sau chỉ dựa trên phương thức bài đăng:

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    4
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    7

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    9

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1
    document.getElementById("results").innerHTML += this.responseText;
    
    4
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    a1

    document.getElementById("results").innerHTML += this.responseText;
    
    0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    Cho một tài liệu chứa mã HTML và PHP và nhiệm vụ là gọi chức năng PHP sau khi nhấp vào nút. Có nhiều phương pháp khác nhau để giải quyết vấn đề này. Ngoài ra, ngoài việc làm điều này với một nút bấm, một hàm PHP có thể được gọi bằng cách sử dụng Ajax, JavaScript và JQuery. Nhưng bài viết này chủ yếu tập trung vào cách tiếp cận định hướng nút của việc gọi hàm PHP.

    Chương trình 1:

    document.getElementById("results").innerHTML += this.responseText;
    
    6button1

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1button5
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6button8
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    7

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    9

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1button8
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    document.getElementById("results").innerHTML += this.responseText;
    
    2onclick9$sendingValue0

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6a8 a9button0button1
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    $sendingValue3ajax-php-page.php6

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue6

    document.getElementById("results").innerHTML += this.responseText;
    
    6
    document.getElementById("results").innerHTML += this.responseText;
    
    11

    $sendingValue3print_r2

    document.getElementById("results").innerHTML += this.responseText;
    
    6$sendingValue6

    document.getElementById("results").innerHTML += this.responseText;
    
    2print_r6

    document.getElementById("results").innerHTML += this.responseText;
    
    2
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    6button5 a9button0button8
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    Các

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    13
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    17button0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    19
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    20

    Các

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    13
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    17button0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    36
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    20

    document.getElementById("results").innerHTML += this.responseText;
    
    2a1print_r9
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    a1

    document.getElementById("results").innerHTML += this.responseText;
    
    0
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    a1

    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    7
    ");
      } else {
        print_r("The request didn´t pass correctly through the GET...");
      }
    
    ?>
    
    8

    Output:

    Hướng dẫn call php function from href onclick - gọi hàm php từ href onclick


    Làm cách nào để gọi chức năng PHP khi nhấp vào liên kết?

    Gọi chức năng PHP bằng nút HTML: Tạo tài liệu biểu mẫu HTML chứa nút HTML. Khi nút được nhấp, bài viết phương thức được gọi. Phương thức bài mô tả cách gửi dữ liệu đến máy chủ. Sau khi nhấp vào nút, hàm Array_Key_Exists () được gọi.Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

    Bạn có thể sử dụng onclick với PHP không?

    Tôi có thể sử dụng onclick trong PHP không? Giải thích sai về sự kiện OnClick trong PHP Ngoài ra, PHP xử lý các yêu cầu máy khách phía máy chủ và kết nối cơ sở dữ liệu. PHP không có gì liên quan đến phía người dùng, tương tác trên màn hình. Vì vậy, chúng tôi không thể đặt một sự kiện nhấp chuột trên JavaScript phía máy khách với chức năng PHP.we can not set an on click event of client-side javascript with a PHP function.

    Làm thế nào để bạn gọi một chức năng trong PHP?

    Có hai phương pháp để làm điều này.Một được gọi trực tiếp chức năng theo tên biến bằng cách sử dụng khung và tham số và tên kia là bằng cách sử dụng hàm call_user_func () nhưng trong cả hai tên biến của phương thức sẽ được sử dụng.call_user_func ($ var);call_user_func ($ var1, "fun_function");calling function by variable name using bracket and parameters and the other is by using call_user_func() Function but in both method variable name is to be used. call_user_func( $var ); call_user_func( $var1 , "fun_function" );

    Làm thế nào để bạn gọi một nút chức năng trong HTML?

    Để gọi chức năng này trong tài liệu HTML, chúng tôi phải tạo một nút đơn giản và sử dụng thuộc tính sự kiện Onclick (là trình xử lý sự kiện) cùng với nó, chúng tôi có thể gọi chức năng bằng cách nhấp vào nút.create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button.