<p>After some debugging I found, what I did wrong:
I had for some reason the wrong groupID (checked that in the backend and DB) as well as <code>title</code> needs to be present in the passed data and is the name of the tag itself.</p>

CC BY-SA 3.0
2016-11-08T21:35:04.183Z
2016-11-08T21:35:04.183Z
Eternal Black
<p>I pretty much followed <a href="https://craftcms.stackexchange.com/questions/1171/how-to-add-a-tags-field-to-an-entry-form?rq=1">this</a> and the answer there really helped me a lot so far. Currently I'm stuck with either a <code>500 (Internal Server Error)</code> or a <code>400 (Bad Request)</code> Error.
My <code>craft/storage/runtime/logs/craft.log</code> read the following:</p>

<p>For <code>400</code></p>

<pre><code>[error] [exception.Craft\HttpException.400] Craft\HttpException: POST param “title” doesn’t exist. in XXX/craft/app/services/HttpRequestService.php:509
Stack trace:
0 XXX/craft/app/controllers/TagsController.php(235): Craft\HttpRequestService-&gt;getRequiredPost('title')
1 XXX/craft/app/framework/web/actions/CInlineAction.php(49): Craft\TagsController-&gt;actionCreateTag()
2 XXX/craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams(Array)
3 XXX/craft/app/framework/web/CController.php(286): CController-&gt;runAction(Object(CInlineAction))
4 XXX/craft/app/framework/web/CController.php(265): CController-&gt;runActionWithFilters(Object(CInlineAction), Array)
5 XXX/craft/app/framework/web/CWebApplication.php(282): CController-&gt;run('createTag')
6 XXX/craft/app/etc/web/WebApp.php(818): CWebApplication-&gt;runController('tags/createTag')
7 XXX/craft/app/etc/web/WebApp.php(288): Craft\WebApp-&gt;_processActionRequest()
8 XXXcraft/app/framework/base/CApplication.php(185): Craft\WebApp-&gt;processRequest()
9 XXX/craft/app/index.php(62): CApplication-&gt;run()
10 XXX/index.php(19): require_once('/var/www/vhosts...')
11 {main}
REQUEST_URI=/actions/tags/createTag
HTTP_REFERER=XXX
---
</code></pre>

<p>When I add <code>title: "Tags"</code> to the <code>POST data</code> it results in the <code>500</code> with following error:</p>

<pre><code>[error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`craft_`.`craft_tags`, CONSTRAINT `craft_tags_groupId_fk` FOREIGN KEY (`groupId`) REFERENCES `craft_taggroups` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_tags` (`uid`, `groupId`, `id`, `dateUpdated`, `dateCreated`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4).
2016/11/03 22:16:01 [error] [exception.CDbException] CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`craft_`.`craft_tags`, CONSTRAINT `craft_tags_groupId_fk` FOREIGN KEY (`groupId`) REFERENCES `craft_taggroups` (`id`) ON DELETE CASCADE) in XXX/craft/app/framework/db/CDbCommand.php:358
Stack trace:
#0 XXX/craft/app/framework/db/ar/CActiveRecord.php(1081): CDbCommand-&gt;execute()
#1 XXX/craft/app/framework/db/ar/CActiveRecord.php(810): CActiveRecord-&gt;insert(NULL)
#2 XXX/craft/app/services/TagsService.php(396): CActiveRecord-&gt;save(false)
#3 XXX/craft/app/controllers/TagsController.php(237): Craft\TagsService-&gt;saveTag(Object(Craft\TagModel))
#4 XXX/craft/app/framework/web/actions/CInlineAction.php(49): Craft\TagsController-&gt;actionCreateTag()
#5 XXX/craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams(Array)
#6 XXX/craft/app/framework/web/CController.php(286): CController-&gt;runAction(Object(CInlineAction))
#7 XXX/craft/app/framework/web/CController.php(265): CController-&gt;runActionWithFilters(Object(CInlineAction), Array)
#8 XXX/craft/app/framework/web/CWebApplication.php(282): CController-&gt;run('createTag')
#9 XXX/craft/app/etc/web/WebApp.php(818): CWebApplication-&gt;runController('tags/createTag')
#10 XXX/craft/app/etc/web/WebApp.php(288): Craft\WebApp-&gt;_processActionRequest()
#11 XXX/craft/app/framework/base/CApplication.php(185): Craft\WebApp-&gt;processRequest()
#12 XXX/craft/app/index.php(62): CApplication-&gt;run()
#13 XXX/index.php(19): require_once('/var/www/vhosts...')
#14 {main}
REQUEST_URI=/actions/tags/createTag
HTTP_REFERER=XXX
---
</code></pre>

<p>Any suggestions would be much appreciated. This is the JS code I'm using:</p>

<pre><code>$('#new-tag').click(function() {
    var name = prompt('What’s the new tag name?');
    if (!name) return;
    var data = {
        title: 'tags',
        groupId: 2,
        name: name
    };
    $.post('/actions/tags/createTag', data, function(response) {
        if (response.success) {
            $('&lt;label&gt;' +
            '&lt;input type="checkbox" name="fields[tags][]" value="'+response.id+'" checked="checked"/&gt; ' +
            name +
            '&lt;/label&gt;').appendTo($('#tags'));
        }
        else {
            alert('Unable to create that tag.');
        }
    });
});
</code></pre>

<form><entry><ajax><tags><frontend>
Tags via AJAX actionTrigger
# Tags via AJAX actionTrigger

(Asked by: username_0 on 2016-11-03)

I pretty much followed this and the answer there really helped me a lot so far. Currently I'm stuck with either a 500 (Internal Server Error) or a 400 (Bad Request) Error.
My craft/storage/runtime/logs/craft.log read the following:
For 400
[error] [exception.Craft\HttpException.400] Craft\HttpException: POST param “title” doesn’t exist. in XXX/craft/app/services/HttpRequestService.php:509
Stack trace:
0 XXX/craft/app/controllers/TagsController.php(235): Craft\HttpRequestService->getRequiredPost('title')
1 XXX/craft/app/framework/web/actions/CInlineAction.php(49): Craft\TagsController->actionCreateTag()
2 XXX/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)
3 XXX/craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))
4 XXX/craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
5 XXX/craft/app/framework/web/CWebApplication.php(282): CController->run('createTag')
6 XXX/craft/app/etc/web/WebApp.php(818): CWebApplication->runController('tags/createTag')
7 XXX/craft/app/etc/web/WebApp.php(288): Craft\WebApp->_processActionRequest()
8 XXXcraft/app/framework/base/CApplication.php(185): Craft\WebApp->processRequest()
9 XXX/craft/app/index.php(62): CApplication->run()
10 XXX/index.php(19): require_once('/var/www/vhosts...')
11 {main}
REQUEST_URI=/actions/tags/createTag
HTTP_REFERER=XXX
---

When I add title: "Tags" to the POST data it results in the 500 with following error:
[error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`craft_`.`craft_tags`, CONSTRAINT `craft_tags_groupId_fk` FOREIGN KEY (`groupId`) REFERENCES `craft_taggroups` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_tags` (`uid`, `groupId`, `id`, `dateUpdated`, `dateCreated`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4).
2016/11/03 22:16:01 [error] [exception.CDbException] CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`craft_`.`craft_tags`, CONSTRAINT `craft_tags_groupId_fk` FOREIGN KEY (`groupId`) REFERENCES `craft_taggroups` (`id`) ON DELETE CASCADE) in XXX/craft/app/framework/db/CDbCommand.php:358
Stack trace:
#0 XXX/craft/app/framework/db/ar/CActiveRecord.php(1081): CDbCommand->execute()
#1 XXX/craft/app/framework/db/ar/CActiveRecord.php(810): CActiveRecord->insert(NULL)
#2 XXX/craft/app/services/TagsService.php(396): CActiveRecord->save(false)
#3 XXX/craft/app/controllers/TagsController.php(237): Craft\TagsService->saveTag(Object(Craft\TagModel))
#4 XXX/craft/app/framework/web/actions/CInlineAction.php(49): Craft\TagsController->actionCreateTag()
#5 XXX/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)
#6 XXX/craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))
#7 XXX/craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
#8 XXX/craft/app/framework/web/CWebApplication.php(282): CController->run('createTag')
#9 XXX/craft/app/etc/web/WebApp.php(818): CWebApplication->runController('tags/createTag')
#10 XXX/craft/app/etc/web/WebApp.php(288): Craft\WebApp->_processActionRequest()
#11 XXX/craft/app/framework/base/CApplication.php(185): Craft\WebApp->processRequest()
#12 XXX/craft/app/index.php(62): CApplication->run()
#13 XXX/index.php(19): require_once('/var/www/vhosts...')
#14 {main}
REQUEST_URI=/actions/tags/createTag
HTTP_REFERER=XXX
---

Any suggestions would be much appreciated. This is the JS code I'm using:
$('#new-tag').click(function() {
    var name = prompt('What’s the new tag name?');
    if (!name) return;
    var data = {
        title: 'tags',
        groupId: 2,
        name: name
    };
    $.post('/actions/tags/createTag', data, function(response) {
        if (response.success) {
            $('' +
            ' ' +
            name +
            '').appendTo($('#tags'));
        }
        else {
            alert('Unable to create that tag.');
        }
    });
});

**Tags:** form, entry, ajax, tags, frontend

---

## 1 answers

### Answer by username_0 on 2016-11-08. Score: 2 | Accepted answer

After some debugging I found, what I did wrong:
I had for some reason the wrong groupID (checked that in the backend and DB) as well as `title` needs to be present in the passed data and is the name of the tag itself.


---

