1

I'm trying to parse an html table and use the column values to form a mysql query. I'm trying to test to see if my variables are working correctly, but I'm not getting the first alert to pop up. I know that the function is executing because the second alert displays, but I don't know what is wrong with my variables.

Also, how do I get the selected value from the select box?

Here is my function:

function rows()
{
    $("#ingredients tr").each(function() {
            var ingr= $(this).find("td").eq(2).html();
            var prep= $(this).find("td").eq(3).html();
            var quant= $(this).find("td").eq(4).html();
            alert(prep);
    });
    alert("something");
}

Here is my table:

<TABLE name="ingredients" id="dataTable" width="350px" align="center" border="0">
<TR>
<td></td>
<td>Ingredient</td>
<td>Preparation</td>
<td>Amount</td>
</tr>
<tr>
<td><input type="checkbox" name="chk"/></td>
<TD>
<SELECT name="ingredient">
<?php
$db = new mysqli('localhost','root','mellon','recipes');
if($db->connect_errno >0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$query="select * from ingredients;";
if(!$results= $db->query($query)){
    die('There was an error running the query [' . $db->error . ']');
}

while($row = $results->fetch_assoc()){
    echo "<option value=\'" . $row['name'] . ">" . $row['name'] . " (" . $row['units'] . ")" . "</option>";
}
$results->free();
$db->close();

?>
</SELECT>
</TD>
<TD><INPUT type="text" name="prep"/></TD>
<TD><INPUT type="text" name="quantity"/></TD>
</TR>
</TABLE>
1
  • What errors are you getting? Are you including jQuery? Commented Nov 18, 2013 at 19:55

1 Answer 1

1

You are using the wrong jQuery selector. You should use:

$("#dataTable tr")

dataTable is the id not ingredients

You could also class your <td> for easier (more readible) access. For example you could add classes: ingredient, prep, quantity to the appropriate columns and access like this in your loop:

var ingr = $(this).find('.ingredient').html();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.