Use jQuery to add a connection to a field

Answered

Comments

3 comments

  • Avatar
    Nic

    Hi Mauricio,

    Knack is expecting an Object for the email field -- take a look here: http://helpdesk.knackhq.com/solution/categories/5000053588/folders/5000083636/articles/5000446405-field-types#email

  • Avatar
    Mauricio González

    Hello Nicolas! 

    Thank you. You saved me tons of extra time.

  • Avatar
    Mauricio González

    One extra step, which I will ask in another topic, is how to use the logged in user to add the connection to his profile. 

    For anyone wanting to add a user with values using ajax, the basic code is this:

    //execute this when user clicks the button
    	$('.button').click(function(event) {
    	
    	// use the following data.
    	var data = {
    		field_37 : { 
    			'email' : 'example@example.com',	//new user email to be inserted in field 37
    			'label' : 'example'
    		},
    		field_41 : [55a1b550b2847881712431fe] //this is the id of the record you want to add to the new user inside of connection field_41
    	}
    	
    	// insert the record
    	// this uses the PAGE API access to use an existing form to insert the record.
    	
    	// replace XXX with your application ID
    	$.ajax({
    	url: "https://api.knackhq.com/v1/objects/object_6/records",
    	type: "POST",
    	headers: {"X-Knack-Application-Id": "xxx", "X-Knack-REST-API-Key": "xxx"},
    	data: data,
    	success: function(response) { 
    	alert('Record Added!');
    	}
    	});
    
    	}); 

       

Please sign in to leave a comment.