Skip Navigation

[Resolved] Need to set default value based on url parameter

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a one-to-many relationship set up. On the parent post I would like to display a link so my site Users can easily create a new child post for this parent post. I would like to automatically set the correct parent post in the Form, and I don't want to allow Users to change the selected parent.

Solution: Forms offers a New Child Post Form link. It will redirect your Users to a page with the New Child Post Form displayed, and the parent post automatically set. To hide the parent post select field, use CSS display:none or similar. Even though the field is not shown, the correct parent will be set based on the automated URL parameter.

Relevant Documentation:
https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1265711

I want to pre-fill a relationship field in a Toolset form using the url parameter in a View filter result.

I have a View search where users input a specific value, hit search, and the View result is displayed on a different page. From this Views search result page the users can fill out a form.

I need the relationship field default to be automatically populated by the result of the View. This may be possible with a URL parameter, but I would need the post ID from the result in that parameter, and I haven't found that as an option in Views.

Another option was somehow programmatically populating the default value of the relationship field using PHP, however, I can't see a way to insert PHP in the Toolset form.

What do you suggest?

-------------------------------

What is the link to your site? hidden link
The specific value you can input to test this search and see what I am trying to do is: 1234

#1265933

Okay you have a search View that produces some result on another page. Then from that result, you want to link to a Form that includes a post relationship field, which should be automatically populated by the ID of the result from the previous page.
- What type of post relationship are we talking about, is this a one-to-many or a many-to-many relationship?
- Have you already set up a Content Template to hold the Form?
- Is the child form link approach not applicable for your case? Why not? https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/

#1268553

The relationship is one-to-many.

The reason the child form link doesn't work in this instance is a) we don't want users to have to input the same information twice, and b) we don't want users to be able to search other parent content, or accidentally attach the child form to another parent item that they don't own.

Maybe there is a better way to set this up? It would be fine if the user had to click on the parent item so they could fill out a child form, but I haven't figured out a way to make it work, even following the documentation you provided. It looks like the child form link requires the child content be created before the link can happen, right?

It would be better if the flow could work like this:

Search for vehicle (parent item) -> select vehicle and go to vehicle page -> fill out child form on that page like a complex comment form.

Am I making this too complicated, or is there a way to make that work?

#1268633

The reason the child form link doesn't work in this instance is a) we don't want users to have to input the same information twice, and b) we don't want users to be able to search other parent content, or accidentally attach the child form to another parent item that they don't own.
Okay I think the child form link is still the best way to go here. You can actually hide the parent input field in the Form so the User isn't aware of it, and it will still be set to save the appropriate parent. Basically you will edit the Form code and add some CSS to hide the parent select field and its container element. I can help with that if you'd like. The User won't have to select the parent post again, it will be preselected automatically based on a URL parameter (see below),

It looks like the child form link requires the child content be created before the link can happen, right?
No, in this case the parent post is created first. The child post link can be displayed somewhere on the parent post template or in a View of parent posts. The link redirects the User to a Form to create a new child post. In the link URL there will be a URL parameter that specifies the parent post (this is generated automatically when you insert the child post link). That URL parameter automatically sets the correct parent post in the Form. This field can be hidden from the User with a bit of CSS.

#1268639

It looks like the child form link requires the child content be created before the link can happen, right?
I should add that many-to-many relationships work like you are describing here. But since yours will be a one-to-many relationship, child post creation happens at the same time as the parent post link is established.

#1268801

My issue is resolved now. Thank you!

I was trying to add a Relationship Link form to the View, instead of just a Relationship Form. When I got the right form added it worked as expected. Thank you! This is working really well.