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;
}
Alonso
  • 141
  • 4
  • 12

1 Answers1

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

Community
  • 1
  • 1
clearshot66
  • 2,292
  • 1
  • 8
  • 17
  • Does 'button1value' indicate wether has been pressed? – Alonso May 08 '17 at 18:28
  • 1
    @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 – clearshot66 May 08 '17 at 18:29
  • 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 – Alonso May 08 '17 at 18:40
  • 1
    #button1 is the ID of the button, you need to have it like this , button is the POST value, such as $_POST['button'] will come through as whatever value= ' ', in this case, button1value – clearshot66 May 08 '17 at 18:41
  • 1
    Yup. Whatever you name inputs/buttons in HTML with name= ' ', is what comes through as $_POST or $_GET in PHP. So on submit of a form, the post would be $_POST['user'] and will be 'dog' – clearshot66 May 08 '17 at 18:44
  • I added some comments if you have any questions feel free to ask – clearshot66 May 08 '17 at 18:46
  • I get this Notice message: **Notice:** Undefined index: button in file.php – Alonso May 08 '17 at 18:56