# Use jQuery to add a connection to a field

**URL:** <https://forums.knack.com/t/use-jquery-to-add-a-connection-to-a-field/10247>\
**Category:** API & Custom Code\
**Created:** [July 22, 2015, 9:23pm UTC](https://forums.knack.com/t/use-jquery-to-add-a-connection-to-a-field/10247 "2015-07-22T21:23:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MauricioGo01617](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MauricioGo01617](https://forums.knack.com/u/MauricioGo01617)\
**Post date:** [July 22, 2015, 9:23pm UTC](https://forums.knack.com/t/use-jquery-to-add-a-connection-to-a-field/10247/1 "2015-07-22T21:23:16Z")

</div>

Hello.&nbsp;

Im using PHP to list records.

Each record has a button.

Once the user clicks a button I need to add a new user with a connection **to the record that the user clicked**.

Right now, Im using this: &nbsp;

```
$('#button').click(function(event) {
	// replace XXX with your application ID
	$.ajax({
	url: "https://api.knackhq.com/v1/objects/object_6/records",
	type: "POST",
	headers: {"X-Knack-Application-Id": "myID", "X-Knack-REST-API-Key": "myKey"},
	data: JSON.stringify({
	field_37 : "name@example.com"
	}),
	success: function(response) { 
	alert('Record Added!');
	}
}); });
```

&nbsp; &nbsp;&nbsp;

The user gets created but the email field (field\_37) is empty.

So I have two questions:

What am I doing wrong to enter the email?

How can I enter a new connection to the record the user clicked?

Thank you.

---

<div class="post-metadata">

**Author:** ![MauricioGo01617](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MauricioGo01617](https://forums.knack.com/u/MauricioGo01617)\
**Post date:** [July 22, 2015, 10:54pm UTC](https://forums.knack.com/t/use-jquery-to-add-a-connection-to-a-field/10247/2 "2015-07-22T22:54:10Z")

</div>

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.&nbsp;

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!');
}
});

});&nbsp;</pre><p>&nbsp; &nbsp;</p>
```
```

---

<div class="post-metadata">

**Author:** ![MauricioGo01617](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MauricioGo01617](https://forums.knack.com/u/MauricioGo01617)\
**Post date:** [July 22, 2015, 9:39pm UTC](https://forums.knack.com/t/use-jquery-to-add-a-connection-to-a-field/10247/3 "2015-07-22T21:39:32Z")

</div>

Hello Nicolas!&nbsp;

Thank you. You saved me tons of extra time.

---

<div class="post-metadata">

**Author:** ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://forums.knack.com/u/Nic)\
**Post date:** [July 22, 2015, 9:24pm UTC](https://forums.knack.com/t/use-jquery-to-add-a-connection-to-a-field/10247/4 "2015-07-22T21:24:50Z")

</div>

Hi Mauricio,

Knack is expecting an **Object** &nbsp;for the email field -- take a look here:&nbsp;[http://helpdesk.knackhq.com/solution/categories/5000053588/folders/5000083636/articles/5000446405-field-types#email](http://helpdesk.knackhq.com/solution/categories/5000053588/folders/5000083636/articles/5000446405-field-types#email)
