如何从查询字符串中删除复选框键值?

时间:2012-11-01 18:17:16

标签: php html web-services forms

我在表单中有复选框,我发布到php表单处理器。我的表单处理器向Web服务发送get请求。该请求需要与每个复选框类似?services = Chin& services = Neck& services = Back& location = 5

没有键值,但是使用我的php代码,它在每个服务之后输出[]。

//build query string
$fields = array('services' => $services,
            'location' => $location,
            'firstname' => $firstname,
            'lastname' => $lastname,
            'email' => $email,
            'emailconfirm' => $email,
            'phone' => $telephone,
            'comments' => $message);

$url = "fakewebaddress?" . http_build_query($fields, '', "&");

//send email if all is ok
if($formok){

    $curl_handle = curl_init($url);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    $results = curl_exec($curl_handle);
    curl_close($curl_handle);

}

我的html框看起来像这样

<input type="checkbox" name="services[]" value="Leg" />
<input type="checkbox" name="services[]" value="Chest" />
<input type="checkbox" name="services[]" value="Neck" />
<input type="checkbox" name="services[]" value="Back" />

如何修复它以获得我需要的输出?

2 个答案:

答案 0 :(得分:0)

服务是一个数组,这就是为什么你在每个之后获得[]的原因。如果不是,那么GET中的最后一个service将替换所有其他的。{/ p>

您希望这样做的方式无效,因为每个services值都会覆盖前一个值。

为什么不在您的web服务中为所有检查的值循环遍历services数组?

有关详细信息,请参阅http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

修改

对于你想要的东西(不确定它是如何工作的,但它是你想要的)。

$url = str_replace('services[]', 'services', $url);

所以

//build query string
$fields = array('services' => $services,
            'location' => $location,
            'firstname' => $firstname,
            'lastname' => $lastname,
            'email' => $email,
            'emailconfirm' => $email,
            'phone' => $telephone,
            'comments' => $message);

$url = "fakewebaddress?" . http_build_query($fields, '', "&");
$url = str_replace('services[]', 'services', $url);
// or use this if the [] is encoded
$url = str_replace('services%5B%5D', 'services', $url);

答案 1 :(得分:0)

$url = "fakewebaddress?" . http_build_query($fields, '', "&");

$url = str_replace(urlencode('services[]'), 'services', $url);