chowwy
chowwy

Reputation: 1126

use jquery dialog button to submit form

Working on a submit function from a jQuery dialog (based on this question). Using codeigniter and jquery.

I'd like to tie the ajax POST to the jquery dialog button click, but don't know what the selector should be for that button. I tried to use .ui-dialog-buttonpane button:first or "this" as the selector; neither worked.

HTML FORM

<?php echo form_open('bookmarks/addBookmark'); ?> //form name: create_bookmark
<?php echo form_input('bookn', $bname); ?>
<?php echo form_hidden('booki', $this->uri->segment(4, 0)); ?>
<button class="bb_button">Bookmark</button>
<?php echo form_close(); ?>

jQuery

$(function() {

        $( "#create_bookmark" ).dialog({
            autoOpen: false,
            width: 250,
            height: 250,
            modal: true,
            buttons: {
                "Add location": function() {    

                $('?????').click(function() {  //How do I specify the dialog button?

            $.ajax({
                url: '/bookmarks/addBookmark',
                type: 'POST',
                data:{"bookn": $("input[name='bookn']").val()​, "booki": $("[name='booki']").val()​},
                success: function (result) {
                    alert("Your bookmark has been added.");
                }                                         
            }); 
                },
                Cancel: function() {$( this ).dialog( "close" );}
            }
        });
    });

Upvotes: 0

Views: 1748

Answers (2)

Kyle Macey
Kyle Macey

Reputation: 8154

It looks like after I broke it out and indented properly, you were missing a } This is presuming the implementation of Slaks's answer

        buttons: {
          "Add location": function() {    

            $.ajax({
              url: '/bookmarks/addBookmark',
              type: 'POST',
              data:{
                "bookn": $("input[name='bookn']").val()​, 
                "booki": $("[name='booki']").val()
              ​},
              success: function (result) {
                alert("Your bookmark has been added.");
              }
          });                                       
        } //<<This one  

Upvotes: 0

SLaks
SLaks

Reputation: 887453

The function you specify in the buttons object is the click handler.
You can send your request right in that function.

Upvotes: 4

Related Questions