-1
<script>    

        $(document).ready(function(){
        $("#amount").on('click', function(){
            var amount = this.value;
            $.ajax({
            url: "ipg-util.php/createHash",
             type: 'post',
              data: { "amount": amount
             },
                 success: function(response) { console.log(response); }
                });     

        });

    });
</script>
Biffen
  • 6,249
  • 6
  • 28
  • 36
  • You have to call another function in ajax resposne right :) – Naresh Kumar P Sep 16 '16 at 05:02
  • You never mentioned that your Problem was about sending mails in your Post. Anyways, if you are using a Class for your Mailing. **You may need to `include` the Class File in your** ***ipt-util.php***. – Poiz Sep 17 '16 at 06:09
  • yes, I dint mention that in my question. but In my stackoverflow account, per day it allows only one question to ask. I included all Php mailer class file inside my function. But it shows the same error "could not instantiate the mail function". – Princess lilly Sep 17 '16 at 10:59
  • @Princesslilly. Alternative too the solution of the **Poiz** i have mentioned a simpler way to send Email in my answer below. :) – Naresh Kumar P Sep 18 '16 at 02:52

2 Answers2

0

You can simply pass the Method to Callas part of the dataproperty like so:

<script>    
    $(document).ready(function(){
    $("#amount").on('click', function(){
        var amount = this.value;
        $.ajax({
            url: "ipg-util.php",
            type: 'post',
            data: { "amount": amount, "callable": "createHash"},
        success: function(response) { console.log(response); }
        });     
    });
});
</script>

Then inside ipg-util.php You can do something like this:

    <?php
        $callable = isset($_POST['callable']) ? $_POST['callable'] : "default";
        $amount   = isset($_POST['amount'])   ? $_POST['amount']   : null;

        switch($callable){
            case "createHash":
                $response = createHash(); // CALL createHash METHOD DEFINED HEREIN
                break;
            case "doAnotherThing":
                $response = doAnotherThing(); // CALL doAnotherThing METHOD DEFINED HEREIN
                break;
            default:
                $response = default(); // CALL default METHOD DEFINED HEREIN
                break;

        }

        die(json_encode($response));

        function createHash(){

        }


        function doAnotherThing(){

        }


        function default(){

        }
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Good... just check it as the answer @Princesslilly – Poiz Sep 16 '16 at 05:26
  • One more doubt, I cant send mail from localhost. I tried many settings in php.ini and sendmail.ini. I tried this link also [http://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost] but still it shows the error "Could not instantiate mail function" – Princess lilly Sep 17 '16 at 04:58
  • You never mentioned that your Problem was about sending mails in your Post. Anyways, if you are using a Class for your Mailing. **You may need to `include` the Class File in your** ***ipt-util.php***. – Poiz Sep 17 '16 at 06:06
0

$.ajax to call a server context or URL, whatever to invoke a particular 'action'. What you want is something like and the below code as follows:

$.ajax({ url: '/demo/websitepage',
         data: {action: 'calltofunction'},
         type: 'POST',
         success: function(output) {
                  alert(output);
                  }
});

On the server side, the action POST parameter should be read and the corresponding value should point to the method to invoke and the below code is used for doing that:

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'calltofunction' : test();break;
        case 'blah' : blah();break;
        // ...etc...
    }
}

Hope so this will be a better reference for this: https://en.wikipedia.org/wiki/Command_pattern

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
  • One more doubt, I cant send mail from localhost. I tried many settings in php.ini and sendmail.ini. I tried this link also [http://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost] but still it shows the error "Could not instantiate mail function". Can you help me – Princess lilly Sep 17 '16 at 04:59
  • You never mentioned that your Problem was about sending mails in your Post. Anyways, if you are using a Class for your Mailing. **You may need to `include` the Class File in your** ***ipt-util.php***. – Poiz Sep 17 '16 at 06:09
  • @Princesslilly. There are methods to send Email from the local-host using **send-grid** or **mailgun**. Reference: https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html and https://documentation.mailgun.com/quickstart-sending.html#how-to-start-sending-email – Naresh Kumar P Sep 18 '16 at 02:49
  • thank you Naresh! I tried send-grid. But It shows error `401HTTP/1.1 401 Unauthorized Server: nginx Date: Mon, 19 Sep 2016 06:55:31 GMT Content-Type: application/json Content-Length: 88 Connection: keep-alive X-Frame-Options: DENY {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}` – Princess lilly Sep 19 '16 at 06:56