Use PHP in the same form to handle multiple submit tasks _php basics

Source: Internet
Author: User
Tags php script

Working with form data in PHP is simpler than other Web programming languages-if you use the language for a while, you'll find it an indisputable fact. The simplicity of this operation makes it easy to handle more complex form events, including the topic discussed today, where multiple buttons are used to handle different tasks in the same form.

Why use multiple submit tasks?

Before I answer this question, let me answer the obvious question: since many forms are more suitable for a single submit button, why do people sometimes need two (or more) of submit buttons?

The best way to explain this problem is to use an example from my recent development project. In this project, my task is to create a detailed catalogue query system for a library. Book titles are stored in the database, and administrators will be able to use a browser-based interface to view the records of any of these books, and then choose one of four actions on the record: Member book registration, Member library registration, books lost records and book sales records.

All of the above tasks are handled through a separate form, which requires the appropriate buttons to respond to these tasks. The data that is passed to the form will be processed in a different way depending on which button is clicked (the Library/return book and member records are interrelated; the loss/sales record changes the detailed catalog table). Because a form can only handle a single task, the same PHP script can handle the above four tasks based on the clicked button and executing the appropriate code snippet. Therefore, you need to handle a single form of multiple submit task buttons, and a form-handling code snippet that implements the automatic response of different buttons.

Let me start by enumerating a simple example: a form that submits a button. This gives you a clear understanding of the basic concepts and lays the groundwork for the complex paradigm that will be described. Here is a form:

<body>

<form action= "processor.php" method= "POST" > Enter a number: <input type= "text" name= "number" size= "3" > < Br>
<input type= "Submit" Name= "Submit" > </form>

</body>

The following is the processor.php script that invokes the Submit task:

<?php

Check for submission
Retrieve value from posted data
if ($_post[' submit '])
{
echo "You entered the number". $_post[' number ']; }

?>

When a form is submitted to a PHP script, PHP automatically creates a specific $_post or $_get array, depending on the submission method used (I assume post in this article). The values you type into the form's input fields automatically convert the key data in the array, and you can use regular data symbols to access the data.

Notably, how to handle the push of a submit task in the above script. When a form is submitted, the Submit button is converted to an element in the $_post according to its actual "name". Adding the following line of code is clear:

Print_r ($_post);

To understand the above PHP script, you can look at the internal structure of the array, and you can see clearly how different forms of controls are interconnected.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.