Question

I'm breaking my head trying to call a js function from a button element inside a form, here is my code:

<% 
PortletPreferences prefs = renderRequest.getPreferences(); 
String employee = (String)prefs.getValue("name", "New Employee");  
%>

<portlet:actionURL var="callURL" windowState="<%=LiferayWindowState.EXCLUSIVE.toString() %>" />

<script type="text/javascript"> 
    Liferay.provide(window, 'insertEmployee',
    function () 
        {
            var A = AUI();
            var url = 'http://localhost:8080/c/portal/json_Service';
            A.io.request(url,
                {
                    method:'POST',
                    data:
                    {
                        serviceClassName:   'com.liferay.test.service.TrabajadorServiceUtil',
                        serviceMethodName:  'create',
                        servletContextName: 'TrabajadorPlugin-portlet',
                        serviceParameters:  '[param]',
                    },
                    headers: {'Content-Type':'charset=utf-8'},
                    on:
                    {
                        success: function()
                        {
                            alert("success " + responseData.name);
                        }
                    },
                    form: {id: 'postForm'},
                    xdr: {dataType: 'json'}
                });
        },
        ['aui-io']
    );
</script>

<div>
    <aui:form name="postForm" id="postForm" method="post" onSubmit="insertEmployee();">
        <input type="text" name="param" value="<%=employee %>"/>
        <input type="submit" value="Submit"/>
    </aui:form>
</div>

I'm not using an java class, thus I'm not using the portlet:actionURL either.

My intention is to call "insertEmployee()" when clicking the 'Submit' button, but it's only sending the param inserted by the user inside the text field. I've tried to put the 'onsubmit' also in the submit input, but the same result is given.

If you could help me or guide me to solve issue it would be so great! I'm not finding good information/tuts on the internet and I'm not sure where is the problem or what else I need to know.

Thanks a lot in advance!

Était-ce utile?

La solution

I just need:

<aui:script>
window.functionName = function ()  
{
//code
};
</aui:script>

and call it from:

<aui:form name="myform" action="javascript:functionName();">
<aui:input type="submit" name="Submit" value="Update"/>
</aui:form>

and the function is being called from the form tag.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top