0

I have a form inside a while loop in PHP. There is action="javascript:block_grade($id)", but when i alert(id) or alert(lvl) in the block_grade function, I receive the same numbers from all the the loops?

This is inside my loop:

echo '<form action="javascript:block_grade('.$show['id'].')" method="post">';
echo 'Change blockgrade: ';
echo '<input type="text" name="lvl" size="1" value="'.$show['lvl'].'">';
echo '<input type="submit" value="Save">';
echo '</form>';

This is my function at bottom:

<script>
function block_grade ( id ) {
var lvl = $('input[name=lvl]').val();
alert(lvl);
alert(id);
}
</script>

How can I solve this, and is there any more effective way than this, maybe?

Johnson
  • 818
  • 4
  • 21
  • 39
  • possible duplicate of [javascript: Using the current for-loop counter-value inside a function() { }?](http://stackoverflow.com/questions/3448195/javascript-using-the-current-for-loop-counter-value-inside-a-function) –  Nov 12 '10 at 22:37
  • @pst I don't think it relates to closures. More like he expects the jQuery to magically know which element he requires. – alex Nov 12 '10 at 22:41
  • He passes `id` as a parameter to `block_grade()`. No magic involved. – AndreKR Nov 12 '10 at 22:43

2 Answers2

3

I'd prefer to see JavaScript attach the event handler directly and then you can easily do things relative to the form.

echo '<input type="text" name="lvl" size="1" value="'.$show['lvl'].'">';
echo '<input type="hidden" name="id" size="1" value="'.$show['id'].'">';

Then:

$('form').submit(function() {
    var id = $(this).find('input[name=id]').val();
    var lvl = $(this).find('input[name=lvl]').val(); 
});

Among other advantages, this frees up the action property so you can fill in a script that can directly process this request without JavaScript if necessary.

But more to the point (of your original question), it ensures that you're looking at the correct lvl field, and not just the first lvl field jQuery happens to come across in the DOM.

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • Should i have my JS inside the loop? Else im still getting these same results? Or is there a way.. – Johnson Nov 12 '10 at 22:43
  • No, that JavaScript is self-sufficient. You do not need any other JavaScript anywhere on the page to make that work. If you're getting the same results with this setup, something's wrong with your PHP — it's outputting the same values for `lvl` and `id` for every form. – VoteyDisciple Nov 12 '10 at 22:45
  • Just saw in the html output (view source) It shows the right values for id and lvl in the value="" there. – Johnson Nov 12 '10 at 22:48
  • Then either your markup is screwy somewhere or you have an entirely separate piece of JavaScript interfering. My best guess: you've got a `
    ` tag wrapped around the entire collection of forms. The HTML validator should catch that.
    – VoteyDisciple Nov 12 '10 at 22:51
0

I didn't even know that it's possible to set the action of a form to "javascript: ...". I always use the onsubmit event.

Anyway, for lvl it is no wonder you get always the same because val() gives the value of the first element in the set. The set contains all the inputs in the whole page.

However, for id you should get the correct value. Maybe you should show the final HTML that is generated.

AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • the final html (source) is giving me right results.. I believe its because the JS is outside the loop? – Johnson Nov 12 '10 at 22:45
  • No, that's ok. If you are calling it like `block_grade(1)`, `block_grade(2)`, `block_grade(3)` it should alert "1", "2" and then "3" for `id`. – AndreKR Nov 12 '10 at 22:48
  • Okay, I still can't get any other values out from the other elements – Johnson Nov 12 '10 at 22:49