I have created a controller called myPlugin_Controller.php
This is the following code inside it.
<?php
class myPluginController extends BaseController
{
public function actionSave()
{
$this->requirePostRequest();
// get all the post variables
$elementId = craft()->request->getPost('elementId');
$userId = craft()->request->getPost('userId');
// set the name of the field you want to update
$fieldHandle = 'friends';
// based on the element and field vars, get the models for each
// using the elements and fields services
$element = craft()->elements->getElementById($elementId);
$field = craft()->fields->getFieldByHandle($fieldHandle);
// store the current ids into a variable
$currentIds = $element->{$field->handle}->ids();
// merge the new id into the current ids
$newIds = array_merge($currentIds, (array) $userId);
// save the new array of ids, using the relations service
craft()->relations->saveRelations($field, $element, $newIds);
// redirect to the redirect url
$this->redirectToPostedUrl();
}
}
?>
This is my code in an html file, it is submitting some data however it returns a 404 error.
<form method="post" accept-charset="UTF-8">
{{ getCsrfInput() }}
<input type="hidden" name="action" value="myPlugin/Save">
<input type="hidden" name="redirect" value="/success">
<input type="hidden" name="elementId" value="{{ user.id }}">
<input type="hidden" name="userId" value="{{ currentUser.id }}">
<button type="submit" >Submit</button>
</form>
i'm not too sure where its going wrong now.
Also if your wondering why its called user this is why {% set user = craft.users.username( craft.request.segment(2) ).first() %} Do you know where I am going wrong now any tips or help is all appreciated! :)
Still not working I have looked at craft cms documentation online i've followed your steps and there steps.
Is there anything you can still see that is wrong?