1

フォームにチェックボックスがあり、php フォーム プロセッサに投稿しています。私のフォーム プロセッサは、get 要求を Web サービスに送信します。リクエストには、この ?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" />

必要な出力を得るにはどうすれば修正できますか?

4

2 に答える 2

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

$url = str_replace(urlencode('services[]'), 'services', $url);
于 2012-11-02T16:04:26.680 に答える
0

サービスは配列であるため[]、各サービスを取得しています。そうでない場合service、GET の最後のものが他のすべてを置き換えます。

services各値が前の値を上書きするため、やりたい方法は機能しません。

Web サービスで、チェックされたすべての値のサービス配列をループしてみませんか?

詳細については、 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);
于 2012-11-01T18:25:19.783 に答える