简体   繁体   中英

How to Print a variable in a function in PHP

I have a form, with post="ModelSelector", when submitted, we go through these codes. Issue I'm facing is, I want to check the value of $_POST, I know it is getting set by calling "isset()".

I just want to Print/alert/pushout the variable $productselection

function selectProduct() {       
    // save the post in a variable
    $ProductSelections =  $_POST['ModelSelector'];

    // I want to print $ProductSelection to check its value
    $frmVars['ProductSelections'] = $ProductSelections;
    $frmVars['WindowSize']        = $WindowSize;
    $frmVars['PageNum']  = 1;
    saveFormValues(0,'RunDefMgr', $frmVars);

    // Clear the checkboxes         
    $sel = array();
    deleteRunDef(0,"*","RUN_DEF_EDIT","*");
}


if(isset($_POST['ModelSelector'])) {
    selectProduct();
} 

I have tried ECHO, for some reason it is not printing the value in HTML. Thanks in advance.

I want to check the value of $_POST

$_POST will be an array.

Use print_r($_POST) or var_dump($_POST) to view its contents.

Your form method should be method="POST" , You can use the following edit to see if it works , as you have to pass $_POST (array) to the function to use it inside function. function expects an parameter else , $_POST does not exists.

And also enable the errors inside your file to check which type of errors you are getting by using : ini_set('display_errors',1); or error_reporting(E_ALL);

function selectProduct($_POST) { // create parameter $_POST which we get from isset condition.
    // save the post in a variable
    $ProductSelections =  $_POST['ModelSelector'];
    print_r($ProductSelections); // print the value.
    // I want to print $ProductSelection to check its value

    $frmVars['ProductSelections'] = $ProductSelections;
    $frmVars['WindowSize']        = $WindowSize;
    $frmVars['PageNum']  = 1;
    saveFormValues(0,'RunDefMgr', $frmVars);

    // Clear the checkboxes         
    $sel = array();
    deleteRunDef(0,"*","RUN_DEF_EDIT","*");
}

if(isset($_POST['ModelSelector'])) {
    selectProduct($_POST); // pass the $_POST array to the selectProduct function.
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM