2

I have a dynamically obtained Listview with a H3 and 2 P tags. As in:

<li>
  <h3>Product no.4</h3>
  <p><strong>Product description</strong></p>
  <p>_price_</p>
  <p>_quant_</p>
  <p class="ui-li-aside"><strong>productID</strong></p>
</li>

The listview may contain 1 items (li) or 10-15 items. I cannot know from the beginning. I also have a button on my page that Is destined to save the contents of the listview to a database. I actually need the productID, quant and price TAG contents from all the list items in order to be able to save the item properly in my database. How can I get each list item's inner tags so I can post them to a PHP that will do the work?

Is there a way to send all list items in one post so there would be no lost items?

I assume I should have the listview inside a form and the button onclik should be something like:

$("#saveinv").click(function() { //When save button is clicked...
       var optionTexts = [];
       $("#listaproduse li").each(function() { optionTexts.push($(this).text()) });
       var text = '"' + optionTexts.join('"<br/> "') + '"';
       ... some post function here...
});       

But I have no idea how to post a form containing dynamic elements from jquery.

Any help would be appreciated

user1137313
  • 2,390
  • 9
  • 44
  • 91

1 Answers1

3

Working example: http://jsfiddle.net/ZmtW3/

What you want to do is create an array of objects that will be saved into a database. Each loop will go through a listview and parse only needed data. Every listview element is going to be stored into a single object, and that object will be pushed into an array.

Javascript used:

$(document).on('pagebeforeshow', '#index', function(){ 
    $(document).on('click', '#save-list', function(){ 
        var listview_array = new Array();
        $( "#list li" ).each(function( index ) {
            listview_el = new Object();
            listview_el.id=$(this).find('h3').text()
            listview_el.price=$(this).find('p').eq(2).text();
            listview_el.quantity=$(this).find('p').eq(3).text();
            listview_array.push(listview_el) 
        });
        var stringifyObject = JSON.stringify(listview_array);
        alert(stringifyObject);
    });
});

This example is create on a basis of your listview example so you can use it directly.

When listview is parsed we will stringify an array and send that string to server side. Form is not needed but you can use it, here's my older answer on how to properly submit a form when working with jQuery Mobile. Basically you will use AJAX to communicate with a server.

Whey you read that data back from a database you will need to use JSON.parse() function to create an object from a stringified text. After that just loop through an array and recreate a listview.

Take a look at my other ARTICLE to find how a listview can be created dynamically.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • Your answer is perfect. It shows that you are very experienced in this area. However, I must ask you one more thing: **How do I access the

    content in order to add it to the JSON ?** I tried to do it like listview_el.prodid=$(this).find('p').eq(4).text(); but it does not seem to work like that.

    – user1137313 Jul 11 '13 at 08:48
  • Here's an example: http://jsfiddle.net/ZmtW3/4/ , you need to use this line: $(this).find('p.ui-li-aside strong').text() – Gajotres Jul 11 '13 at 08:54
  • I do not seem to be able to POST the stringifyObject data to a php file. Is there a catch somewhere? After the alert (stringifyObject); I try the code: `$.ajax ({ type: "POST", url: 'processing.php', dataType: 'json', async: false, data: stringifyObject, success: function () { alert("Done processing!"); } });` but my page wont load completely so I assume there is something wrong with my above syntax Can you help me? – user1137313 Jul 11 '13 at 11:04
  • The processing.php file does only this: var_dump($_POST); and the response I get from $.ajax is on error side. In javascript console the response of the php file is: array(0) { } As if the json did not get to my php file... or what am I doing wrong? – user1137313 Jul 11 '13 at 11:15
  • Mail me your project and I will take a look. – Gajotres Jul 11 '13 at 11:45
  • Sorry I commented before I refreshed the page and saw your addition. The code works like a charm. **Thank you very much** – user1137313 Jul 11 '13 at 14:26