0

ユーザーがリストから複数の国を選択すると、選択したリストをjqueryが実行し、国ごとに一連のフォームフィールドを作成するなど、いくつかの要素を追加するhtmlフォームがあります。

ユーザーが米国、中国、インドを選択したと仮定すると、この国ごとにフォーム フィールドが作成され、id asn name が *countryName_indexValue, customeName_countryName_indexValue.* として与えられます。

私の質問: ユーザーが入力して送信ボタンを押したときに、この追加された要素の値を php を介して mysql データベースに送信する必要があります。どうすればいいのですか?私を助けてください......

で、結果がこれ

<form>

<table>
<thead>
<tr>
<td>Country Name</td>
<td>Data01</td>
<td>Data02</td>
</tr>
</thead>

<!-- APPENDED ELEMENTS -->

<tbody>
<tr id="usa_0">
<td><input type="text" name="userCountry_usa_0" id="userCountry_usa_0"></td>
<td><input type="text" name="countryData01_usa_0" id="countryData01_usa_0"></td>
<td><input type="text" name="countryData02_usa_0" id="countryData02_usa_0"></td>
</tr>

<tr id="china_1">
<td><input type="text" name="userCountry_china_1" id="userCountry_china_1"></td>
<td><input type="text" name="countryData01_china_1" id="countryData01_china_1"></td>
<td><input type="text" name="countryData02_china_1" id="countryData02_china_1"></td>
</tr>

<tr id="india_2">
<td><input type="text" name="userCountry_india_2" id="userCountry_india_2"></td>
<td><input type="text" name="countryData01_india_2" id="countryData01_india_2"></td>
<td><input type="text" name="countryData02_india_2" id="countryData02_india_2"></td>
</tr>
</tbody>

</table>

</form>

ありがとうございました..

4

2 に答える 2

1

それで、問題は何ですか?データを送信するときに問題が発生していますか? または、PHP 側で $_POST を処理するのに苦労していますか?

$_POST を処理している場合は、生成されたフォーム フィールドを .iduserCountry[countryName][indexValue]ではなくフォームの id を持つように変更することをお勧めしますuserCountry_countryName_indexValue

そうすれば、フォームが送信されると、$_POST は配列で構成され、これをループして DB に書き込むことができます。たとえば、$_POST は次のようになります。

$_POST['userCountry'] = array( 'usa' => array( '0' => 'someValue'),
                               'china' => array( '1' => 'someOtherValue'),
                               'india' => array( '2' => 'yetAnotherValue'),
                        )

次に、これをループできます。たとえば、次のようになります。

foreach($_POST['userCountry'] as $country=>$subArray) {
    //code here to insert data
}

$_POST オブジェクト全体をループし、各キーをチェックし、特定のサブストリングを探し、アンダースコアで展開してから処理する限り、現在の実装はループでも機能しますが、それはちょっと醜いでしょう.

于 2012-08-29T16:34:04.043 に答える
-1

フォーム送信イベント時、または要素クリック時 jQuery の .post() または .get() 関数を使用して、入力値を php ファイルに送信できます。

.post() の使用方法に関するいくつかの基本的な例とより複雑な例を次に示します: http://api.jquery.com/jQuery.post/

php ファイルにデータ検証を含めることを強くお勧めします。その後、そこから php を使用して sql insert を発行できます。

于 2012-08-29T16:43:49.190 に答える