Bạn không thể làm điều này bằng PHP mà không gửi trang. Mã PHP thực thi trên máy chủ trước khi trang được hiển thị trong trình duyệt. Sau đó, khi người dùng thực hiện bất kỳ hành động nào trên trang [e. g. chọn một mục trong danh sách thả xuống], không còn PHP nữa
Làm thế nào để bạn viết một hành động hai hình thức?
Làm cách nào để sử dụng nhiều nút gửi trong biểu mẫu HTML?
- Tạo biểu mẫu với phương thức 'đăng' và đặt giá trị của thuộc tính hành động thành URL mặc định nơi bạn muốn gửi dữ liệu biểu mẫu
- Tạo các trường đầu vào bên trong theo mối quan tâm của bạn
- Tạo một nút với loại gửi
- Tạo một nút khác với loại gửi
Tôi có thể có hai nút gửi trong một biểu mẫu không?
vâng, nhiều nút gửi có thể bao gồm trong biểu mẫu html. Một ví dụ đơn giản được đưa ra dưới đây. Ở đây tôi đang sử dụng MVC VB. mạng lưới. Trong chế độ xem, tôi đang sử dụng ba nút có cùng tên nhưng ở các giá trị khác nhau
bản thân là gì. trong PHP?
Bản thân PHP đề cập đến các thành viên của lớp, nhưng không phải cho bất kỳ đối tượng cụ thể nào. Điều này là do các thành viên tĩnh [biến hoặc hàm] là các thành viên lớp được chia sẻ bởi tất cả các đối tượng của lớp. Trong khi đó, $this sẽ tham chiếu các biến thành viên và hàm cho một trường hợp cụ thể
Các trang tự xử lý trong PHP là gì?
Trang tự xử lý một tập lệnh PHP duy nhất, e. g. , head_to_hat. php. khi được yêu cầu mà không có dữ liệu người dùng, nó sẽ xuất ra dạng. khi được yêu cầu với dữ liệu người dùng, nó sẽ nhận dữ liệu, thực hiện phép tính và xuất lại biểu mẫu nhưng có kèm theo kết quả
Làm cách nào để di chuyển một nút từ trang này sang trang khác trong PHP?
Chúng ta có thể sử dụng các thẻ Anchor để liên kết nút Gửi đến một trang khác trong PHP. Chúng ta cần Viết/Khai báo nút Gửi giữa thẻ Bắt đầu và thẻ Đóng của thẻ Anchor. Bằng cách sử dụng thuộc tính href=”” của thẻ Anchor, chúng tôi có thể cung cấp Đường dẫn nơi chúng tôi muốn Liên kết Nút Gửi của mình
Việc sử dụng hành động ở dạng HTML là gì?
Cách sử dụng HTML Form action Action được sử dụng để cung cấp tham chiếu/liên kết của một trang khác. Nếu chúng ta muốn tách logic nghiệp vụ [tập lệnh PHP] khỏi lớp Trình bày [tập lệnh HTML] thì hãy sử dụng Thuộc tính hành động của Biểu mẫu. Nó làm giảm độ phức tạp của số lượng lớn mã. Bởi vì Tất cả các tập lệnh được xác định riêng trên trang riêng của chúng
Đây là một cách bạn có thể làm điều này
Enter a Message
Your input:
Một cách khác bạn có thể làm điều đó. Điểm cần lưu ý loại đầu vào là nút
Thực hiện theo mã này
HTML JavaScript output on same page
Enter message:
Message is:
Bạn có thể làm cho nó cũng không có hình thức
HTML JavaScript output on same page
Enter message:
Message is:
Tại đây bạn có thể sử dụng nút gửi hoặc nút
Không cần phải thiết lập
return false ;
Một cách khác bạn có thể thử. Điều này sẽ làm việc
Lần này dành cho JavaScript
/**
* Class for getting form content
*
* @version 1.0.0
*/
class FormContent{
/**
* Create new form content object
*
* @param {HTMLFormElement} formElement Single form element
* @param {string} inputSelectors Selectors for elements which will be used to read and display data [like jQuery selectors: # for IDs, . for classes and so on]. Separate multiple selectors with comas.
* @param {HTMLButtonElement | HTMLInputElement} submitButton Submit button to display form data [although the type should be always 'button']
* @param {Element} outputSection Section where the form data is displayed
* @since 1.0.0
*/
constructor[formElement, inputSelectors, submitButton, outputSection]{
/**
* Disabled elements [values will not be shown in output]. Values can be tag names, attribute 'type' values or certain element [inside form]
*
* @type {Array}
* @since 1.0.0
*/
this.disabledElements = ["button", "reset", "submit"];
/**
* Input elements node list [created by inputSelectors]
*
* @type {NodeList}
* @since 1.0.0
*/
var inputElements = formElement.querySelectorAll[inputSelectors];
/**
* Get input elements
*
* @see inputElements
* @return {NodeList} Input elements
* @since 1.0.0
*/
this.getInputElements = function[]{ return inputElements; };
/**
* Get submit button
*
* @return {HTMLButtonElement} Submit button
* @since 1.0.0
*/
this.getSubmitButton = function[]{ return submitButton; };
/**
* Get output section
*
* @see outputSection
* @return {NodeList} Output section
* @since 1.0.0
*/
this.getOutputSection = function[]{ return outputSection; };
/**
* Empty input's alternative [print] value
*
* @type {string}
* @since 1.0.0
*/
this.emptyValueMessage = "Unknown";
/**
* Error message [when there is empty required fields]
*
* @type {string}
* @since 1.0.0
*/
this.errorMessage = "Please fill all the required inputs!
";
/**
* Instance for this class
*
* @type {FormContent}
* @since 1.0.0
*/
var thisInstance = this;
if[submitButton && outputSection]{
submitButton. title = function[]{
thisInstance.onSubmitButtonClick[];
};
}
}
/**
* When submit button is clicked
*
* @since 1.0.0
*/
onSubmitButtonClick[]{
var outputMessage = [this.areRequiredInputsFilled[]] ? this.getFormattedFormContent[] : this.errorMessage;
this.printToOutput[outputMessage];
}
/**
* Are all the required inputs/fields filled?
*
* @return {boolean}
* @since 1.0.0
*/
areRequiredInputsFilled[]{
for[var node of this.getInputElements[]]{
if[node.required && !node.value]{
return false;
}
}
return true;
}
/**
* Print/display form data to output element
*
* @see getOutputSections[]
* @since 1.0.0
*/
printToOutput[content]{
this.getOutputSection[].innerHTML = content;
}
/**
* Get form content/data which is formatted
*
* @return {string} Formatted form content
* @since 1.0.0
*/
getFormattedFormContent[]{
var formContent = "";
var formData = this.getFormData[];
for[var input in formData]{
formContent += "" + input + ": " + formData[input] + "
";
}
return formContent;
}
/**
* Get raw form data
*
* @return {json} Form data
* @since 1.0.0
*/
getFormData[]{
var formData = {};
var noNameCounter = 0;
var formInputs = this.getFormInputs[];
for[var input of formInputs]{
let inputName = input.name || "no_name_element_" + noNameCounter;
let inputValue = input.data || input.value || this.emptyValueMessage;
if[!input.name]{
noNameCounter++;
}
formData[inputName] = inputValue;
}
return formData;
}
/**
* Get all the form input elements
*
* @return {Array} Inputs and values [form data]
* @since 1.0.0
*/
getFormInputs[]{
var formInputs = [];
for[var input of this.getInputElements[]]{
if[!this.disabledElements.includes[input.tagName.toLowerCase[]] && !this.disabledElements.includes[input.type] && !this.disabledElements.includes[input]]{
if[input.type === "radio"]{
if[input.checked]{
formInputs.push[input];
}
}else if[input.type === "checkbox"]{
input.value = [input.checked] ? true : false;
formInputs.push[input];
}else if[input.multiple]{
formInputs.push[this.getMultipleInput[input]];
}else if[input.value || input.innerHTML]{
formInputs.push[input];
}else{
formInputs.push[input];
}
}
}
return formInputs;
}
/**
* Get input which has attribute multiple
*
* @param {HTMLInputElement} multipleInput Input with attribute multiple
* @return {HTMLInputElement} Input instance
* @since 1.0.0
*/
getMultipleInput[multipleInput]{
var inputInstance = document.createElement["input"];
inputInstance.name = multipleInput.name;
var values = [];
if[multipleInput.type !== "file"]{
for[var option of multipleInput]{
if[option.selected]{
values.push[option.value];
}
}
}else{
for[var file of multipleInput.files]{
values.push[file.name];
}
}
inputInstance.data = values.toString[];
return inputInstance;
}
}
var forms = document.getElementsByTagName["form"];
for[var form of forms]{
let inputSelectors = "input, select, textaera";
let submitButton = form.querySelector["#submit-button"];
let outputSection = form.querySelector["#output"];
new FormContent[form, inputSelectors, submitButton, outputSection];
}
Sự kết luận
Hy vọng nó đã giúp