2012-03-13 12 views
-4

私は次のような入力フィールドを持つフォームを持っています:異なる入力名の

post_1a; post_1b; post_1c

post_2a; post_2a; post_2a

post_3a; post_3a; post_3a

<table> 
    <tbody> 
     <tr> 
      <td><input id="new_post_a" name="post_1a" type="text"></td> 
      <td><input id="new_post_b" name="post_1b" type="text"></td> 
      <td><input id="new_post_c" name="post_1c" type="text"></td> 
      <td><button id="add_row_btn">Toevoegen</button></td> 
     </tr> 
    </tbody> 
</table> 

私はポストをキャッチしたいのですが、このフィールドので、JavaScriptで生成され、私はするpreg_match /爆発するが、結果をキャッチしようとしましたが、私は、入力の値を必要とします。

javascriptのために入力名を変更できません。

私はもはやアイデアがないので誰かがアイデアを持っていますか?

+2

あなたは入力名を変更すべきです(SHOULD)。 [これ](http://stackoverflow.com/questions/9469860/random-number-of-form-fields-being-prepared-for-database/9​​469956#9469956)を明確にする必要があります。 – Crashspeeder

答えて

0

これはどう:

// start with post_1a 
$i = 1; 

// while post_{$i}a (and ...b and ...c) is set, do ... 
while (isset($_POST["post_".$i."a"], $_POST["post_".$i."b"], 
    $_POST["post_".$i."c"])) 
{ 
    // do something useful with the three values 
    var_dump(
     $_POST["post_".$i."a"], 
     $_POST["post_".$i."b"], 
     $_POST["post_".$i."c"] 
    ); 

    // increment $i for the next 3 inputs. 
    $i++; 
} 

あなたがクリーンソリューションのためのthis articleを参照して、入力の名前を変更することができます。

+0

はい、これは私が探していた解決策です!ありがとうございました!私はあなたが与えたリンクを見て、それを変更しようとします。 – David

+1

これがなぜ落とされたのか、私は嬉しいです。 – Basti

0

質問をPHPとしてタグ付けしたので、私はあなたがPHPファイルにフォームを投稿していると仮定します。

最初に、 "post_1a"、 "post_1b"などの入力フィールドに名前を付ける理由はわかりませんが、上記のBastiはすでに回答済みです。あなたは、単にそれらに配列を作る場合は、あなたがデータでやっていることに応じて、それがバックエンドで容易かもしれ、あなたが持っているでしょう:

<tr> 
    <td><input id="new_post_1a" name="post_1[]" type="text"></td> 
    <td><input id="new_post_1b" name="post_1[]" type="text"></td> 
    <td><input id="new_post_1c" name="post_1[]" type="text"></td> 
</tr> 
<tr> 
    <td><input id="new_post_2a" name="post_2[]" type="text"></td> 
    <td><input id="new_post_2b" name="post_2[]" type="text"></td> 
    <td><input id="new_post_2c" name="post_2[]" type="text"></td> 
</tr> 

を次にバックエンドで:

<?php 
$i = 1; 
$post_values = array(); 

while (array_key_exists("post_{$i}", $_POST)) { 
    $post_values = array_merge($post_values, $_POST['post_'.$i++]); 
} 
関連する問題