PHP isset在带有循环和数组的表单上无法正常工作 [英] PHP isset not working properly on form, with loops and arrays

查看:63
本文介绍了PHP isset在带有循环和数组的表单上无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老实说,这是我编码过的最挑剔,最愚蠢的语言.当这个项目顺利完成后,我将感到非常高兴.

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.

无论如何,我必须使用PHP,所以这是我的问题.

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

我有一个名为$ form_data的数组,如下所示:

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', );

这些是HTML表单上的name ="字段的名称.我已经使用var_dump($ _ POST)验证了所有名称的默认名称为

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).

我想做的非常简单,使用$ form_data作为参考:

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

  • 创建一个名为$ out_data的新数组,该数组可以处理要在反吹表单上显示的数据.

$ out_data的结构很简单,例如,键将是另一个数组$ out_data [txtApp1Name]中元素的名称,然后该键的值将是该值.

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.

  • 现在,我要首先检查是否设置了每个name =",以消除错误并验证数据.然后,无论是否设置,都在$ out_data数组中创建其占位符.

  • 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.

因此,如果未设置$ _POST [$ form_data [1]](名称为"trav_emer_single"),则在$ out_data中创建一个看起来像这样的条目$ out_data([trav_emer_single] =>"NO DATA")

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")

如果设置了$ _POST [$ form_data [1]](名称为"trav_emer_single"),则在$ out_data中创建和输入如下所示:$ out_data([trav_emer_single] =>无论用户键入了什么)

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")

我尝试了以下代码:

$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.

我在表单上未输入任何内容就点击了提交".因此,每个项目在我的反流输出(供用户查看)上都应该说"NO_DATA",但是只有一些项目在说.我确认的所有项目都具有name ="并且与数组匹配,并且未在其中输入任何内容.为什么未将"NO_DATA"分配给数组中的每个项目?

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?

还要注意,如果我完全填写表格,则$ out_data会完全正确地填充. !isset有什么问题?我尝试做$ _POST [$ form_data [$ i]] ==''确实将no_data放在没有数据的每个实例中,但是无论我是否写东西,它都会为页面上的每个项目引发未定义索引"警告是否在框中.

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.

真的,我只是想知道WTF正在进行中,该项目的最后期限正在迅速关闭,PHP的每一步都使我感到悲伤.

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.

谢谢 -西恩(Sean)

Thanks -Sean

推荐答案

使用empty()代替检查!isset().如果表单发布了一个空字符串,它仍将在$_POST中显示为一个空字符串,并且isset()将返回TRUE.

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.

我已经将for增量循环替换为foreach循环,该循环几乎总是在PHP中用于迭代数组.

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];
    }
}

这篇关于PHP isset在带有循环和数组的表单上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆