1

なぜこれが機能しないのですか?

// I'm sending this data: $_POST['amount_1'] = '3';
$val = '1'; // This is coming from a foreach loop, but I'm specifying here for simplicity
echo $_POST['amount_'.$val]; // Returns null
echo $_POST['amount_1']; // Returns 3

POST 値を取得しようとするときに変数は許可されていませんか?

編集:リクエストごとにより多くのコードを投稿します。

$outgoing = array();

foreach($_POST as $key => $val):
    if(strpos($key,'_number_')){ // Matching 'item_number_' would return false because of 0-indexing
        $item_id = $val;
        $test = str_replace('item_number_',$key); // Realized when pasting this that I didn't have a $replacement defined.
        $quantity = $_POST['quantity_'.$test];

        $name = $_POST['lp-name'];
        $email = $_POST['lp-email'];
        $phone = $_POST['lp-phone'];

    array_push($outgoing,array($test,$item_id,$quantity,$name,$email,$phone));
    }
endforeach;

$json = json_encode($outgoing);

今すぐ動作します。$replacementstr_replace() に が定義されていませんでした。

4

1 に答える 1

0

これを行う代わりに、フォーム要素で配列表記を使用できます。

<form>
    <input name='amount[]' value='this is first'>
    <input name='amount[]' value='this is second'>
    <input name='amount[]' value='this is last'>
</form>

このフォームを送信すると、データが配列形式で送信されます。

echo $amount[0]; // this is first
echo $amount[1]; // this is second
echo $amount[2]; // this is last

幸運をお祈りしています、

コルヴィン

于 2012-09-18T16:13:25.497 に答える