0

I am a newbie in Jquery. First of all I would like to say that I have researched almost a day regarding this query. TRied numberous methods, but none that works. I am trying to code a billing module with HTML, PHP and Jquery. Until now, I have successfully created the HTML tables. I would like the data from these tables to the retrieved to a PHP page, so that I can print them with a proper formatting.

The HTML Table is as follows.

<table id="items">
    <tbody>

      <tr>
          <th>SlNo</th>
          <th>Item</th>
          <th>Unit Cost</th>
          <th>Quantity</th>
          <th>Price</th>
      </tr>

 <tr class="item-row">
         <td class="item-name">
           <div class="delete-wpr">
          <input type="text" class="slno"/>
           <a class="delete" href="javascript:;" title="Remove row">X</a>
           </div>
         </td>

           <td><input type="text" class="slno"/></td>           

           <td><input type="text" class="cost"/></td>
           <td><input type="text" class="qty"/></td>
           <td><span class="price"></span></td>
      </tr>  



    <input type="button" value="submit" onClick="storeAndShowTableValues()"/>


    <tbody>

Now, the Jquery is as follows.

     var TableData = new Array();
     $('#items tr').each(function(row, tr){

     TableData[row]={
        "ItemNum" : $(tr).find('td:eq(0)').text(), 
        "Itemname" :$(tr).find('td:eq(1)').text(),
         "unitprice" : $(tr).find('td:eq(2)').text(),
         "Qty" : $(tr).find('td:eq(3)').text(),
        "price" : $(tr).find('td:eq(4)').text()
    }
}
); 
TableData.shift();  // first row is the table header - so remove

var TableData = JSON.stringify(TableData);

But the JSON object I am getting is garbage. The table data is not retrieved. Please help.

3 Answers 3

4

To get the value of an input field in jQuery you need to use the .val() function.

Also make sure that you select the input element within your td tag

http://api.jquery.com/val/

So your code should look something like this:

var TableData = new Array();

    $('#items tr').each(function(row, tr){

    TableData[row]={
        "ItemNum" : $(tr).find('td:eq(0) input').val(),
        "Itemname" :$(tr).find('td:eq(1) input').val(),
         "unitprice" : $(tr).find('td:eq(2) input').val(),
         "Qty" : $(tr).find('td:eq(3) input').val(),
        "price" : $(tr).find('td:eq(4)').text()
    }
}
);
TableData.shift();  // first row is the table header - so remove

TableData = JSON.stringify(TableData);
Sign up to request clarification or add additional context in comments.

3 Comments

@AkhilKPAULOSE what are you trying to tell me?
Don't feel too bad. The OP and accepted answer both work for the same company. Google is not always your friend! +1:)
@TrueBlueAussie nothing to feel bad for :-).
1
var TableData = new Array();

$('#items tr').each(function(row){

TableData[row]={
     "ItemNum" : $(this).find('td:eq(0) input').val(), 
    "Itemname" :$(this).find('td:eq(1) input').val(),
     "unitprice" : $(this).find('td:eq(2) input').val(),
     "Qty" : $(this).find('td:eq(3) input').val(),
    "price" : $(this).find('td:eq(4) input').val(),
  }
  }
); 
 TableData.shift();  // first row is the table header - so remove

  var TableData = JSON.stringify(TableData);

1 Comment

Price is an span tag so you would use .text here and not .val
-2

Not to take much of your time, but I think you would need to get the val() instead of text() or html()

2 Comments

How does a table cell have a value!?
@epascarello: It is an input field inside the table cell, and input fields can have values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.