Bạn có một vài lựa chọn.
Đơn giản nhất là chỉ cần có logic xử lý biểu mẫu trên cùng một trang và sau đó đặt thuộc tính hành động của biểu mẫu của bạn cho trang đó.
Bạn có thể làm một cái gì đó như kiểm tra nếu bạn có một giá trị được đăng để cho bạn biết nếu bạn cần xử lý trang.
if [isset[$_POST['my_value_from_form']]] {
// process
}
// body of page itself
Điều đó cho phép bạn đặt một cái khá dễ dàng.
Một cách khác là thêm giá trị nhận bằng cách thêm nó vào URL:
header["location: contact.php?message=1"]
Và sử dụng điều đó với $_GET['message']
để xác định những gì cần hiển thị. Tuy nhiên, đó? Tin nhắn = 1 sẽ nằm trong URL của trang, vì vậy nó có thể không lý tưởng.
Một cách khác là đặt giá trị phiên trước khi bạn trực tiếp, sau đó kiểm tra xem giá trị đó có ở đó không [và cũng xóa nó sau khi bạn hiển thị nó.
// on send_form_email.php
session_start[];
$_SESSION['message'] = 1;
// on contact.php
session_start[];
if [$_SESSION['message'] == 1] {
// do something
}
unset[$_SESSION['message']]; // so it only shows once.
Tất cả các cách có sự đánh đổi nhỏ, chủ yếu là do cách bạn sắp xếp mã của mình. Nếu tôi định thực hiện nó, tôi sẽ sử dụng phương thức phiên.
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Using Header Function in PHP:
The header[] function is an inbuilt function in PHP which is
used to send the raw HTTP [Hyper Text Transfer Protocol] header to the client.
Syntax:
header[ $header, $replace, $http_response_code ]
Bàn luận This function accepts three parameters as mentioned above and described below:
- Chuyển hướng từ trang này sang trang khác trong PHP thường đạt được bằng hai cách sau: sử dụng hàm tiêu đề trong PHP: & nbsp; hàm tiêu đề [] là hàm sẵn trong PHP được sử dụng để gửi HTTP thô [Giao thức chuyển văn bản Hyper] tiêu đề cho máy khách. & nbsp; cú pháp: & nbsp; This parameter is used to hold the header string.
- Tham số: Hàm này chấp nhận ba tham số như đã đề cập ở trên và được mô tả dưới đây: & nbsp; & nbsp; This parameter is used to hold the replace parameter which indicates the header should replace a previous similar header, or add a second header of the same type. It is optional parameter.
- Tiêu đề $: Tham số này được sử dụng để giữ chuỗi tiêu đề. This parameter hold the HTTP response code.
$ Thay thế: Tham số này được sử dụng để giữ tham số thay thế cho biết tiêu đề sẽ thay thế một tiêu đề tương tự trước đó hoặc thêm tiêu đề thứ hai cùng loại. Đó là tham số tùy chọn.
Program:
$ http_response_code: Tham số này giữ mã phản hồi HTTP.
Dưới đây chương trình minh họa hàm tiêu đề [] trong PHP: Chương trình: & nbsp; The die[] or exit[] function after header is mandatory. If die[] or exit[] is not put after the header[‘Location: ….’] then script may continue resulting in unexpected behavior. For
example, result in content being disclosed that actually wanted to prevent with the redirect [HTTP 301].
Using JavaScript via PHP:
The windows.location object in JavaScript is used to get the current page address[URL] and to redirect the browser to a new page. The window.location object contains the crucial information about a page such as hostname, href, pathname, port etc.
Example:
PHP
header["location: contact.php?message=1"]
3
// on send_form_email.php
session_start[];
$_SESSION['message'] = 1;
// on contact.php
session_start[];
if [$_SESSION['message'] == 1] {
// do something
}
unset[$_SESSION['message']]; // so it only shows once.
0header["location: contact.php?message=1"]
1>
Lưu ý: hàm chết [] hoặc thoát [] sau khi tiêu đề là bắt buộc. Nếu chết [] hoặc thoát [] không được đặt sau tiêu đề [‘vị trí:, thì] thì tập lệnh có thể tiếp tục dẫn đến hành vi bất ngờ. Ví dụ, kết quả là nội dung được tiết lộ thực sự muốn ngăn chặn với chuyển hướng [http 301]. & Nbsp; sử dụng javascript qua PHP: & nbsp; đối tượng windows.location trong javascript Chuyển hướng trình duyệt đến một trang mới. Đối tượng Window.location chứa các thông tin quan trọng về một trang như tên máy chủ, href, pathName, port, vv ví dụ: & nbsp;
header["location: contact.php?message=1"]
3URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:1
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:2
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:3
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:4
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:5
header["location: contact.php?message=1"]
3URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:1
header["location: contact.php?message=1"]
3URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:9
header["location: contact.php?message=1"]
3$_GET['message']
1URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:4
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:5
header["location: contact.php?message=1"]
3URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:1
header["location: contact.php?message=1"]
3URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:9
header["location: contact.php?message=1"]
3$_GET['message']
9URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:4
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:5
// on send_form_email.php
session_start[];
$_SESSION['message'] = 1;
// on contact.php
session_start[];
if [$_SESSION['message'] == 1] {
// do something
}
unset[$_SESSION['message']]; // so it only shows once.
0header[ $header, $replace, $http_response_code ]8
>
// on send_form_email.php
session_start[];
$_SESSION['message'] = 1;
// on contact.php
session_start[];
if [$_SESSION['message'] == 1] {
// do something
}
unset[$_SESSION['message']]; // so it only shows once.
0// on send_form_email.php
session_start[];
$_SESSION['message'] = 1;
// on contact.php
session_start[];
if [$_SESSION['message'] == 1] {
// do something
}
unset[$_SESSION['message']]; // so it only shows once.
5>
// on send_form_email.php
session_start[];
$_SESSION['message'] = 1;
// on contact.php
session_start[];
if [$_SESSION['message'] == 1] {
// do something
}
unset[$_SESSION['message']]; // so it only shows once.
0html
html
2Output:
URL: //ide.geeksforgeeks.org/tryit.php Hostname: ide.geeksforgeeks.org Protocol:
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.