I’m trying to allow a user to accept/decline requests for an event through submit buttons. Information is looped and displayed in a row (username, location, accept,decline).
Right now 2 users are being displayed; User 1 and User 2(current ones for testing). Anyway, I’m trying to get the correct userid to work with the correct username. Currently, regardless of which user I accept or decline, user 2 is displayed. I tried to set the value of the userid based on a hidden input but it’s not working correctly.
Here is my code.
for($i=0;$i<count($userExplode)-1;$i++){
$user = mysql_query("select userid,username from users where userid = ".$userExplode[$i]." ");
$user = mysql_fetch_array($user);
$userLoc = mysql_query("select userLocation from userinfo where userid = ".$userExplode[$i]." ");
$location = mysql_fetch_array($userLoc);
$locationExplode = explode("~",$location['userLocation']);
//the displayed output is working correctly so I know it's setting $user['userid'] properly
echo '<form method="post"><table><tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td>
<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>
<td><input type="hidden" name="userReq" value='.$user['userid'].'></td>
<td><input type="submit" name="accept" value="Accept Request" ></td>
<td><input type="submit" name="decline" value="Decline Request" ></td></tr>';
}
echo '</table></form>';
}
if(isset($_POST['accept'])){
echo $_POST['userReq']; //displays user 2 even if I click user 1
}
if(isset($_POST['decline'])){
echo $_POST['userReq']; //also displays user 2 even if i click user 1
}
}
There is a lot wrong with your code:
for‘s in the loopReplace the submit button’s by anchors and add the parameters in there. Then you can style the anchor to look like a button.
becomes
Remove all the
<form>s and load everything in 1 table.Result:
I think this is only the tip of the iceberg.. How do you get
$userExplodefor example? It is very weird and illogical. I assume that you first run a query to get all the users and then loop with this for?