-1

This seems straightforward, but I can't get it to work. I have a button, when it's clicked I'd like to execute a php function which is defined in the same file as the button. My code is below, I get the "clicked" alert when the button is clicked, but no alert on response from the function.

//this is in myfile.php
    <?php
        echo '<button type="button" name="save_button" onclick="save()">Save</button>';
    ?>
        <script type="text/javascript">
            function save() 
            {
              alert("clicked");

              $.ajax({
                url: 'myfile.php',
                type: 'post',
                data: { "set_description": ""},
                success: function(response) { alert(response); }
              });
            }
          </script>

        <?php 
         function set_description() {

          return "a string";
        }
        ?>
ab11
  • 19,770
  • 42
  • 120
  • 207
  • How do you get the alert("clicked"); whether at the button the function is named save()? – Wilfredo P Apr 23 '15 at 20:44
  • 1
    Better to put your PHP into a different file, otherwise you'll get all your HTML in the file as well when you get the response back from AJAX – scrowler Apr 23 '15 at 20:47

1 Answers1

1

Change the type of the jquery Ajax code from post to get since you want to use the response, else I can't see something wrong there