Làm cách nào để hiển thị biến PHP trong JavaScript?

This post is with regards to handling forms that have more than one submit button.

Suppose we have an HTML form with a submit button specified like this:

Normally the 'value' attribute of the HTML 'input' tag [in this case "Delete"] that creates the submit button can be accessed in PHP after post like this:

$_POST['action_button'];
?>

We of course use the 'name' of the button as an index into the $_POST array.

This works fine, except when we want to pass more information with the click of this particular button.

Imagine a scenario where you're dealing with user management in some administrative interface.  You are presented with a list of user names queried from a database and wish to add a "Delete" and "Modify" button next to each of the names in the list.  Naturally the 'value' of our buttons in the HTML form that we want to display will be "Delete" and "Modify" since that's what we want to appear on the buttons' faceplates.

Both buttons [Modify and Delete] will be named "action_button" since that's what we want to index the $_POST array with.  In other words, the 'name' of the buttons along cannot carry any uniquely identifying information if we want to process them systematically after submit. Since these buttons will exist for every user in the list, we need some further way to distinguish them, so that we know for which user one of the buttons has been pressed.

Using arrays is the way to go.  Assuming that we know the unique numerical identifier of each user, such as their primary key from the database, and we DON'T wish to protect that number from the public, we can make the 'action_button' into an array and use the user's unique numerical identifier as a key in this array.

Suppose we have an HTML form with a submit button specified like this:0

Suppose we have an HTML form with a submit button specified like this:1

Suppose we have an HTML form with a submit button specified like this:2

Suppose we have an HTML form with a submit button specified like this:3

Suppose we have an HTML form with a submit button specified like this:4

Suppose we have an HTML form with a submit button specified like this:5

Suppose we have an HTML form with a submit button specified like this:6

Suppose we have an HTML form with a submit button specified like this:7

Ký hiệu "Chấm" có thể được sử dụng để tham chiếu các chế độ xem lồng nhau. Quyền Dưới đây, dữ liệu sẽ hiển thị trên

public function show[Permission $permission]
{
return view['admin.permission.show', compact['permission']];
}
2

public function show[Permission $permission]
{
return view['admin.permission.show', compact['permission']];
}

Đừng mắc lỗi này trong bộ sưu tập Laravel

Tránh sử dụng chức năng trống trong bộ sưu tập Laravel

Blog. thiên tài. io

Truyền một biến PHP cho JavaScript

Nhưng trong một số trường hợp, bạn muốn chuyển một số var phía máy chủ sang JavaScript. Ví dụ: yêu cầu một số giá trị cho phân tích. Chúng ta có thể chuyển các biến bằng cách sử dụng gói Transform PHP Vars to JavaScript. Ngoài ra, chúng tôi có thể làm mà không cần sử dụng gói này

  • 1. Sử dụng kết xuất JSON
  • 2. Sử dụng Chuyển đổi PHP Vars sang gói JavaScript

1. không có gói. Sử dụng kết xuất JSON

Trong Laravel sẽ được dùng để khởi tạo một biến JavaScript


var app = {{ Js::from[$array] }};

Thí dụ

Bây giờ hãy thêm dữ liệu mảng vào tuyến ứng dụng. Mảng page_data này chúng ta cần sử dụng làm JavaScript

Route::get['/', function [] {
return view['welcome', ['page_data' => [
'title' => 'Home',
'price' => 20
]]];
}];

Thêm Js bên dưới. từ trong chế độ xem chào mừng

public function show[Permission $permission]
{
return view['admin.permission.show', compact['permission']];
}
3


var page_data = {{ Js::from[$page_data] }};

mở trình duyệt và kiểm tra page_data trong bảng điều khiển

Ngừng sử dụng DateTime trong PHP

Tại sao chúng ta cần sử dụng DateTime không thay đổi trong PHP?

Blog. thiên tài. io

2. Sử dụng Chuyển đổi PHP Vars sang gói JavaScript

Cài đặt gói thông qua Composer

composer require laracasts/utilities

Thêm gói vào nhà cung cấp

public function show[Permission $permission]
{
return view['admin.permission.show', compact['permission']];
}
4

________số 8_______

Tạo HomeController và gọi chế độ xem chào mừng

public function show[Permission $permission]
{
return view['admin.permission.show', compact['permission']];
}
5

Chủ Đề