Php: $_Get and $_Post in Functions

PHP: $_GET and $_POST in functions?

When do you you need to include POST
and GET methods as parameters to
functions?

I would say "never": $_GET and $_POST are what is called superglobals: they exists in the whole script; which means they exist inside functions/methods.

Especially, you don't need to you the global keyword for those.



Still, relying on those in your functions/methods is quite a bad practice: your functions/methods should generally not depend on anything not passed as a parameter.

What I mean is; consider those two functions:

function check_login_password()
{
$login = $_GET['login'];
$password = $_GET['password'];
// Work with $login and $password
}

and

/**
* Check login and password
*
* @param $login string
* @param $password string
* @return boolean
*/
function check_login_password($login, $password)
{
// Work with $login and $password
}

OK, with the first one, you don't have to pass two parameters... But that function will not be independent and will not work in any situation where you'd have to check a couple of login/password that doesn't come from $_GET.

With the second function, the caller is responsible for passing the right parameters; which mean they can come from wherever you want: the function will always be able to do its job.

Passing $_POST/$_GET as a parameter to function

IMHO it's a practice of abstraction, and there are benefits:

  1. Generality: by receiving $_POST as a parameter, the function becomes less tightly coupled to $_POST. The function may serve more scenarios & possibly be more reusable.

  2. Inversion of control: because the function's dependency($_POST) is injected from outside, you have more control over the function. It is a bit unlikely but let's suppose your form has been updated and now you need to submit via GET method. Without modifying the function's body, passing in $_GET on the caller's side, is enough to reflect the change.

  3. Test fixture isolation: To mock FORM inputs to test a certain code path in the function, it is better access global states (such as $_POST) in an abstracted way, so that the test itself does not bring side effects to other part of the system.

Using GET and POST for a search function in PHP

If you want to have the same behavior for both $_GET and $_POST you can use the $_REQUEST superglobal.

If you want to know where your data is coming from, (wether it's a GET or POST request) you can simply check for the existence of the parameter on both arrays:

if(isset($_GET['search'])
{
//We have a GET request
}
else if(isset($_POST['search'])
{
//We have a POST request
}
else
{
//Nothing found
}

POST and GET methods calling functions in the same file

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

$_REQUEST will fetch both POST and GET request variables.

Hope this helps.

Php Get and post function not working

add type <button name="submit" type="submit"></button>

Tip: Always specify the type attribute for a element. Different browsers use different default types for the element.

also remove name="submit"

Pass POST data into a PHP function

Change this:

$this->editUserPicture($_POST['profil_slika']);

To this:

$this->editUserPicture('profil_slika');

And also add enctype="multipart/form-data" to the attributes of the HTML <form>.

The reason is that profil_slika will not be passed to the $_POST array. It will be in the $_FILES array, with key profil_slika. In other words, the key you need to use for the $_FILES array is the name of the HTML input, you don't need to use $_POST at all.

$_POST as a function parameter

$_POST is globally accessible. So you don't have to pass to your function:

if ($_POST['action'] == 'actionName') actionName(); 

function actionName() {
//code using $_POST
}

If you want your function to be able to do what it wants with any array (i.e. other than $_POST), then make the function take a parameter, but don't call it $_POST:

if ($_POST['action'] == 'actionName') actionName($_POST); 

function actionName(parameters) {
//code using parameters
}

Passing from values to a php function on post

Check the below example;

<?php
//send.php has the following code
$message_sent = isset($_POST['message']) ? $_POST['message'] : 'There is no message';
$t1 = isset($_POST['t1']) ? $_POST['t1'] : 'default something';
$t2 = isset($_POST['t2']) ? $_POST['t2'] : 'default something';

//create database connection

$sql = "SELECT DISTINCT msisdn FROM customer WHERE time_paid BETWEEN '$t1' AND '$t2'";

$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);

if($rowcount > 0){
$resultarr = mysqli_fetch_assoc($result); // fetch data
$mobilenumber = $resultarr['msisdn'];

// parameters goes in function...
sendbulk($mobilenumber, $message_sent);
}else{
echo 'Opppss!!! There is no result.';
}

function sendbulk($mobilenumber, $message_sent) {
global $conn;
echo "$message_sent";
echo '<br />';
echo "$mobilenumber";
$serviceArguments = array(
"mobilenumber" => $mobilenumber,
"message" => $message_sent
);

$client = new SoapClient("http://.......");

$result = $client->process($serviceArguments);
$conn->close();

return $result;
}


Related Topics



Leave a reply



Submit