1

Please excuse this is a dumb question as I am new to AJAX.

I have a form that is dynamically generated by PHP according to data from a mysql query

I can get the value but what I also need to know, is how to get the name or id of a form field (i.e. num[$id])

<input type='text' name='num[$id]' id='num[$id]' value='0' onchange = 'getAmt(this.value)'/>
hakre
  • 193,403
  • 52
  • 435
  • 836
user1667474
  • 819
  • 6
  • 24
  • 37
  • [Check your HTML, it is not valid](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html). Before anything AJAX, start with valid HTML first. Also you should show the code how you generate that HTML (there is a PHP variable in there, `$id`) because the answer to your question can be related to that. – hakre Oct 22 '12 at 12:50

3 Answers3

1

your html should be like this .
<input type='text' name='num[]' id='num_<?php echo $id; ?>' value='<?php echo echo $id; ?>' onchange = 'getAmt(this)'/>

in your js function getAmt

var id=this.id;
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
  • Thank you all for the responses - I think this is nearly it, but I see you've put the id in the value. I actually need the value the user types in but I also need the id from the array as that is the primary key and I need to process it - i.e. the value that is types in is the amount they are ordering, the script needs to find out the price according to the primary key. So would this.id return the id and this.value return the value? – user1667474 Oct 22 '12 at 12:50
1

HTML code:
<input type='text' name='num[$id]' id='num[$id]' value='0' onchange='getAmt(this)'/>

Script:
function getAmt(Obj){
var id = Obj.id;
var name = Obj.name;
}

Airful
  • 312
  • 2
  • 12
0
document.getElementById("MYID");
alert(x.name);
Svetoslav
  • 4,686
  • 2
  • 28
  • 43