user5075858 user5075858 - 1 year ago 95
PHP Question

POST and GET methods calling functions in the same file

I've created an

insert.php
file where I have several insert functions. My forms are on different pages and some use
POST
and others use
GET
methods, but they all point to the
insert.php
file.

This was all coded in PHP5.2 and I didn't have much trouble specifying which insert function to execute based on which form is being submitted. However, in my attempts to upgrade to procedural PHP7, I find that my submitted form doesn't necessarily point to the correct insert function in the
insert.php
file anymore.

Here is an example of how I am doing this:

page1.php

<form action='insert.php' method='post'>
<input type='hidden' name='form' value='data1'>
...
</form>


insert.php

if($_POST['form']=='data1') {
//perform insert into db
}


I have different insert functions with different
$_POST['form']
variables. Some of these functions are called from forms that use the
GET
method, so I have
if($_GET['form']=='data9') {...}.


Questions:


  1. Why are some of my forms not pointing to the correct insert function?

  2. Is it better to put the
    GET
    and
    POST
    insert functions in different files, or is it okay to leave them in one file?

  3. I'm thinking of possibly using the below code to solve question 2.



Any thoughts on this solution?

$form = ($_SERVER['REQUEST_METHOD'] == 'GET') ? $_GET['form'] : $_POST['form'];

Answer Source

try this code: $form = $_REQUEST['form'];

$_REQUEST will fetch both POST and GET request variables.

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download