Reputation: 89
I am having a problem using submit button in loop.
What I am trying to do is to show a list from a table in a database and next to each row a submit button.
That insert into a table the id of that row my code is
for ($i = 0; $i <= count($all_rows)-1; $i++) {
<form>
<input type='text' id='name' value='".$all_tv_shows[$i]['id']."' />
<input type='button' value='Submit' onclick='addRecord()' />
</form>}
I am using AJAX with the submit buttons. so when i press on any submit button the value that inserted in the table is all the same value (the last row that showed)
Upvotes: 0
Views: 1373
Reputation: 711
I guess the problem is not in the code you have shown, the problem should be in addRecord javascript function on top of that id is same i.e. 'name' for all element....
You get the value from the form using $('#name').val();
or document.getElementById('name');
and look all input has same id. If I am going right then pass value in addRecord function and use that value.
Upvotes: -1
Reputation: 2455
Try this, close the php tags properly
<?php for ($i = 0; $i <= count($all_rows)-1; $i++) { ?>
<form>
<input type='text' id='name' value='".$all_tv_shows[$i]['id']."' />
<input type='button' value='Submit' onclick='addRecord()' />
</form>
<php } ?>
Upvotes: 4
Reputation: 33542
You need to close the PHP tags:
<?php
//assuming some other code first...
for ($i = 0; $i < count($all_rows); $i++) {
?>
<form>
<input type='text' id='name' value='<? php echo$all_tv_shows[$i]['id'];?>' />
<input type='button' value='Submit' onclick='addRecord()' />
</form>
<?php
}
Also, it's not hugely important, but it might be easier to read if you change the for
loop as above.
Upvotes: 3