问题
I have page which user can add date range from, to and price. By default there is three, but user can add more ranges using the add date range button button. My problem is, after adding a few extra date ranges when I get the $_POST
value it is not showing the appended html element's post values.
javascript part
var n = 0;
$(document).ready(function () {
$("#btn2").click(function () {
$("#add_drange").append(" <p><input type='text' name='from" + n + "' class='datepicker' placeholder='From' /> <input type='text' name='to" + n + "' class='datepicker' placeholder='To' /> <input type='text' name='price" + n + "'id='price' placeholder='Price in USD' /></p>");
n = n + $("#add_drange").length;
//c = c + n;
//alert(n);
if (n == 25) {
alert('You can only add 25 Date Ranges');
event.preventDefault();
$("#btn2").hide();
}
$(".datepicker").datepicker({
dateFormat: 'yy-mm-dd'
}).val();
});
});
HTML
<div id="add_drange">
<br>
<h1>Add Price for Date Ranges</h1>
<br>
<form action="" method="post" name="add_price">
<p>
<input type="text" name="from_f_1" class="datepicker" placeholder="From" />
<input type="text" name="to_f_1" class="datepicker" placeholder="To" />
<input type="text" name="price_f_1" id="price" placeholder="Price in USD" />
</p>
<p>
<input type="text" name="from_f_2" class="datepicker" placeholder="From" />
<input type="text" name="to_f_2" class="datepicker" placeholder="To" />
<input type="text" name="price_f_2" id="price" placeholder="Price in USD" />
</p>
<p>
<input type="text" name="from_f_3" class="datepicker" placeholder="From" />
<input type="text" name="to_f_3" class="datepicker" placeholder="To" />
<input type="text" name="price_f_3" id="price" placeholder="Price in USD" />
</p>
</div>
<br>
<input type="button" id="btn2" name="add_more" value="AddMore Data Ranges" />
<br>
<br>
<br>
<br>
<h1>Alter you Base Price for Each Day of the Week</h1>
<br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quam velit, vulputate eu pharetra nec, mattis ac neque. Duis vulputate commodo lectus, ac blandit elit tincidunt id. Sed rhoncus, tortor sed eleifend tristique, tortor mauris molestie elit, et lacinia ipsum quam nec dui.</p>
<br>
<input type="text" id="" name="sunday" placeholder="Sunday" size="8" />
<input type="text" id="" name="monday" placeholder="Monday" size="8" />
<input type="text" id="" name="tuesday" placeholder="Tuesday" size="8" />
<input type="text" id="" name="wednesday" placeholder="Wednesday" size="8" />
<input type="text" name="Thursday" id="" placeholder="Thursday" size="8" />
<input type="text" name="Friday" id="" placeholder="Friday" size="8" />
<input type="text" name="Saturday" id="" placeholder="Saturday" size="8" />
<br>
<br>
<input type="submit" id="save" name="save_all" value="save" style="float: right;" />
</form>
PHP
///dynamic created text feild part
$dynamic_feild;
for($n=0;$n<25;$n++)
{
$dynamic_feild[$n][0]=$_POST['from'."$n"];
$dynamic_feild[$n][1]=$_POST['to'."$n"];
$dynamic_feild[$n][2]=$_POST['price'."$n"];
}
var_dump($dynamic_feild);
回答1:
Check your HTML for improper nesting.
Specifically, look at the <div id="add_drange">
and <form>
tags.
It looks like it's not properly nested... more like this:
<div id="add_drange">
<form>
</div>
</form>
Your browser will update that to something like this to make it valid:
<div id="add_drange">
<form>
</form>
</div>
And then when you append a new field to #add_drange
, it's actually outside of the form.
To fix it, either append directly to the <form>
tag, or move the #add_drange
div entirely inside the form.
回答2:
$("#add_drange form").append(" <p><input type='text' name='from" + n + "' class='datepicker' placeholder='From' /> <input type='text' name='to" + n + "' class='datepicker' placeholder='To' /> <input type='text' name='price" + n + "'id='price' placeholder='Price in USD' /></p>");
try this for appending
回答3:
I think instead of using n = n + $("#add_drange").length; in your jquery you can use
n = parseInt(jQuery('#index').val()) + 1;
Keep a hidden input box with initial value 4. Because you have already 3 element for ranges.
<input type="hidden" name="index" id="index" value="4" />
It can be use to find the number of element you have use during POST.
来源:https://stackoverflow.com/questions/16976055/php-post-not-getting-the-html-input-element-value-which-append-by-jquery