如果在html内的内联php代码中的条件(如果按下按钮)

时间:2017-01-12 14:49:11

标签: php html if-statement

我正在尝试按下按钮时发送好友请求,但我的问题是当页面刷新时我收到两个警报,当我点击按钮时,因为我的php在我的html代码中是内联的?感谢

when the page refreshes

when i click the button

public class Base {
    public virtual void Validate(){return; };
}

[NotMapped]
public class Derived:Base {
    public override void Validate(){DoSomethingWithData(); return; };
}

1 个答案:

答案 0 :(得分:0)

//In your main page


            <div class="alert alert-success fade in">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                <p><strong>Ey!</strong></p>
                <?
                $getusersquery=mysqli_query($link,"Select * from 360tery where userid='$otherusersid' LIMIT 4  ");
                while($row5=mysqli_fetch_array($getusersquery))
                   {
                      //here, added userid to use in button
                      $userid=$row5['userid'];
                      $userusername=$row5['username'];
                      $useremail=$row5['Email'];
                      $userprofilepicture=$row5['Profilepicture'];
                      $userfirstname=$row5['firstname'];
                      $userlastname=$row5['lastname'];
                      $user_profile_pic='/users/'.$userusername.'/profilepicture/'.$userprofilepicture;
                      $userphoto = "<img src='.$user_profile_pic' id='avatarimg' class='img-circle' height='85' width='85'> <br> <br>";

                      echo'<div class="well">';
                      echo '<p><strong> '.$userfirstname.' '.$userlastname.'</strong> </p>';
                      echo'<p>'.$userusername.'</p>';
                      echo $userphoto;
                      //Here changed form to button
                      echo '<button onclick="send_request('.$userid.')" name="addfriend" class="btn btn-warning btn-xs">Add Friend</button>'; 
                      echo '</div>';
                    }    
                ?>

        //Javascript - Ajax submit function
        <script>
            function send_request(userid){
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "userid="+userid,
                cache: false,
                success: function(result){
                    alert(result);
                    }
                });
            }
        </script>

    //In ajax.php page
    <?
    if(isset($_POST['userid'])){
        $userid=$_POST['userid'];
        if(your_condition_like_mysqliupdate_or_other_with_$userid) {
            echo "Friend Request sent";
        } else {
            echo "Friend Request NOT sent";
        }   
    }
    ?>

请试试这个。确保您已关闭DIV alert。我没有在你的代码中看到它

对main.php的一些更新

  1. data: {otherusersid: "$otherusersid", myuserid: "$myuserid"},,此处您正在使用name而不是myuserid
  2. $query=mysqli_query($link,"SELECT userid from 360tery where Email='$email'");,此处您正在使用username而不是Email
  3. 非常重要:PHP是有效的。您有时使用UsernameEmail作为usernameemail等,请使用变量,mysql表,mysql中设计的列。