Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:15:37+00:00 2026-06-04T08:15:37+00:00

Has anyone found a way to intercept the default Form::postLink() forms with Jquery? I

  • 0

Has anyone found a way to intercept the default Form::postLink() forms with Jquery?
I would like the form to work without JS (therefore the postLink).
But with JS enabled I want to intercept the post and call it via AJAX.

<?php echo $this->Form->postLink('Delete', array('action'=>'delete', $prospect['Prospect']['id']), array('class'=>'postLink', 'escape'=>false), __('Sure you want to delete # %s?', $prospect['Prospect']['id'])); ?>

generates:

<form action="/admin/prospects/delete/4f61ce95-2b6c-4009-9b89-07e852b0caef" name="post_4f648f773923b" id="post_4f648f773923b" style="display:none;" method="post">
    <input type="hidden" name="_method" value="POST"/>
</form>
<a href="#" class="postLink" onclick="if (confirm('Sure you want to delete # 4f61ce95-2b6c-4009-9b89-07e852b0caef?')) { document.post_4f648f773923b.submit(); } event.returnValue = false; return false;">
    Delete
</a>

The main problem is that the js is placed inline here. Therefore always triggers even if I try to intercept the click event (or the post event – tried that too):

<script>
$(document).ready(function() {
    $('table.list a.postLink').click(function(e) {
        e.preventDefault();
      alert('Handler for .submit() called.');
      // TODO: do ajax request here and return false
      return false;
    });
});
</script>

So in the end the form always submits normally and redirects – either ignoring any ajax call (catching the form submit) or posting/redirecting regardless of an ajax call just made (catching the click event).

I would like to delete this record via AJAX and – if successful – just remove that table row from DOM. It would be great if one doesn’t have to modify all 300+ “delete buttons” in the application for it, though.

If everything fails I could probably still override the FormHelper (extend it and alias it). But I was hoping on a less invasive solution here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:15:38+00:00Added an answer on June 4, 2026 at 8:15 am

    I know this is old, but for any of those searching:

    • You need to first remove the ‘onclick’ attribute added to the delete
      link.
    • Then, you add a .click function to the delete link
    • You need the url (which can be hardcoded or retrieved from the form, which is always the prev element in cakephp Form->postLink

    Here is the code:

    
    $(function(){
    
        $('a:contains("Delete")').removeAttr('onclick');
    
        $('a:contains("Delete")').click(function(e){
            e.preventDefault();
            var form = $(this).prev();
            url = $(form).attr("action");
            $.post(url);
            return false;
        });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone found a way to avoid leaderbaord hacking on Game Center or at
Has anyone found a way to style the widget that appears when SectionIndexer is
Has anyone found a decent way to restart Passenger via a link? I need
I am using TortoiseHg + VisualHg on Visual Studio 2008. Has anyone found way
Has anyone found a way of display a loading message while rich:dataTable loads? I've
Has anyone found a way to get around this? Or a better technique to
Has anyone found a way to save a FlowDocument as BAML or other compressed
Has anyone found a version of pywin32 for python 3.x? The latest available appears
Has anyone found a keyboard shortcut for the Show All Files in the Solution
Has anyone found support for creating or changing a drawing using Google Apps Script?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.