views:

20

answers:

2

Hi

I am relatively new to Javascript and have a problem i hope your guys can help

What i am trying to do is this

$("tr:not(:first-child)").each(function(){
                            var sitename = $("this" + "td:nth-child(1)").text();
                            var type= $("this" + "td:nth-child(2)").text();
                            var address= $("this" + "td:nth-child(3)").text();
                            var city= $("this" + "td:nth-child(4)").text();
                            var lat= $("this" + "td:nth-child(5)").text();
                            var long= $("this" + "td:nth-child(6)").text();
                            var ftime= $("this" + "td:nth-child(7)").text();
                            $.post('./watchdog.php',{ oadata: sitename});
                    }).end().remove();

I am selecting the "tr" in a a table except the first one which contains "th". Then i am just stripping the td and assigning the text value to variable. in the end i am posting this data to a php script that basically inserts this a in a Mysql DB. the problem is i keep getting NULL value in the database.

Here is my Table as generated in the HTML

Site Name   Type    Address City    Lat Long    Fault Time
ISB056  Spur    Opposite Plot No. 435, Sector I-10/4    Islamabad   73.04393    33.64677    12:02 PM
ISB078  Spur    SEECS NUST H-12 Islamabad   Islamabad   72.990044   33.64246    12:02 PM
A: 

This

$("this" + "td:nth-child(1)").text()

generates the selector thistd:nth-child(1) which does not make sense at all. I think you want:

$(this).find("td:nth-child(1)").text()

But easier would be to just take an array with e.g.

var data = $(this).children('td').map(function() {
                 return $(this).text();
           }).get();

and then send the data like

$.post('./watchdog.php',{ sitename: data[0], type: data[1],...});
Felix Kling
@Felix KlingThanks the map function worked like a charm and reduced the code size as well...
Sufi Hani
A: 

You musn't use 'this' but this so remove your quotes.

You also should use classes.

<tr class="site">
   <td class="sitename">...</td>
   ...
</tr>

That will make your code easier to read and to debug:

$("tr.site").each(function(){
    var sitename = $(".sitename",this)
})
Ghommey