josephChiaWY Posted March 7, 2008 Share Posted March 7, 2008 Hi all, I am a newbie in php. I have met a problem in echoing a few submit buttons using while loop. How do i differentiate which submit button is being click in order to grab the correct data. Below are a part of my codes: Code: ( php ) while($requestMatch = mysql_fetch_array($friendRequest)){ echo "<table width=425 height=130 border=0>"; echo "<tr>"; echo "<td width=110 class=style2>Add as Friend?? : </td>"; echo "<td width=55 align=left valign=middle>"; echo "<p>"; echo "<form action='".$_SERVER['PHP_SELF'.'QUERY_STRING']."' method='post'>"; echo "<a href='newFriendRequest.php?test="; echo $_SESSION['reqFriendEmail']; echo "'><input name=Submit type=submit value=YES style='width: 30px; height: 14px; font-family: Lucida Sans; font-size: 9px; color:#FFFFFF; background:#FF4700; font-style: normal; font-weight: normal; border:1px #FFFFFF; display: block; cursor:pointer; text-align: center;>"; echo "</form></p></td>"; echo "</tr>"; echo "</table>"; echo "<br>"; $_SESSION['reqFriendName'] = $requestMatch['username']; $_SESSION['reqFriendEmail'] = $requestEmail; } Note: i am doing a friend request page. The situation is when the user receives two requests and when the user click one of the "Yes" submit button. It takes the last request out of the friends request. Quote Link to comment https://forums.phpfreaks.com/topic/94855-how-to-differentiate-which-submit-button-user-clicks/ Share on other sites More sharing options...
Lumio Posted March 7, 2008 Share Posted March 7, 2008 <?php //maybe like that: if(isset($_POST['buttonname1'])) { echo 'result1'; }elseif (isset($_POST['buttonname2'])) { echo 'result2'; } ?> I can't see any buttons in your code. Also please write your code like <?php while($request/*something like that*/) { ?>some html code here <?php echo $_SERVER['PHP_SELF']; ?> some html... <?php } ?> And $_SERVER['PHP_SELF'.'QUERY_STRING'] is not right. You have to write $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']. If you do it yourway it calls $_SERVER[php_SELFQUERY_STRING]. That element does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/94855-how-to-differentiate-which-submit-button-user-clicks/#findComment-485872 Share on other sites More sharing options...
rameshfaj Posted March 7, 2008 Share Posted March 7, 2008 Yes the above one is the simplest one to be used.But this may not work when the buttons have same label or name. Quote Link to comment https://forums.phpfreaks.com/topic/94855-how-to-differentiate-which-submit-button-user-clicks/#findComment-485894 Share on other sites More sharing options...
laffin Posted March 7, 2008 Share Posted March 7, 2008 Either add a hidden input with the id for that user or change the submit name for that form instance. Quote Link to comment https://forums.phpfreaks.com/topic/94855-how-to-differentiate-which-submit-button-user-clicks/#findComment-485995 Share on other sites More sharing options...
deadonarrival Posted March 7, 2008 Share Posted March 7, 2008 Best way, IMO, would be to have two different submit buttons with different values. <input type="submit" name="submit" value="Yes" /> <input type="submit" name="submit" value="Go" /> then <?php switch ($_POST['submit']) { case "Yes" : //Code for if "Yes" button is pressed break; case "Go" : //Code for if "Go" button is pressed break; default: echo "No valid submit button pressed"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94855-how-to-differentiate-which-submit-button-user-clicks/#findComment-486006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.