0

There already are some entries about the topic explaining that the only way is by using Ajax. However, I do not find solution for this problem: I just want that, once pressing a button in an HTML file, run a PHP function.

Imagine I have two buttons button1 button2 so I want each of them to run function run1() function run2() of my PHP file.

How the Ajax would look like? This is what I have.

HTML:

<script> // For button1
$(document).ready(function(){
          $("#button1").click(function(){
     var value = $("#button1").val();
              $.ajax({
                  url: 'myPHP.php',
                  type: 'POST',
                  data: {button:value},
                  success: function(response){
                      $('#output').append(response);
                      }//end success

              }); //end ajax
          });
        });
</script>


<body>
    <form method="post" action="myPHP.php">

    <button id= "button1" type="submit" value="value1">Button One</button>
    <button id= "button2" type="submit" value="value2">Button Two</button>

    </form>
</body>

PHP:

if($_POST['button'] == 'value1'){
  // run function1; 
}else{
  // run function2;
}
6
  • 1
    In php you'd have to determine which post variable (which button was clicked), then you'd run the function that way. Ajax is used to send the button value over Commented May 8, 2017 at 18:22
  • I thought Ajax let you specify the function you want to run on the php Commented May 8, 2017 at 18:25
  • did you name the other file myPhP.php, and inside do you have that code within <?php and ?> tags? Commented May 8, 2017 at 19:30
  • Yes, I checked the syntax and tags. I think it does not execute the script and goes directly to the PHP file Commented May 8, 2017 at 19:32
  • Are they separate files?, There should be 2, 1 for html and 1 for the php script Commented May 8, 2017 at 19:39

1 Answer 1

1

Send the page the value via ajax, then use it within that page

$(document).ready(function(){

          $("#button1").click(function(){    // the #id you supplied to the button, id= 'button1'

     var value = $("#button1").val();  // Value of the button, value= 'button1value'

              $.ajax({
                  url: 'phppage.php', // The php page with the functions
                  type: 'POST',
                  data: {button:value},  // the name you're assigning, think how a $_GET works in URL,  .php?name=value...

                  success: function(response){
                      $('#output').append(response);
                      }//end success

              }); //end ajax
          });
        });

php page

if($_POST['button'] == 'button1value'){     / $_POST['name of input/button'] == 'value being sent through'

  // run function 1;
}else{
  // run function2;
}

?>

This post, first answer, also answers depending how you intend to use it: using jquery $.ajax to call a PHP function

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

7 Comments

Does 'button1value' indicate wether has been pressed?
@Alonso yes, in jquery $("#button1").click(function(){ indicidates button 1, where as $("#button2").click(function(){ would be what happens when it's button 2. Just give your buttons a value and use that in your php page
So please correct me if I am wrong: in this example #button1 is the name of the button 1, button1value is the value of the button 1 and... what is the first 'button' in the php page
#button1 is the ID of the button, you need to have it like this <button id='button1' value='button1value' />, button is the POST value, such as $_POST['button'] will come through as whatever value= ' ', in this case, button1value
Yup. Whatever you name inputs/buttons in HTML with name= ' ', is what comes through as $_POST or $_GET in PHP. So <input type='text' name='user' value='dog' /> on submit of a form, the post would be $_POST['user'] and will be 'dog'
|

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.