Home > Back-end >  Problem sending request with jquery by onclick event
Problem sending request with jquery by onclick event

Time:01-25

hello I am sending an ajax request to php but it does not work But my onclick event works fine

this is my code

html code

<span  onclick="accept_recharge(this, <?= $pay['payment_id'] ?>)">accept</span>

jquery code

<script>
      function accept_recharge(sender, payment_id) {
            sender = $(sender);
            var parent = sender.parentsUntil('.todo-entry').parent();
            $.ajax('<?= baseUrl() ?>/admin/acceptRecharge/'   payment_id, {
                  type: 'post',
                  dataType: 'json',
                  success: function(data) {
                        var response = JSON.parse(data);
                        new Noty({
                              type: 'success',
                              layout: 'topRight',
                              theme: 'nest',
                              text: response.message,
                              timeout: '2000',
                              progressBar: true,
                        }).show();
                  }
            });
            parent.remove();
      }
</script>

php code

public function acceptRecharge($payment_id)
      {
            AdminModel::acceptRecharge($payment_id);
            echo json_encode(array('status' => '1', 'message' => 'recharge success'));
      }

CodePudding user response:

It is best not to mix scripting languages when you can. Consider the following changes.

<script>
function accept_recharge(sender, payment_id) {
  sender = $(sender);
  var parent = sender.parentsUntil('.todo-entry').parent();
  $.ajax({
    url: './admin/acceptRecharge/'   payment_id,
    type: 'post',
    dataType: 'json',
    success: function(response) {
      new Noty({
        type: 'success',
        layout: 'topRight',
        theme: 'nest',
        text: response.message,
        timeout: '2000',
        progressBar: true,
      }).show();
    }
  });
  parent.remove();
}
</script>

You should not need to use BaseURL if the page is on the same server. You may want to use a Relative Path URL instead.

if you continue to have issues, check your Request and Response details under Network Tab.

  •  Tags:  
  • Related