2
<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?

2
  • I can help you with code, if you need. =) Commented Sep 19, 2015 at 18:00
  • What is the name of the file you posted? Commented Sep 19, 2015 at 18:45

2 Answers 2

1

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

$("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.

Sign up to request clarification or add additional context in comments.

2 Comments

By the time Javascript is being used on a page, the PHP is long done running. You'll need to make a separate file (maybe ajax.php) and have the javascript send a call to that file in order to get your php function to run
Yeah, it's good idea to keep code clean in separate files. I'm using something like scripts, scripts\ajax, so it's easy to understand.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.