javascript - Dynamically generated form submits only the last row ID of the mysql table -
i have dynamically generated form.it has 1 text input value id dynamically fetched database table (table_one). aim submit id table(table_two). on page can see these ids fetched table_one say: 1, 2, 3, 4 ,5, when submit of these ids '2', last id '5' submitted table_two. how can amend code when submit row 2, id '2' submitted not '5'? below code.
html
<form> <input type="text" id="name" name="name" value="<?php echo $name_id;?>" > <button type="submit" onclick="return chk()">edit</button> </form>
javascript
function chk(){ var name = document.getelementbyid('name').value; var datastring = 'name='+ name; $.ajax({ type:"post", url:"test.php", data:datastring, cache:false, success: function(html){ $('#msg').html(html); } }); return false; }
php
$name = $_post['name']; echo "$name";
you must add unique identifier or use dom. simple solution case is:
html
<input type="text" id="name-<?php echo $name_id;?>"> <button type="submit" onclick="return chk(<?php echo $name_id;?>)">edit</button>
javascript
function chk(name_id){ $.ajax({ type:"post", url:"test.php", data: { id: name_id, name: $('#name-'+name_id).val() }, cache:false, success: function(html){ $('#msg').html(html); } }); return false; }
and pass id of row want change. on server process both values $_post. hope helps.
btw don't need form element in ajax scenario.
Comments
Post a Comment