Bạn có thể chèn tập lệnh vào HTML như trong bất kỳ trang nào khác (không phải Php), PHP xử lý nó như bất kỳ mã nào khác:
<button id="butt"> → Click ME! ← </button> <script> document.getElementById("butt"). title = function () { alert("Message"); } </script>Bạn có thể sử dụng các thuộc tính onSOMETHING:
<button title="alert('Message')">Button</button>Để tạo tin nhắn trong PHP, hãy sử dụng hàm json_encode (nó có thể chuyển đổi thành JavaScript mọi thứ có thể được thể hiện trong JSON - mảng, đối tượng, chuỗi, Hồi):
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button>Nếu bạn tạo mã cho các thẻ <script>, không sử dụng <button title="alert('Message')">Button</button> 0 hoặc chức năng tương tự:NOT use <button title="alert('Message')">Button</button> 0 or similar function:
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script>Bạn có thể tạo toàn bộ các tệp JavaScript, không chỉ JavaScript được nhúng vào HTML. Bạn vẫn phải đặt tên cho chúng với tiện ích mở rộng <button title="alert('Message')">Button</button> 1 (như <button title="alert('Message')">Button</button> 2). Chỉ cần gửi tiêu đề chính xác.
<button title="alert('Message')">Button</button> 2 - Tệp JavaScript
<?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?> <?php $var = "Message"; ?> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); }<button title="alert('Message')">Button</button> 4 - Trang ví dụ sử dụng <button title="alert('Message')">Button</button> 2
<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html>Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận Write JavaScript code within PHP code
<button title="alert('Message')">Button</button> 6
JavaScript là ngôn ngữ kịch bản phía máy khách và PHP là ngôn ngữ kịch bản phía máy chủ. JavaScript được sử dụng làm phía máy khách để kiểm tra và xác minh chi tiết máy khách và PHP là phía máy chủ được sử dụng để tương tác với cơ sở dữ liệu. Trong PHP, HTML được sử dụng làm chuỗi trong mã. Để hiển thị nó cho trình duyệt, chúng tôi tạo mã JavaScript dưới dạng chuỗi trong mã PHP.
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 1<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 2<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 3<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 4
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 1<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 6
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 7
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 8
Output:
Ví dụ 1: Viết mã JavaScript trong mã PHP Write JavaScript code outside of PHP code (in same PHP file)
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 9
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 8
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 1<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 2<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 3
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 4<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 5<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 6<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 4
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 8
Output:
Ví dụ 3: Hàm JavaScript - thao tác DOM (trong cùng một tệp PHP) JavaScript Function – DOM Manipulation (in same PHP file)
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 9
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 4<button title="alert('Message')">Button</button> 7 <?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?> <?php $var = "Message"; ?> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } 2<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 7
<?php $message = "Your message variable"; ?> <button title="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button> 8
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 1<button title="alert('Message')">Button</button> 9<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 3
<?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?> <?php $var = "Message"; ?> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } 8 <?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?> <?php $var = "Message"; ?> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } 9
<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 0<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 1<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 2
<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 3 <!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 4
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 4<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 6 <!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 7
<!doctype html> <html lang=en> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> <button id="butt"> BUTTON </button> <script src="script.js"></script> </body> </html> 8
<?php $var = "Test string"; ?> <button id="butt">Button</button> <script> document.getElementById("butt"). title = function () { alert(<?=json_encode($var)?>); } </script> 8
Output:
110JavaScript đượ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.