

PHP IF ELSE STATEMENT WITH POST DATA CODE
$_POST element, and branch the code appropriately. The processing script needs to check the value of the AnĪlternative is giving all the submit buttons the same name, but different values: Using different name attributes for the submit buttons in the form. In the previous example, I distinguished between buttons by Statement to handle the different code branches more efficiently. If you have a bunch of submits, a good idea is to use a switch/case Two keys appears in the $_POST array and branch the code appropriately. “if” test at the top of the PHP form processor to see which of the

(1) give each submit button in the form a unique name and (2) write a simple $_POST PHP array and is keyed by its “name” attribute, it’s simple to Since you already know that the submit button appears in the Processor, that PHP script must be revised to “know” which one was Since both submit buttons will invoke the same PHP form Let’s now make things a bit more complicated by adding a
PHP IF ELSE STATEMENT WITH POST DATA HOW TO
Now let’s take a look at how to handle multi-button You can look inside theĪrray and clearly see the correspondence between the form controls and their This is clearly visible by adding the line: When the form is submitted, the submit button itself becomesĪn element in the $_POST array, with a key corresponding to its Pay special attention to how the submit button is handled in Pairs in this array and can then be accessed using regular array notation. ValuesĮntered into the form input fields are automatically converted to key-value Of submission was used (I’ll assume POST throughout this tutorial). When a form is submitted to a PHP script, PHP automaticallyĬreates a special $_POST or $_GET associative array, depending on which method Here is a form:Īnd here is the processor.php script that gets invoked on Submit button-to ensure you are clear on the basics and to provide a base for

I’ll begin with a simple example-a single form with a single The intelligence to distinguish between each one. Single form carrying multiple submit buttons, and a form-processing script with PHP script had to handle all four tasks, by identifying which button had beenĬlicked and executing the appropriate code branch. Since a form can only have a single action, the same (check in/out was connected with membership records mark as missing/sold Was clicked, the data entered in the form was processed in a different way Which had buttons corresponding to the actions above. On that record: check out to a member, check in from a member, mark as missing,Īll these activities were handled through a single form, Interface to view the record for any book and then perform one of four actions The book titles wereĪll stored in a database, and administrators could use a browser-based In that project, I was tasked withīuilding an inventory system for a book-lending library. The best way to explain such a need is with an actualĮxample from the last project I worked on. Question: Since most forms are perfectly happy with a single submit button, why Why multiple submits?īefore I get started, though, let me answer the obvious Topic of today’s discussion, handling different actions through multiple submitīuttons in the same form. Makes it possible to do some fairly complex things with forms…including the If you’ve been using the language for a while. Most other Web programming languages-a fact you’re undoubtedly already aware of Processing form data in PHP is significantly simpler than in

This simplicity and ease of use makes it possible to do some fairly complex things with forms, including handling multiple submit buttons in the same form. Processing form data in PHP is significantly simpler than most other Web programming languages. Handling multiple submits in a single form with PHP
