0

I have to pass the selected picklist value by user from apex form using <select> and <options> html tag to a standard controllers picklist field.

I am unable to get the values from Page to controller. Please suggest how this can be done.

VF Page

<select class="form-control form-group">
    <option value="">--None--</option>
    <option value="">A</option>
    <option value="">B</option>
    <option value="">C</option>
</select>

<apex:actionFunction name="dfunction" action="{!receiveInput}">
    <apex:param id="dname" name="type" value=""/>
</apex:actionFunction>

<!--Some <button> with onCLick= myfoo() in vf page-->
//Javascript
myfoo(){
    var n3 = document.getElementById("type").value;
    dfunction(n3);
}

Controller

//Controller
public void receiveInput() {
    String input = Apexpages.currentPage().getParameters().get('type');
}

EDIT1-

var n3 = document.getElementById("type").value; dfunction(n3); This seems to get the value of option properly but I am not able to get it in my controller.

1

You need to make following changes in the markup, JS and the controller:

VF Page

<select id="inputSelect" class="form-control form-group">
    <option value="">--None--</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
</select>

<apex:actionFunction name="dfunction" action="{!receiveInput}">
    <apex:param id="dname" name="type" value=""/>
</apex:actionFunction>

<!--Some <button> with onCLick= myfoo() in vf page-->
//Javascript
myfoo(){
    var n3 = document.getElementById("inputSelect").value;
    dfunction(n3);
}

Controller

//Controller
public void receiveInput() {
    String input = Apexpages.currentPage().getParameters().get('type');
}
| improve this answer | |
0

Edit 1

There should be id attribute on your select tag

 <select class="form-control form-group" id="selectInputId">
     <option value="">--None--</option>
     <option value="A">A</option>
     <option value="B">B</option>
     <option value="C">C</option>
  </select>

Edit 2

myfoo(){
        var n3 = document.getElementById("selectInputId").value;
            dfunction(n3);
     }

I feel this will make things work smoothly.

| improve this answer | |
  • Corrected..but still its coming as null. :( – maverick May 23 '16 at 19:22
  • Are you sure your Javascript worked correctly. You can alert n3 to verify if value is properly coming to JavaScript function foo .Also check if you have value parameter set on options. See my revised answer. – Mukesh Verma May 24 '16 at 3:46
  • var v = n3.options[n3.selectedIndex].value; func(v); is giving the value correctly in alert. <apex:actionFunction name="func" action="{!receiveInput}"> <apex:param id="dname" name="type" value=""/> </apex:actionFunction> But I am not getting it in controller: String input = Apexpages.currentPage().getParameters().get('type');. Its still coming as null. – maverick May 24 '16 at 9:29
-1

You will need to use a public string in your controller to save the value in:

public String positions {get; set;}

and after that you can use a salesforce selectlist to make the select list and link that value to the one in the controller:

<apex:selectList id="Positions" value="{!positions}" size="1">

Info about apex selectlist: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm

So when a form is submitted, the value will be given to the controller.

| improve this answer | |
  • I am bound to use HTML <select> tag. – maverick May 23 '16 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.