execute php script inside javascript : This is my php page --

    <div class="whomfollow">
          	<div class="umayknow">People you may know</div>
          <ul>
          <?php
				$result = $linkID1->query("select ud.UserID,ud.Name,ud.Company,ud.Designation,ud.UserType,up.ProfilePic from user_details ud join user_picture up on ud.UserID=up.UserID where ud.UserID!='$UserID' ORDER BY RAND() LIMIT 20")
				or
				die(mysqli_error());
				
				while($row = mysqli_fetch_array($result))
				{
					$UT1='';
					if($row['UserType']=='A')
					{
						$UT1='Advertiser';
					}
					else if($row['UserType']=='P')
					{
						$UT1='Publisher';
					}
			?>
            <li class="postimage followimgconnections">
                <div class="pull-left"><a href="#"><?php echo "<img src=profile_pic/".$row['ProfilePic']." />"; ?></a></div>
                <div class="pull-left followboxcontent">
                  <div><a href="#"><b><?php echo $row['Name']; ?></b></a></div>
                  <div><a class="followsmall_content" href="#"><?php echo $UT1." - ".$row['Designation']." - ".$row['Company']; ?></a></div>
                  <div class="followbtn"><button id="myBtn" onClick="connect(<?php echo $row[UserID]; ?>)">Connect</button></div>
                </div>
            </li>
            <?php
				}
			?>
          </ul>
          </div>
          <div class="clear"></div>
        </div>
    <div id="myModal" class="modal">
		  <!-- Modal content -->
		  <div class="modal-content">
		    
		    <p class="popupheading">Invitation Sent</p>
		    <p class="popupcontent">Your invitation is on its way.</p>
		    <div class="invidone"><a href="#" class="close">Done</a></div>
		  </div>
		</div>

    <script>
    function connect(num) {
		var x = document.getElementById('myBtn');
		<?php
			$ID=num;
			$stts='IS';  //Invitation Sent
			$stmt = $linkID1->prepare("insert into user_connections set MyUserID=?, MyFriendsUserID=?, cstatus=?");
			$stmt->bind_param("sss", $UserID,$ID,$stts);
			$stmt->execute();
			$stmt->close();
		?>
		modal.style.display = "block";
		}
    </script>

Now as soon as the page is loaded, the php part inside the javascript is executed automatically, I want to run that php part when the javascript is called. If its not possible to do it this way then kindly help with some alternate solutions. Kindly help.