-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";
        }
        ?>
2
  • How do you get the alert("clicked"); whether at the button the function is named save()? Commented Apr 23, 2015 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 Commented Apr 23, 2015 at 20:47

1 Answer 1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.