0

I am creating a form using exp:channel:form to add an entry to a channel using ajax I have a hidden title, utl_title fields that are programmatically set and an entry_id field that is initially set to 0. This works fine, with the data return returned from the ajax call I update the form that is created and set the entry_id field with the return entry_id so that in hopes that when the form is posted again it would update channel entry instead of adding another channel entry.

The problem is that everytime the form is posted it creates a new channel entry, is there a way using the ajax option to update the entry that was just previously posted?

Here is my form template and the JavaScript code I use to do the ajax call and update the form entry_id field and url parameter.

Template Form
=============
{exp:channel:form channel="ads" json="yes" datepicker="no" include_assets="no" include_jquery="no" logged_out_member_id="2"}
    <input type="hidden" name="title"       id="ee_form_title"      value="">
    <input type="hidden" name="url_title"   id="ee_form_url_title"  value="">
    <input type="hidden" name="ad_email"    id="ee_form_ad_email"   value="">
    <input type="hidden" name="ad_image"    id="ee_form_ad_image"   value="">
    <input type="hidden" name="ad_message"  id="ee_form_ad_message" value="">
    <input type="hidden" name="ad_persona"  id="ee_form_ad_persona" value="">
    <input type="hidden" name="ad_tagline"  id="ee_form_ad_tagline" value="">
{/exp:channel:form}


JavaScript
==========
$('#publishForm').ajaxForm({
    dataType: 'json',
    async: false,
    cache: false,
    success: publishFormResponse
});


function publishFormResponse(responseText, statusText, xhr, $form) {
    if (statusText == "success") {
        // xhr.responseJSON.entry_id is always a new incremented id instead of the id from the first form post
        if (!entry_id) {
            // Update the form entry_id field and the form entry_id url parameter
            entry_id = xhr.responseJSON.entry_id;
            $("#publishForm .hiddenFields input[name='entry_id']").val(entry_id);
            $('#publishForm').attr('action', $('#publishForm').attr('action').replace("entry_id=0", "entry_id="+entry_id));
        }

    } else {
        alert('Failed with the following errors: '+data.errors.join(', '));

    }
}
  • Don't inc the entry_id on your form. EE only needs title when creating new entries. – stuartmcd69 Mar 20 '15 at 7:39
  • I am not incrementing the entry_id I am setting it to the entry_id returned form the ajax call in hopes that if the fields are updated and then submitted again that the ajax call would update the previous channel entry instead of adding a new channel entry. – Trevor Orr Mar 20 '15 at 14:56
  • Can you throw up some template code or something with programming logic? Adding + editing require different params so u will have to use switched or if statements in your channel form – stuartmcd69 Mar 21 '15 at 0:10
1

I ran into the same problem. After talking to EE support, they informed me I'd need to update the meta hidden field as well if I wanted to update the entry, instead of creating a new one.

The meta hidden field is an encrypted string representing the settings for the form. This includes the entry_id, so if the meta value isn't updated, it posts a new entry instead of updating.

A module could be written to do this, and I may do that at some point, but I ended up getting the meta value I needed by creating a template, passing it the newly-created entry_id (the one you want to edit) as a segment variable, asynchronously calling the template and grabbing the meta value from the returned HTML.

The template that';s called just contains a Channel Form tag pair with the entry_id parameter set to the entry_id you want to edit (the segment variable I passed in). When the template is called, it renders out a new form that contains the meta value you need.

At that point, all you have to do is grab the meta value from the returned HTML and replace your initial form's meta hidden field value with the new one. Once you've done that, you'll be able to update the form.

It definitely feels a little wonky to do it this way, but it got the job done. A module would be cleaner, but since I was already working with AJAX it didn't seem egregious to handle the meta update this way.

|improve this answer|||||

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.