how to differentiate which submit button user clicks???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • josephChiaWY
    New Member
    • Mar 2008
    • 2

    how to differentiate which submit button user clicks???

    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($requestM atch = mysql_fetch_arr ay($friendReque st)){

    echo "<table width=425 height=130 border=0>";
    echo "<tr>";
    echo "<td width=110 class=style2>Ad d as Friend?? : </td>";
    echo "<td width=55 align=left valign=middle>" ;
    echo "<p>";
    echo "<form action='".$_SER VER['PHP_SELF'.'QUE RY_STRING']."' method='post'>" ;
    echo "<a href='newFriend Request.php?tes t="; 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:#FF4 700; 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.
    [/code]
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    There is definitely an answer to this, I think by making the name attribute for the button unique. But I do things a much simpler way (I think it is simpler).

    I define a function in javascript something like this:

    function setToGo(v) {
    document.form1. buttonpressed.v alue = v;
    }

    and I have in the HTML on the page

    <input type="hidden" name="buttonpre ssed" value="">

    I then add an "onclick" to my submit buttons, sort of looking like this:

    echo "<input type=\"submit\" value=\"whateve r\" onclick=\"javas cript:setToGo(" . $index . ")\" />";

    where $index is a PHP variable that will be set to something different for each button that appears on the page.

    The script that receives the page after the user clicks on a button can then examine the variable $_POST['buttonpressed'] to find out which button was actually pressed.

    Again, this is only one way of doing it. I think by making the name attribute unique (i.e. catenate a loop index to the name) you can do it without javascript.

    Steve, Denmark

    Comment

    Working...