I have a html form where i append few elements like, when user select multiple countries from list jquery run through the selected lists and create a set of form fields for each country.
Assume user selected USA, China and India, then form fields will be created for each of this countries giving id asn name as *countryName_indexValue, customeName_countryName_indexValue.*
My question:
I need to submit this appended elements values to mysql database through php when user fill and hits submit button. How do i do it? Please help me……
And the result is this
<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>
Thank you..
So what’s the issue? Are you running into issues when you submit the data? Or are you struggling to handle the $_POST on the PHP side?
If it’s handling the $_POST, I’d suggest changing your generated form fields to have ids of the form
userCountry[countryName][indexValue]instead ofuserCountry_countryName_indexValue.That way, when the form is submitted, the $_POST will consist of arrays, which you can then loop over to write to a DB, e.g. the $_POST would have something like:
Then you can loop over this, e.g.:
Your current implementation could work with loops as well, as long as you loop over the whole $_POST object, checking each key, and looking for particular sub-strings, exploding on underscores, and then handling, but it’d be kind of ugly.