[SalesForce] How to pass string value from javascript to controller

I am using javaScript in Visualforce page to set the value of String variable. How can I pass this updated value to the controler?

  function saveFunctionality()
        { document.getElementById('VWValveSearch:VWSiteTemplate:frm1:pressureClass').value = 'Please have this value';
                   validateSaveInputFields();     
            }           

 <apex:actionFunction id="validateSaveInputFields" name="validateSaveInputFields" action="{!btnAddToProject}"  reRender="dummy" > </apex:actionFunction>
        </apex:form>
        <apex:actionFunction id="validateSaveInputFields" name="validateSaveInputFields" action="{!btnAddToProject}"  reRender="dummy" > 
</apex:actionFunction>

The reason why I am using javascript is because the strPressureClass is a string variable and when I click on save button. Sometimes it has value or sometime it has Null value.

Best Answer

To bind a variable to a formfield you can add the variablename to the value using the following syntax: {!variableName}.

This would be an example:

Visualforce Page:

<apex:inputHidden id="returnString" value="{!returnString}" />
<apex:commandButton onclick="return functionToSetReturnString();" value="Submit" action="{!actionInController}"></apex:commandButton>

Apex Controller:

public String returnString { get; set; }
public void actionInController () {
    // Read your returnString here using this.returnString
}

JavaScript code in Visualforce Page for example in functionToSetReturnString (replace with the id of your apex-form):

document.getElementById('{!$Component.idOfYourForm.returnString}').value = "Your String to pass to the controller";