PHP Buttons inside loop -
i have problem , don't know how sove it.i have inventory table contains id (that assign user)column , id_item column (that assign item items table) , items table contains id table.
more database contains: items table:
id name 1 dagger 2 staff 3 wood shield each unique id.
inventory table:
id id_item username name 1 3 cristi wood shield 2 1 motoc dagger 2 2 motoc staff the id every user id , id_item item's id items table.
problem: let's i'm logged in motoc has 2 weapons in inventory. til fine. want make button every item has. buttons there not working properly. when click first 1 shows me ssss1 correct when press second 1 nothing hapens. want show me ssss2 more next $row1['id_item'].
i don't know how solve this. thank you.
this i've tried:
if (isset($_session['id'])) { $sth1 = $dbh->prepare("select * inventory id = ".$_session['id'].""); $sth1->execute(); while($row1 = $sth1->fetch(pdo::fetch_assoc)){ $sth = $dbh->prepare("select * items id = ".$row1['id_item'].""); $sth->execute(); $row = $sth->fetch(pdo::fetch_assoc); $ss = print $row1["id_item"]; ?> <form id='<?php echo $row1["id_item"]; ?>' method="post" action="" > <input type="hidden" name="refid" value="add" /> <input type="submit" name="submit<?php echo $row1["id_item"]; ?>" value="add" /> </form> <?php } if (isset($_post["submit$ss"])) { $refid = intval($_post["refid"]); $sth1 = $dbh->prepare("select * inventory id = ".$_session['id'].""); $sth1->execute(); $row1 = $sth1->fetch(pdo::fetch_assoc); echo "ssss".$row1['id_item']; } }
this bad way of building form. since you're building "personalized" form every item, there's no need create dynamic field names, hidden form field:
<form ... > <input type="hidden" name="id_item" value="<?php echo $row1['id_item'] ?>" /> <input type="hidden" name="refid" value="add" /> <input type="submit" name="submit" value="add" /> </form> then check $_post['id_item'] in form handling code, instead of having every single possible submit1, submit2, etc...
as well, form handling code running within same context form generation code, before form has had chance displayed , user click. should @ least have somethign like
if ($_server['request_method'] == 'post') { ... handle form here ... echo "ssss..."; } so item info retrieval runs when form has been submitted.
Comments
Post a Comment