2

I'm trying to submit a guest entry by building a javascript array of objects to mimic a form submission and submit using ajax. I've managed to successfully submit the entry with just a title but when trying to add Matrix/Super Table rows I'm having no luck.

I've looked here https://github.com/engram-design/SuperTable/wiki/Updating-a-Super-Table-field-from-a-front-end-form to get ideas for how this should work.

Super table block handle is answers and the Super Table field I'm trying to submit is a plain text field with the handle question.

My current code is:

$('#submitQuiz').on('click', function (e) {
  e.preventDefault();

  var testEntrySubmission = [
    {
      name: 'action',
      value: 'guestEntries/saveEntry'
    },
    {
      name: 'sectionId',
      value: '5'
    },
    {
      name: 'enabled',
      value: '1'
    },
    {
      name: 'redirect',
      value: '/'
    },
    {
      name: 'title',
      value: 'user name'
    },
    {
      name: 'answers[new1][fields][question]',
      value: 'fingers crossed'
    }
  ];

  $.post('http://mywebsite.dev/', testEntrySubmission, function(response) {
    if (response.success) {
      console.log('success')
    } else {
      // response.error will be an object containing any validation errors that occurred, indexed by field name
      // e.g. response.error.fromName => ['From Name is required']
      alert('An error occurred. Please try again.');
    }
  });
});
  • All of your custom fields are going to require a name that starts with fields. So, your answers[new1][fields][question] should probably be fields[answers][new1][fields][question]. You can find an example of a front-end Matrix fields input structure here: straightupcraft.com/articles/… – Ben Parizek Sep 20 '16 at 8:25
0

Answer to my question. I ended up making a form and printing out the post as JSON.

$('#submitQuiz').on('click', function (e) {
  e.preventDefault();

  var testEntrySubmission = [
    {
      name: 'action',
      value: 'guestEntries/saveEntry'
    },
    {
      name: 'sectionId',
      value: '5'
    },
    {
      name: 'enabled',
      value: '1'
    },
    {
      name: 'redirect',
      value: '/'
    },
    {
      name: 'title',
      value: 'user name'
    },
    {
    'name':'fields[answers]',
    'value':''
    },
    {
      'name':'fields[answers][new1][type]',
      'value':'6'
    },
    {
      'name':'fields[answers][new1][enabled]',
      'value':'1'
    },
    {
      'name':'fields[answers][new1][fields][question]',
      'value':'John Smith'
    },
  ];

  $.post('http://mywebsite.dev/', testEntrySubmission, function(response) {
    if (response.success) {
      console.log('success')
    } else {
      // response.error will be an object containing any validation errors that occurred, indexed by field name
      // e.g. response.error.fromName => ['From Name is required']
      alert('An error occurred. Please try again.');
    }
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.