简体   繁体   中英

How to pass cakephp formhelper input values to ajax via jquery?

I am still new to cakephp, and my attempt is to retrieve the FormHelper's value and pass it via $.ajax() call in jquery. However, by cakephp convention, the name of each field generated by FormHelper will be in the format of data[Model][field] . Now, I want to submit $_POST data in form of cakephp array format. However, I couldn't find a way to do so, because I couldn't find a way to turn name and value attribute into a passable array format.

My attempt was to turn everything into string and try to create a json array. However, I failed to do so, and this method doesn't seem convincing to me too.

function submitEdit(sendurl, formid){
  var dataset = [];
  $('form#'+ formid + ' > input,select').each(function(){
    dataset.push($(this).attr('name') + ':' + $(this).val());
  });

  alert(dataset);
  $.ajax({
        type: 'POST',
        data: '{' + dataset + ']',
        url: sendurl,
        success: function(content){
          $('.setting-preview.username').append('<pre>' + content + '</pre>');
        }
  });
}   

Therefore, how do I pass this as data[Model][field] array to the sendurl controller?

Something like

$.ajax({
        type: 'POST',
        data: {
            Model: {
                 foo: $('#ModelFoo').val(),
                 bar: $('#ModelBar').val()
            }
        },
        url: sendurl,
        success: function(content){
          $('.setting-preview.username').append('<pre>' + content + '</pre>');
        }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM