Jump to content

Select option from another page


Raul
 Share

Recommended Posts

Hello all, 

I have a contact-form with a select field as Subject.

The select field is defined as:

+=

subject1= THE FIRST Subject

subject2= THE SECOND Subject

Now I call the contact-fom from another page with a get parameter "subject" :


http://processwiresite.com/contact/?subject="subject1"

I have a Hook defined to process the form "onRender" and I have got the get parameter and sanitized it, etc.

Now the question is:

How can I select the option based on the get parameter ?

Thank you in advance

Raul

Link to comment
Share on other sites

Hi Raul,

Not sure if I completely understand your needs and it sounds like you know what you are doing so I might be missing your point, but is this what you are looking for?

<option name="subject" value="subject1" <?php echo ($input->get->subject == 'subject1' ? '' selected="selected"' : ''); ?>></option>

I am not sure how you are building the contact form and the select options, but likely you will be generating all of the option lines dynamically via a foreach, or maybe you are letting PW take care of it with something like $form = $modules->get('InputfieldForm');.

Maybe something like this would work for you:

foreach($subjects as $subject) {
	$selected = $subject->name == $input->whitelist->subject ? " selected='selected' " : '';	
        echo "<option$selected value='{$subject->name}'>{$subject->title}</option>"; 
}

Hopefully something there will be of use. If not, let us know in more details exactly what you need to do and I am sure someone can help.

Link to comment
Share on other sites

Hi Adrian, 

Thank your for your answer.

This would work if I would have made the formular.

The contact- form has been made with Formbuilder.  Also I don't care of building. I can use only Hooks to modify the results.

The functionality :

1.- I have a page with a link (http://processwire/fompage?subject=contact

2.- I click the link and i get into the form page.

3.- On the form page I have a select with the options

<option value="contact">Contact</option>

<option value="press">Press</option>

NOTE: This code is generated by Formbuilder

4.- When I come into the form page, I get the parameter "subject" and try to select te appropiate value of the select field.

I this case I must select the first value.

Sorry but I can't say more details because there aren't more details to say.

Thank you in advance

Raul

Link to comment
Share on other sites

This should be in the FormBuilder support forum. We can't know you use FormBuilder...

I have a Hook defined to process the form "onRender" and I have got the get parameter and sanitized it, etc.

Maybe share you're code with us so we can see what you got? Have you tried something that doesn't work? 

Without testing or knowing, if you have the form object inside the hook function you should be able to

$form->get("fieldname")->value = "subject1";
Link to comment
Share on other sites

Hi Soma, 

Than you for your answer.

It works.

I want to do it too much complicated.

On the other hand:

I didn't think it's a question for the Fombuilder support forum. 

From my point of view It's only a question of "Select an option of an InputfieldSelect depending on  an URL parameter".

If the field is in a fom or not is not relevant for this question.

I do apologize if I'm mistaken.

Thank you very much

Raul

Link to comment
Share on other sites

I understand your view.

From your first post it's not clear at all, what form... what hook.. are you talking about? You can't assume everybody knows what tools and code you're using, not everybody uses FormBuilder, not everybody uses PW fields for forms. 

See? Adrian already spent his time and answered with what most would think from your question... after all it wasn't what you looked for.

Of course FormBuilder is using InputfieldSelect, and is a general field. So setting a value to the field applies generally, assuming it's single select (multiple select would be different story again). Now here you're somewhat in the context of FormBuilder and you need a special hook in a special place to interact with the form, that is special to FormBuilder.

Further, now that you have it working with FormBuilder how about sharing your solution?

Link to comment
Share on other sites

This capability is actually built into FormBuilder. When editing your form, go to the "settings" menu and check the box for "Preset field values from GET variables". Then on your page that has the form, just specify the GET variable you want to populate in the URL: /path/to/form/page/?subject=subject1

When you view the form (embedded with option A or B) the value will be pre-selected. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...