Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Action within a Form

Ok so I have a form like this ....

<form id="form" method="post" action="http://localhost/shopper/admin/index.php?route=system/template/update&amp;token=19690a8b739af0d963f129b93b236f1d&amp;template_id=22">
....
                    <span class="delete"><a href="#" class="delete_step" rel="1">Delete Step</a></span> 
                         <tr>           
                  <td><span class="required">*</span> Template Name:</td>
                  <td><input size="100" value="real" name="name"></td>
                </tr>
                                    ....

                    <span class="delete"><a href="#" class="delete_step" rel="2">Delete Step</a></span> 
                     <tr>           
              <td><span class="required">*</span> Template Name:</td>
              <td><input size="100" value="real" name="name"></td>
            </tr>   
    ....
    </form>

And i need it to where when someone clicks on the "delete_step" anchor tag that the page goes to a different location then where the form goes and with the data in the rel tag....I was thinking something like this approach

 $('.delete_step').live('click', function(e) {
   var delete_location = window.location.pathname.replace('admin/', '');            
   window.href = delete_location + '?route=module/delete_step&template_number=<?php print $template_id; ?>&step_id=' + $(this).attr("rel");
 });

The problem is it always goes to this location

  http://localhost/shopper/admin/#

I tried preventDefault but then nothing happens...any ideas

Upvotes: 1

Views: 86

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

Three things. First, you have a javascript error since preventing the default action did not solve your problem. The error is when you attempt to reference the href property of the window object. The window object doesn't have this property. Just set location.href = .... Second, I would return false from the click handler since you probably want to both prevent the default action and prevent the click from propagating to the containing elements. Third, I would not do a delete action using a GET request. I'd wrap it up in a form post (could be via AJAX, too) so that the user can't bookmark it and do a delete accidentally.

You can confirm the first by looking at the console in Firefox/Firebug.

$('.delete_step').live('click', function(e) {
   var delete_location = window.location.pathname.replace('admin/', '');
   var data = 'route=module/delete_step&template_number=<?php print $template_id; ?>&step_id=' + $(this).attr("rel");
   $.post( delete_location, data, function(result) {
        // handle the result of doing the deletion
   });
});

Upvotes: 2

Josiah Ruddell
Josiah Ruddell

Reputation: 29831

window.href should be window.location.href. But I would say to redirect from the server code.

Upvotes: 1

Related Questions