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

html中内联php代码中的If条件(如果按下按钮),php,html,if-statement,Php,Html,If Statement,我试图在按下按钮时发送好友请求,但我的问题是,当页面刷新时,我会收到两个警报,当我单击按钮时,是因为我的php在html代码中内联吗?谢谢 Ey //在主页中 Ey //Javascript-Ajax提交函数 函数发送请求(用户ID){ $.ajax({ 类型:“POST”, url:“ajax.php”, 数据:“userid=”+userid, cache:false, 成功:功能(结果){ 警报(结果); } }); } //在ajax.php页面中 请试试这个。确保已关闭DIV警

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


Ey

//在主页中
Ey

//Javascript-Ajax提交函数 函数发送请求(用户ID){ $.ajax({ 类型:“POST”, url:“ajax.php”, 数据:“userid=”+userid, cache:false, 成功:功能(结果){ 警报(结果); } }); } //在ajax.php页面中
请试试这个。确保已关闭DIV
警报
。我没有在你的代码中看到它


对main.php的一些更新

  • 数据:{otherusersid:$otherusersid',myuserid:$myuserid},
    ,这里您使用的是
    名称
    ,而不是
    myuserid
  • $query=mysqli\u query($link,“从360tery中选择userid,其中Email='$Email')
    ,这里您使用的是
    用户名
    而不是
    电子邮件
  • 非常重要:PHP区分大小写。您有时使用
    用户名
    电子邮件
    作为
    用户名
    电子邮件
    等,请使用mysql中设计的变量、mysql表和列
    如果您显示呈现的HTML,也许我们可以了解原因。@mplungjan我添加了屏幕快照,使用ajax在这方面会很有用case@ABCatella举个例子会很有帮助,我是一个网络开发的初学者。但是可以等到明天,因为我在手机上使用SO,不能在手机上写答案,也不能通过笔记本电脑更新吗?@A B Catella感谢您的快速响应,我已经在我的代码中对您的答案做了相应的更改,这是我的代码和错误。我在您的代码中添加了一些变量,因为我想使用会话添加我的用户id及其声明的,我确定它的工作$myuserid,我想添加发送到该用户id$otherusersid的请求,我还确保我的sql statmenet正在工作您添加了我在代码中添加的javascript函数?这是说,
    send\u request
    函数不可用。我已经这么做了,很抱歉上传了错误的main.php文件。这里是代码1。更改为数据:“otherusersid=“+otherusersid+”&myuserid=“+myuserid,
  • ,2。包括Jquery吗?因为$.ajax需要jquery库。3.为什么要在脚本末尾添加
    ?>
    <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))
       {
          $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;
          echo '<form action=""  method="post" enctype="multipart/form-data"> ';
          echo '<input type="submit" name="addfriend" value="Add Friend" class="btn btn-warning btn-xs">'; 
          echo '</div>';
        }
         if(isset($_POST['addfriend']))
         {                 
          echo "<script type='text/javascript'>alert('Request sent !');</script>";
         }    
    ?>
    
    //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";
            }   
        }
        ?>