2

I'm using the Google Maps API to get a users location on the client side...I want to then send this to the server side to do some processing and populate a select box (with local addresses).

Whats the best way of doing this? Prefereably I would like to press a button and populate the box using ajax.

Thanks!

1 Answer 1

2

Here is an example of how to pass a Javascript value (your user location) to the bean with Ajax.

The location will be stored in the <h:inputHidden> field. The execute attribute of <f:ajax> specifies the value(s) that will be sent to the server to process.

<h:form id="formname">
    <h:inputHidden value="#{indexBean.value}" id="hvalue" />
    <h:commandButton value="action" onclick="getLocation();" action="#{indexBean.doSome}">
        <f:ajax execute="hvalue" render="output" />
    </h:commandButton>
    <h:outputText value="#{indexBean.output}" id="output" />
</h:form>

Before the Ajax request is sent the onclick attribute of the <h:commandButton> specifies the Javascript function to execute. Your Javascript function should set the value in your hidden field.

function getLocation() {        
    document.getElementById("formname:hvalue").value = "TEST";                       
}

The render attribue of <f:ajax> specifies what to update on the client side. In this case its an <h:outputText> in your case it will be your <h:selectOneMenu>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.