存储jquery附加值

时间:2012-08-29 16:29:48

标签: php jquery mysql forms

我有一个html表单,其中我添加了一些元素,例如,当用户从列表jquery中选择多个国家/地区时,运行所选列表并为每个国家/地区创建一组表单字段。

假设用户选择了美国,中国和印度,则会为每个国家/地区创建表单字段,其ID为as name 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>

谢谢..

2 个答案:

答案 0 :(得分:1)

那么问题是什么?您在提交数据时遇到问题吗?或者您是否正在努力处理PHP端的$ _​​POST?

如果它正在处理$ _POST,我建议您将生成的表单字段更改为userCountry[countryName][indexValue]而不是userCountry_countryName_indexValue格式的ID。

这样,当提交表单时,$ _POST将由数组组成,然后您可以循环写入数据库,例如$ _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对象,检查每个键,查找特定的子字符串,在下划线上爆炸,然后处理,但它是有点难看。

答案 1 :(得分:-1)

在表单提交事件或单击元素上您可以使用jQuery的.post()或.get()函数将输入值发送到php文件。

以下是一些关于如何使用.post()的基本和更复杂的例子: http://api.jquery.com/jQuery.post/

强烈建议php文件包含数据验证,之后你可以从那里发出带有php的sql插件。

相关问题