0

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?

1 Answer 1

1

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>');
        }
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.