Quick question...
I have an input box that upon key enter, adds the value to an unordered list. How would I pass this unordered list to jquery post? I'm using the form.serialize, so I don't have to define each variable for posting.
Example, let's say I entered 3 fruits and they get dynamically added to an unordered list inside the form.
Fruit: [input text here]
- Apple
- Banana
- Pear
I was thinking of creating a hidden text field that would populate with the new value everytime the user pressed enter.
Any other thoughts?
<ul>
<li>Apple<input type='hidden' name='fruits[]' value='Apple'/></li>
<li>Pear<input type='hidden' name='fruits[]' value='Pear'/></li>
<li>Banana<input type='hidden' name='fruits[]' value='Banana'/></li>
</ul>
When submitted, you'll get an array in $_POST
named fruits
with those value.
when you add thing dynamically to your UL you can add hidden inputs with array name like
<input type='hidden' name='ul[1]' value='banana'/>
<input type='hidden' name='ul[2]' value='apple'/>
<input type='hidden' name='ul[3]' value='pear'/>
Something like that (dynamic construction of the ul content):
<html>
<head>
<title>S.O. 3287336</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#add').click(function() {
var li = $('<li></li>').text($('#edit').val());
$('#ul').append(li);
});
$('#addForm').submit(function() {
var ul = "";
$('#ul li').each(function() {
ul += $(this).html() + ";"
});
alert("About to submit: " + ul);
return false; // don't submit
});
});
</script>
</head>
<body>
<ul id="ul">
</ul>
<form id="addForm">
<input type="text" name="edit" id="edit" value=""/>
<input type="button" id="add" value="Add"/>
<input type="submit" id="submit-btn" value="End"/>
</form>
</body>
</html>