GravityForms: tw-sack.min – Uncaught SyntaxError: Unexpected identifier tw-sack.min.js:1

Here is what the error looks like in the Chrome Debugger Console:

Uncaught SyntaxError: Unexpected identifier tw-sack.min.js:1

GravityForms:

If you’r getting a problem with editing forms in GravityForms, or perhaps trying to add fields, you can take a look at the source code and you’ll see what happens – an AJAX request is made, but twsack does not like the response which looks something like:

EndAddField({"id": 1, "label": "Hidden Field", "adminLabel": "", "type": "hidden
", "isRequired": false, "size": "medium", "errorMessage": "", "inputs": null}, "
<li id='field_1' class='selectable gfield     gform_hidden' ><div class='gfield_
admin_icons'><div class='gfield_admin_header_title'>Hidden : Field ID 1</div><a class='field_delete_icon' id='gfield_delete_1' title='click to delete this field
' rel='nofollow' href='#' onclick='StartDeleteField(this); return false;'>Delete</a><a class='f
ield_duplicate_icon' id='gfield_duplicate_1' title='click to duplicate this fiel
d' rel='nofollow' href='#' onclick='StartDuplicateField(this); return false;'>Duplicate</a><a c
lass='field_edit_icon edit_icon_collapsed' title='click to edit this field'>Edit
</a></div><label class='gfield_label' for='input_1'>Hidden Field</label><input p
laceholder="Hidden Field"  name='input_1' id='input_1' type='text'  value='' dis
abled='disabled'/></li>");m

So the problem is actually that the response comes back with a Content-type of json, javascript, or html when it should be returned as text/plain – something like:

Content-Type: text/html; charset=UTF-8

To test, I edited the file form_detail.php and changed line 2178 (or rather inserted this line) – for gravity forms 1.7.5 (omit the line numbers):

2178 header('Content-type: text/html; charset=utf-8');
2179         die("EndAddField($field_json, \"$field_html\");");

Next I am going to look into changing the .htaccess file for this system to see about editing the default content type which I think is causing the root of the error.

Update

Looks like the content type was a fix but it is not the solution – at least not for when adding fields.

It looks like this is a problem when using GravityForms with Nginx and PHP with WordPress – or perhaps just something that could have been better coded in Gravity forms. The $field_html variable needs to be escaped with add_slashes – here is the fix with line numbers for the file wp-content/plugins/gravityforms/form_detail.php

2177         $field_html = GFFormDisplay::get_field($field, "", true);
2178         echo "EndAddField($field_json, \"".addslashes($field_html)."\");";
2179         die();

Related Posts:

  • No Related Posts
This entry was posted in Tech Tips and tagged , . Bookmark the permalink.

3 Responses to GravityForms: tw-sack.min – Uncaught SyntaxError: Unexpected identifier tw-sack.min.js:1

  1. Jason says:

    Actually — things are working. My issue was that I was trying to print out a gravity forms using the WordPress Types API — somehow the syntax got chewed up using:

    $gravityform = types_render_field(“gravityform”, array()); printf($gravityform);

    As soon as I switched to:

    $gravityform = get_post_meta($post->ID,’wpcf-gravityform’,TRUE); printf($gravityform);

    Note that with Types – when using the API (types_render_field) – you don’t use the wpcf- but when using the WordPress get_post_meta – you have to put the wpcf- in front.

    In other words – both of the examples above were outputting my Gravity Form, however the first one was mucking up the output of the javascript which was causing the AJAX to fail and give the Unexpected Identifier.

    Upon switching to the WordPress method of get_post_meta – the GravityForms shortcode was output the way GravityForms intended it to be, and the Ajax worked as expected.

  2. Jason says:

    I’m using Nginx and PHP-FM (PHP 5.4.24 )

    I’m using GravityForms 1.8.4 – the //Ajax calls section is now down on line 2235 and looks a little different.

    I tried making the edits you mention – but I’m still getting the Unexpected Identifier error — any update on how to get this to work with the updates in this section – or am I editing the wrong thing:

    Current file: //Ajax calls public static function add_field(){ check_ajax_referer(“rg_add_field”, “rg_add_field”); $field_json = stripslashes_deep($_POST[“field”]); $field = GFCommon::json_decode($field_json, true);

        require_once(GFCommon::get_base_path() . "/form_display.php");
        $field_html = GFFormDisplay::get_field($field, "", true);
        $field_html_json = json_encode($field_html);
    
        die("EndAddField($field_json, " . $field_html_json . ");");
    }
    

    I tried:

    //Ajax calls public static function add_field(){ check_ajax_referer(“rg_add_field”, “rg_add_field”); $field_json = stripslashes_deep($_POST[“field”]); $field = GFCommon::json_decode($field_json, true);

        require_once(GFCommon::get_base_path() . "/form_display.php");
        $field_html = GFFormDisplay::get_field($field, "", true);
        echo "EndAddField($field_json, \"".addslashes($field_html)."\");";
       die();
    }
    

    Thanks for your help!

  3. Brent says:

    Thanks for publishing this, just saved me potentially hours in debugging. Have you shared this with Rocket Genius?

    When it shows up, it’s quite a catastrophic bug.

Leave a Reply

Your email address will not be published. Required fields are marked *