Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a $_POST value that is returned, based on its value

Tags:

php

When I print_r it returns an array of data.

print_r($_POST);

My input fields are standard address information and a patient id:

Array ( [patient_id] => this [Last] => that [First] => Ted [ADDRESS1] => dey
[DOB] => [email] => [insurance_id] => [Phone] => [State] => CA [Zip] => 91236
[Status] => 3 [select_top] => 17 )

For status it's usually going to be entered as a 1, 2, or 3.

  • 1 equals Lead
  • 2 equals Active
  • 3 equals Inactive

How can I change the POST value so when a 3 is entered it's returned as Inactive or 2 returned as Active?

Is it possible to do a foreach loop and just change the value to an alias or something?

foreach($_POST as $key => $val){
    if($key == '3') $val = Inactive;
}

Any help would be greatly appreciated.

like image 239
Head Way Avatar asked Nov 23 '25 03:11

Head Way


2 Answers

You can use a switch statement. There's no need to use a foreach loop:

switch($_POST['status']) {
    case 1: $_POST['status'] = 'Lead'; break;
    case 2: $_POST['status'] = 'Active'; break;
    case 3: $_POST['status'] = 'Inactive'; break;
    default: $_POST['status'] = 'Huh?';
}
like image 72
Danny Beckett Avatar answered Nov 25 '25 17:11

Danny Beckett


Rather than manipulate the $_POST super global, I'd rather look up the value from a known set of valid entries. For example

$statusValues = array(
    1 => 'Lead',
    2 => 'Active',
    3 => 'Inactive'
);

if (!array_key_exists($_POST['status'], $statusValues)) {
    throw new UnexpectedValueException($_POST['status']);
}
$status = $statusValues[$_POST['status']];
like image 25
Phil Avatar answered Nov 25 '25 17:11

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!