如何在同一个按钮上运行php动作和javascript代码

时间:2021-07-09 15:28:33
<div id="askfriend">
      <form action="friend.php" method="post">
       <input type="submit" id="requestbutton" name="requestbutton2" value="Send Friend Request" style="margin-top:5%; margin-left:10%; height:50%;" onclick="change()"/>
       <input type="submit" id="cancelbutton" name="cancelbutton2" value="Cancel" disabled style="margin-top:5%; margin-left:10%; height:50%; width:30%;" onclick="changeback()"/>
      </form>
       <?php
        if(isset($_POST['requestbutton2']))
         {
          echo "clicked";
         }
       ?>
     </div>

<script>
  function change()
   {
    var elem=document.getElementById("requestbutton");
    var elem2=document.getElementById("cancelbutton");
    if(elem.value=="Send Friend Request")
     {
      elem.value="Friend Request Send";
      elem.disabled=true;
      elem2.disabled=false;
     }
   }
  function changeback()
   {
    var elem=document.getElementById("requestbutton");
    var elem2=document.getElementById("cancelbutton");
    if(elem2.value=="Cancel")
     {
      elem2.disabled=true;
      elem.value="Send Friend Request";
      elem.disabled=false;  
     }
   }
 </script>

when I click "Send Friend Request" button javascript code runs but the action code whatever I wrote inside php, for the same button does not run. What's the solution?

当我点击“发送朋友请求”按钮javascript代码运行,但动作代码我在PHP内写的任何内容,因为相同的按钮不运行。解决方案是什么?

2 个解决方案

#1


1  

You need to use AJAX. So, when you tap on a button, JavaScript will make an HTTP request to your backend.

你需要使用AJAX。因此,当您点按某个按钮时,JavaScript会向您的后端发出HTTP请求。

$("your_element_id").click(function(){
    $.ajax({
      type: "POST",
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
});

Use jQuery or something like that. Here's a good manual.

使用jQuery或类似的东西。这是一本很好的手册。

#2


0  

$("your_element_id").click(function(){
    $.ajax({
      type: "POST",
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
});

Use jQuery or something like that. Here's a good manual.

使用jQuery或类似的东西。这是一本很好的手册。

#1


1  

You need to use AJAX. So, when you tap on a button, JavaScript will make an HTTP request to your backend.

你需要使用AJAX。因此,当您点按某个按钮时,JavaScript会向您的后端发出HTTP请求。

$("your_element_id").click(function(){
    $.ajax({
      type: "POST",
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
});

Use jQuery or something like that. Here's a good manual.

使用jQuery或类似的东西。这是一本很好的手册。

#2


0  

$("your_element_id").click(function(){
    $.ajax({
      type: "POST",
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
});

Use jQuery or something like that. Here's a good manual.

使用jQuery或类似的东西。这是一本很好的手册。