dongren9966 2012-12-11 21:30
浏览 30
已采纳

PHP $ _POST正在工作,但没有获得HTML元素的价值

TL;DR version

I have a big form with hundreds of html input elements on it, I made a php array to store all the element names in it. When I run my process.php file it runs through that array using a loop and creates a new array, the key of that array is the name of each element, and the value of that array is the value of each element. I know this code works as element values for text boxes, radio buttons, and drop-down selections work fine. Checkboxes do not have the values put into the array, instead the key and value read the same thing, the name of the element. Why is $_POST giving me the NAME or ID of my checkbox, and not its value?

Full explanation /w code samples:

The problem I am having seems rather unusual to me as from my understanding it goes against how $_POST should work.

In any case I have been developing a travel insurance website for the past year and it is nearing completion. The client wants me to create a version of the form which they can view after people have submitted applications and it will get the values out of the file they select.

My problem is that $_POST is not getting the value of my checkbox elements but rather their name or id (both are identical so I cannot be sure which it is getting). $_POST is successfully getting the value of all radio, text, and drop-down elements, just not checkboxes.

I have a very large array in my process.php file as the form is quite large. What I've done is create an array which has the name of each element I wish to access. Below is a sample of the structure the array follows, it is far to large to post the entire thing here (400 plus elements on form).

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

'trav_emer_annual',
'trav_emer_annual_date_go',
'trav_emer_annual_days',

'trav_emer_extend',
'trav_emer_extend_date_go',
'trav_emer_extend_date_ef',
'trav_emer_extend_date_ba',
'trav_emer_extend_days',
);

This is the code that runs on process.php to create the user data file, which is saved to a protected folder on the server.

// Create user output data
$out_data = array();
$count = count($form_data); 
for( $i = 0; $i < $count; $i++ ) 
{
    if(empty($_POST[$form_data[$i]])) {
        $out_data[$form_data[$i]] = " ";
        }
    else {
        $out_data[$form_data[$i]] = $_POST[$form_data[$i]];
        }
}

//Set variable names for new file
$dir = "/home/imelnick/public_html/getawayinsured.ca/userdata/";
$timestamp=date("YmdGis");
$name = $out_data['txtApp1Name'];
$value = str_replace(" ", "", $name);
$item = $value . "^" . $timestamp  . ".txt";
$filename = $dir . $item;

//Put data in file
//Open file for writing
$fileHandle = fopen($filename, 'w') or die("Can't open file");

//Write contents of out_data array to file
foreach ($out_data as $key => $value) {
$fileLine = $key . "\t" . $value . "
";
fwrite($fileHandle, $fileLine);
}

//Close file
fclose($fileHandle);

The first block of names in the array belong to checkboxes and they follow the format of the following:

<input type="checkbox" name="trav_emer_med_insur" id="trav_emer_med_insur_if"  value="YES" class="form_elements" onClick="if(this.checked){document.getElementById('trav_emer_med_options').style.display='block';}else{document.getElementById('trav_emer_med_options').style.display='none';}"/>

The onClick statement expands a div containing additional checkboxes which offer further options to the applicant.

My output data array which is created from the form data has the key of each item in the array as the name of the element, and the value of each item in the array the value of the corresponding HTML element.

Upon splitting the array and writing the $key/$value combination the expected value of $_POST[$form_data[0]] (note: $form_data[0] = trav_emer_med_insur) is the value of the above checkbox code, value="YES". However the output in the file reads as follows.

trav_emer_med_insur trav_emer_med_insur

I am quite sure that there is not a problem with the code that processes the form itself as other elements on the form have their values saved perfectly well to the file (radio buttons, text boxes, drop-downs all work). The Checkboxes do not, they refuse to $_POST the value of the HTML element, and simply keep putting out the name twice.

For example, another element in the form not listed in my array sample above named smoked_if is a radio button pair which asks if the applicant has smoked or not. Here is a sample output from a recent application.

As can be seen the desired result of my code is being performed.

smoked_if no

I am at a loss here because not only does this contradict the functionality of $_POST itself but since all other elements on the form have their values posted without issue it tells me there is a problem with checkbox elements and $_POST.

Any assistance is greatly appreciated.

  • 写回答

3条回答 默认 最新

  • duanjing3656 2012-12-11 21:50
    关注

    AS long as the information is inside of the Form tags, it will be passed to the server. With that said, you need to understand that what is sent to the server is the NAME and VALUE of the items.

    When you are looking at PHP, you want to submit, and then a string will look like: sample from http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox as well

    ?vehicle=car&vehicle=truck
    

    In PHP when you post it, the idea is the same but just hidden from the unskilled eyes. To get the values of it in my example:

    $vehicle= $_POST["vehicle"];
    echo ''.$vehicle;   //shows   ARRAY
    
    foreach( $item in $vehicle){
        echo ''.$item.'
    ';   //will iterate through all the vehicle and print them out
    }
    

    YOu can also say things like

    if(in_array("car", $vehicle)){
       echo "there exists a car
    ";
    }
    http://php.net/manual/en/function.in-array.php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来