tags:

views:

22

answers:

2

hi .. iam trying to add items to an ul element using jquery. the items will be added when a user click on Add new item : here is what i have :

function addFormField() {
            var id = document.getElementById("id").value;
            if(id<5){
            $("#elem ul").append('<li id="listItem_"'+ id +'>Item Added Nr.'+ id +'</li>');
            document.getElementById("id").value = id+1;
            }
        ...

        <div id="elem">
        <ul id="test-list">
        </ul>
        </div>
    <p><a href="#" onClick="addFormField(); return false;">Add New Item</a></p>

but when i submit the form, the ul elements are empty, that means i am getting no array where all the list items the user added are..

how can i get a list of the items added by user when they click on the Add button !?

any help is really appreciated, thanks a lot

A: 

Your code is very wrong and is some mixture of jQuery and plain JavaScript. Here is an example how should be done:

var id = 1;
$(".add_item").click(function() { 
    if(id<5){
        $("#elem ul").append('<li id="listItem_"'+ id +'>Item Added Nr.'+ id +'</li>');
        $.ajax({ url: "addItem.php", item: "listItem_"+ id});
        id++;
    }
});


<div id="elem">
    <ul id="test-list">
    </ul>
</div>
<p><a href="#" class="add_item">Add New Item</a></p>

Each time a user adds an item you should execute AJAX call to your PHP script, e.g. you can use $.ajax, $.post or $.get function, each time the PHP script is called you should save the received item in the database.

AJAX - http://api.jquery.com/jQuery.ajax/

Check the code again.

infinity
hi, thanks for the reply ..the problem isi want to submit them using a php form and get the new added items in the submitted form data ..i can add elements it is working well, but the php posting is not getting the elements i added to the list ..
jqueryanfänger
http://www.wil-linssen.com/demo/jquery-sortable-ajax/ here is a list with example, iam adding new items, but they wont appear in the array submitted with php form
jqueryanfänger
Use AJAX to update user records each time he adds a list item, or other way which is not very good approach is on each added item to create hidden input field, e.g. $("#form").append('<input type="hidden" name="item[]" value="'+id+'" />');
infinity
A: 

hi infinity, iam trying your code, but it is not working ..

this is what i have :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.1.custom.min.js"></script>
<link rel='stylesheet' href='styles.css' type='text/css' media='all' />
<script type="text/javascript">
var id = 1; 
$(".add_item").click(function() {  
    if(id<5){ 
        $("#elem ul").append('<li id="listItem_"'+ id +'>Item Added Nr.'+ id +'</li>'); 
        $.ajax({ url: "addItem.php", item: "listItem_"+ id}); 
        id++; 
    } 
}); 
</script>
</head>
<body>
<div id="elem"> 
    <ul id="test-list"> 
    </ul> 
</div> 
<p><a href="#" class="add_item">Add New Item</a></p> 
</body>
</html>

what am i doing wrong?

jqueryanfänger