PHP isset not working properly on form, with loops and arrays

时间秒杀一切 提交于 2020-02-08 10:14:05

问题


This is honestly the most finicky and inept language I've ever coded in. I'll be glad when this project is good and over with.

In any case I have to us PHP so here's my question.

I have an Array named $form_data as such:

$form_data = array 
('trav_emer_med_insur',
'trav_emer_single',
'trav_emer_single_date_go',
'trav_emer_single_date_ba',

'trav_emer_annual',
'trav_emer_annual_date_go',

'trav_emer_extend',
'trav_emer_extend_date_go',
'trav_emer_extend_date_ef',
'trav_emer_extend_date_ba',


'allinc_insur',

'allinc_insur_opt1',
'allinc_single_date_go',
'allinc_single_date_ba',

'allinc_insur_opt2',
'allinc_annual_date_go',
'allinc_annual_date_ba',



'cancel_insur',
'allinc_annual_date_go', 
'allinc_annual_date_ba',



'visitor_insur', 
'country_select',

'visitor_supervisa', 
'visitor_supervisa_date_go',
'visitor_supervisa_date_ba',

'visitor_student',
'visitor_student_date_go',
'visitor_student_date_ba',

'visitor_xpat',
'visitor_xpat_date_go',
'visitor_xpat_date_ba',

'txtApp1Name',
'txtApp2Name',
'txtApp1DOB',
'txtApp2DOB',
'txtApp1Add',
'txtApp1City',
'selprov',
'txtApp1Postal',
'txtApp1Phone',
'txtApp1Ext',
'txtApp1Email',
'conpref', );

These are the names of name="" fields on an HTML form. I have verified that ALL names exist and have a default value of '' using var_dump($_POST).

What I want to do is very simple, using the $form_data as reference do this:

  • create a new array called $out_data which can handle the data to display on a regurgitated form.

The structure of $out_data is simple the key will be the name of the element from the other array $out_data[txtApp1Name] for example, and then the value of that key will be the value.

  • Now what I want is to first check to see if every name="" is set or not, to eliminate errors and verify the data. Then regardless of whether it is set or not, create its placeholder in the $out_data array.

  • So if $_POST[$form_data[1]] (name is 'trav_emer_single') is not set create an entry in $out_data that looks like this $out_data([trav_emer_single] => "NO DATA")

  • If $_POST[$form_data[1]] (name is 'trav_emer_single') is set create and entry in $out_data that looks like this: $out_data([trav_emer_single] => "whatever the user typed in")

I have tried this code:

$out_data = array();
$count = count($form_data); 
for( $i = 0; $i < $count; $i++ ) 
{
    if(!isset($_POST[$form_data[$i]])) {
        $out_data[$form_data[$i]] = "NO_DATA";
        }
    else {
        $out_data[$form_data[$i]] = $_POST[$form_data[$i]];
        }
}

Now this code technically is working, it is going through the array and assigning values, but it is not doing so properly.

I have hit submit on the form with NOTHING entered. Therefore every item should say "NO_DATA" on my regurgitated output (for user review), however only some items are saying it. All items I have confirmed have name="" and match the array, and have nothing entered in them. Why is "NO_DATA" not being assigned to every item in the array?

Also of note, if I fill in the form completely $out_data is fully and correctly populated. What is the problem with !isset? I've tried doing $_POST[$form_data[$i]] == '' which does put no_data in every instance of no data, however it throws an 'undefined index' warning for every single item on the page whether I write something in the box or not.

Really I just want to know WTF is going on, the dead line for this project is closing fast and EVERY step of the PHP gives me grief.

As far as I can tell by reading around my code is valid, but refuses to execute as advertised.

If you need more code samples please ask.

Really befuddled here, nothing works without an error, help please.

Thanks -Sean


回答1:


Instead of checking !isset(), use empty(). If the form posts an empty string, it will still show up in the $_POST as an empty string, and isset() would return TRUE.

I've replaced your incremental for loop with a foreach loop, which is almost always used in PHP for iterating an array.

$out_data = array();
foreach ($form_data as $key) {
    if(empty($_POST[$key])) {
        $out_data[$key] = "NO_DATA";
    }
    else {
        $out_data[$key] = $_POST[$key];
    }
}



回答2:


PHP's isset returns TRUE unless the variable is undefined or it is NULL. The empty string "" does not cause it to return FALSE. empty() will do exactly what you need, though.

http://php.net/manual/en/function.isset.php

isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.



来源:https://stackoverflow.com/questions/12524502/php-isset-not-working-properly-on-form-with-loops-and-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!