0

This is my form that I am creating from a php loop and what I'm trying to get is when the image is clicked i want to get the form id or all of the inputs names and values so that I can update my database.

$result = mysql_query("SELECT * FROM `inventory` WHERE `active` = '1'");
    $i=1;
    while ($r = mysql_fetch_assoc($result)) {
        echo '<tr>
        <form id="'.$i++.'">
            <td style="width: 15%">'.$r['part_num'].'</td>
            <td style="width: 25%">'.$r['name'].'</td>
            <td style="width: 13%"><input type="text" name="sd_paint" value="'.$r['sd_paint'].'" class="inv_value"</td>
            <td style="width: 13%"><input type="text" name="sd_unpaint" value="'.$r['sd_unpaint'].'" class="inv_value"</td>
            <td style="width: 13%"><input type="text" name="ia_paint" value="'.$r['ia_paint'].'" class="inv_value"</td>
            <td style="width: 13%"><input type="text" name="ia_unpaint" value="'.$r['ia_unpaint'].'" class="inv_value"</td>
            <td style="width: 8%">
                <input type="image" src="pics/icons/edit.png" id="'.$r['inv_id'].'" class="submit_new_inv">
        </form>
                <img src="pics/icons/delete_inv.png" id="'.$r['inv_id'].'" class="edit_inv">
            </td>
        </tr>';
    }

And this is the javascript that Im using but this is just returning the first form:

$('.submit_new_inv').off().click(function(e) {
    e.preventDefault();
    var formID = document.forms[0].id;
    alert(formID);
});

sorry found the answer. You cannot wrap a form around table rows

2 Answers 2

1

Something similar to the following should work for you in the click event:

var myForm = $(this).closest('form');
alert(myForm.prop('id'));

You can find more information about closest at http://api.jquery.com/closest/.

Sign up to request clarification or add additional context in comments.

6 Comments

This is what is returning [object Object]
Now its just returning "undefined"
In the markup, do you see the ID actually set? Sorry, my PHP is rusty and I'm not equipped to debug here.
Yes, when i inspect the element it shows that the form id is incrementing by 1
If you check the underlying DOM element, var el = myForm.get(0), in the debugger, does the el variable have an ID?
|
1
$('.submit_new_inv').off().click(function(e) {
e.preventDefault();
var formID = $(this).parents("form").prop("id");
alert(formID);

});

3 Comments

This is always returning "undefined" instead of the id
which jquery version used? 1.6 below, if 1.6 below,use var formID = $(this).parents("form").attr("id");
version jquery-2.1.1.js

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.