更改代表提取行ID的按钮名称

时间:2018-07-22 00:35:33

标签: javascript php html css

早上好,我有一个问题。我应该为我的大学做一个项目。对于这个项目,我从另一个表中获取信息,并在每一行中放置按钮。用户单击按钮后,应更改为已发送请求。问题是我创建了简单的javascript代码。该代码仅更改第一行ID的按钮,而不会影响其他ID。另一件事是,插入php函数也适用于第一行ID。在我应用javascript之前,我的插入函数会正确运行。我希望亲人的成长能认出我的错。

php

<?php
if(mysqli_num_rows($result)>0)
{

    while($row = mysqli_fetch_array($result))
{      

      $register_ID=$row["register_ID"];
        $username = $row['username'];
        $profile = $row['profile'];
        $email = $row['email'];
        $address=$row['address'];
        $gender=$row['gender'];
         $main_subject=$row["main_subject"];
         $subject_add=$row["subject_add"];
        $rate_main=$row["rate_main"];
        $rate_add=$row["rate_add"];
        $qualification=$row["qualification"];?>
        <table><form method="post">
        <tr class="border_bottom">
        <td height="230"><img src='<?php echo $profile;?>'width="200" height="200"/>&nbsp;</td><td><td></td></td>
        <td class="data" width="800"><strong>Username:</strong>  <?php echo $username;?></br>
        <strong>Address:</strong>  <?php echo $address;?></br>
        <strong>Gender:</strong><?php echo $gender;?></br>
         <strong>Main Subject:</strong><?php echo $main_subject  ;?></br>
         <strong>Subject Added: </strong><?php echo $subject_add;?></br>
         <strong>Main Subject Rate:</strong> <?php echo $rate_main;?></br>
        <strong> Added Subject Rate:</strong><?php echo $rate_add; ?></br>
         <strong>Qualification:</strong> <?php echo $qualification; ?></td>
           <?php 
         if($register_ID == $_SESSION['myid']){
                ?>
                <td><label>Your Profile</label></td>
                <?php
            } else {

                    ?>
                 <form name="post">
                 <td><button class='friendBtn unfriend'  name="" data-type="unfriend">Unfriend</button>
                 <input type="hidden" name="id"  value="<?php echo $row['register_ID'];?>" />  
                 <input onclick="change()" type="button" name="addfriend" data-type='addfriend' id="addfriend" value="addfriend" data-uid=<?php echo $register_ID;?>    /></td>
</form>                   
</td> 
<?php
            }
            }
?>
 </tr>
</div>
</table>
 </form>
<?php
                if(isset($_POST['id']) ) {
$user_id = $_SESSION['myid'];
$friend_id = $_POST['id'];
$status = 1;
$sql="INSERT INTO friends(user_id,status,friend_id)" ."VALUES('$user_id','$status','$friend_id') ";

            if($mysqli->query($sql)=== true) {
                          echo "Request Send" ;
            }
                  else {
                             $_SESSION['addfriend']="Add Friend";   }   
            }
                }
?> 

javascript

function change ()
{
document.getElementById("addfriend").value="Request Sent";

}

1 个答案:

答案 0 :(得分:0)

我有另一种方法,没有javascript。

用此替换

<input type="button" name="addfriend" data-type='addfriend' id="addfriend" value="<?php if($has_been_added == 'yes'){ echo 'Request Sent'}else{ echo 'Addfriend'; } ?>" data-uid=<?php echo $register_ID;?>    />

然后在

之后的while循环中
    $qualification=$row["qualification"];

添加

    $has_been_added = $row["has_been_added"]; 

将插入查询更改为

$sql="INSERT INTO friends(user_id,status,friend_id,has_been_added)" ."VALUES('$user_id','$status','$friend_id','yes')";

这意味着您必须将has_been_added列添加到friends表中,并将其默认值设置为no

这意味着无论何时添加任何朋友,has_been_added都将更新为yes。因此,相应的按钮将是“添加朋友”或“请求发送”