You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Kanade, Sayali" <Sa...@gs.com> on 2008/10/27 19:47:03 UTC

Struts2 : Calling AJAX function on button click and passing form parameters to the same

Hi All,

I am new to Struts 2, so please forgive me if this question is inane.

I have a very simple form which has some checkboxes and on the click of
the submit button of the form, I want to pass the values of the form to
an AJAX action.

############################CODE SNIPPET
STARTS#####################################################

<script type="text/javascript">
            function getResult(){
 
//document.getElementById("diaryRecords").setAttribute("href",
"/equipment.showHistory.action?serialNumber="+serialNumber);
                //alert('In Get result');
                buildValue = document.myForm.build.value;
                //alert('buildValue : '+buildValue);
                //dojo.event.topic.publish('/example/result.action',
buildValue);
	
document.myForm.hiddenbuild.value=buildValue;
	
//alert('document.myForm.hiddenbuild.value :
'+document.myForm.hiddenbuild.value)
            }
</script>

:
:

<s:form action="/example/homepage.action" method="post" name="myForm"
validate="true">
	                   :
				 :
			<td width="5%" >
                           <input type="checkbox" name="build" />
                       </td>
				:
				:	 
				<tr>
                        <td ><s:submit theme="ajax" targets="resultDiv"
notifyTopics="/example/result.action" formId="myForm"
href="%{ajaxResultUrl}" onclick="getResult()" /> </td>
						<td><s:reset
name="reset" /></td>
                    </tr>
                </table>
               </td>

            </tr>
			<tr>
				<td colspan="3">
                   <s:url id="ajaxResultUrl"
value="/example/result.action" >
                       <s:param name="build" value="%{'on'}" />
                    </s:url>
                    <s:div theme="ajax" id="resultDiv" formId="myForm"
loadingText="Loading Result..." listenTopics="/example/result.action"
href="%{ajaxResultUrl}" />
				</td>
			</tr>
			:
			:
</s:form>
######################################CODE SNIPPET
ENDS#####################################

Now, I have two problems in this:
1. I am unable to pass the form parameters to the action. As you can see
that the form submits to homepage.action, but I want the AJAX call to
load from result.action. I have passed the formId to the div, but it
doesn't get the form parameters in the request. To work around this, I
am passing the value of build (build is a checkbox) as a param and
referencing the URL on the div as href.
2. The problem with pass as href : The result.action executes on page
load, I want it to execute on the submit button click and then display
the result.

Ideally, when I click submit, I want these form elements to be submitted
to result.action and the result will be displayed.

The code snippet above submits the build value as 'on' to the form and
the result is displayed on page load.

Please help, I wasn't able to get any good code examples doing this, it
would help immensely if I could get that.

Thanks for reading,
Regards,
Sayali.
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 : Calling AJAX function on button click(ONLY)

Posted by "Kanade, Sayali" <Sa...@gs.com>.
Thanks for all your help!
I tried changing the href of the div on button click but that doesn't
work. How I finally managed to do this was by passing a hidden form
field to action. The action only executes when the form field has a set
value. So on page load the Action doesn't get this value and the div jsp
doesn't get it and displays nothing.

What I was doing wrong was that my Form had a name parameter, the form
must have an id parameter to pass form fields. Also, my Action class
also behaves like a form bean, it needs to have a getter and setter for
the form fields.

As I said, new to Struts 2 and got all confused.

Thanks again,
Regards,
Sayali.
 
-----Original Message-----
From: Roger [mailto:roger.varley@googlemail.com] 
Sent: Wednesday, October 29, 2008 7:56 AM
To: Struts Users Mailing List
Subject: Re: Struts2 : Calling AJAX function on button click(ONLY)

On Tuesday 28 October 2008 20:01:41 Kanade, Sayali wrote:
> Thanks for your reply, but not sure it answers my question.
>
> My question is: How can I prevent a div from refreshing its contents
on
> page load? The div should only be refreshed on button click.
>
> My problem is that, my div loads on page load. It also reloads on
submit
> button click, but it should ONLY load on submit click, not on page
load.
>
> The action that executes in my div has to be only executed after the
user
> selects a checkbox and clicks submit.
>
> Please help, I have browsed for this answer for hours!!!
>

You could tryusing a Javascript function to set your div to
"display:none" on 
page load and re-set it when you want to display it.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : Calling AJAX function on button click(ONLY)

Posted by Roger <ro...@googlemail.com>.
On Tuesday 28 October 2008 20:01:41 Kanade, Sayali wrote:
> Thanks for your reply, but not sure it answers my question.
>
> My question is: How can I prevent a div from refreshing its contents on
> page load? The div should only be refreshed on button click.
>
> My problem is that, my div loads on page load. It also reloads on submit
> button click, but it should ONLY load on submit click, not on page load.
>
> The action that executes in my div has to be only executed after the user
> selects a checkbox and clicks submit.
>
> Please help, I have browsed for this answer for hours!!!
>

You could tryusing a Javascript function to set your div to "display:none" on 
page load and re-set it when you want to display it.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: SPAM WARNING!: RE: Struts2 : Calling AJAX function on button click(ONLY)

Posted by Steven Yang <ke...@gmail.com>.
Hi
on thing i noticed is that you have a notifyTopics attribute on you submit
tag which i dont think is necessary and used wrong.
i havent used dojo for a long time, so i am not sure if they changed any api
or settings.
but just a reminder you might need to take a look.

you dont have to specify href on your div.
you can change it or assign a href in your onclick function like
dojo.byId("resultDiv").href = "/example/result.action";

see if this help

RE: SPAM WARNING!: RE: Struts2 : Calling AJAX function on button click(ONLY)

Posted by "Kanade, Sayali" <Sa...@gs.com>.
Hi,

Still stuck here, can anyone please provide examples of populating a div by calling an action after page load on button click??
Is this supposed to be this hard?? :(

Any help would be appreciated.

Thanks!
Regards,
Sayali.

-----Original Message-----
From: Kanade, Sayali 
Sent: Tuesday, October 28, 2008 2:02 PM
To: Mileidys Gonzalez Prieto; user@struts.apache.org
Subject: SPAM WARNING!: RE: Struts2 : Calling AJAX function on button click(ONLY) 

Thanks for your reply, but not sure it answers my question.

My question is: How can I prevent a div from refreshing its contents on page load? The div should only be refreshed on button click.

My problem is that, my div loads on page load. It also reloads on submit button click, but it should ONLY load on submit click, not on page load.

The action that executes in my div has to be only executed after the user selects a checkbox and clicks submit.

Please help, I have browsed for this answer for hours!!!

Regards,
Sayali.
 
-----Original Message-----
From: Mileidys Gonzalez Prieto [mailto:mileidys.gonzalez@i-frontier.net] 
Sent: Tuesday, October 28, 2008 11:10 AM
To: Kanade, Sayali; user@struts.apache.org
Subject: RE: Struts2 : Calling AJAX function on button click and passing form parameters to the same

Hi,
I don't' know if I get the idea about what do u want to do, but I will try
to copy some codes about how I resolve something similar, using AjaxTags

<ajax:htmlContent target="tabContent" source="sendAfs" doPost="true" 
	baseUrl="${pageContext.request.contextPath}/relatie/tabAfspraken.do"

	parameters="id=${rsKlantId},action=saveAfs,${paramsForm}" />

in this case the id for my button is sendAfs and is sending my post
everything that is in the form. In my case is really a lot of fields and is
difficult to copy one by one, then for me was better to create a form that
will get all the fields:

Fields:
	public String filterArtikelNr;
	public String filterArtikel;
	public String filterSoort;
	public String filterMerk;
	public String filterType;
	public String filterVervallen;

Function for get an string with the parameters list in the form: 
paramaterName = {paramaterName}
This is the way for write the fields in Ajax
	public String getParams(){
		StringBuilder sb = new StringBuilder();
		Field[] f = this.getClass().getFields();
		for (Field field : f) {
			sb.append(field.getName() + "={" + field.getName() +
"},");
		}
		return sb.toString();
	}

Mileidys González Prieto
Consultant
i-Frontier
 
Henck Arronstraat 16 | P.O.Box 12858 | Paramaribo, Suriname (SA)
Office: (597)-424073 | Mobile: (597)-8810219
www.i-frontier.net
-----Original Message-----
From: Kanade, Sayali [mailto:Sayali.Kanade@gs.com] 
Sent: Tuesday, October 28, 2008 11:05 AM
To: user@struts.apache.org
Subject: RE: Struts2 : Calling AJAX function on button click and passing
form parameters to the same

Hi,

Can someone please help me out with the problem I mentioned below? I am
using Struts 2.0.11.2.

Any help will be greatly appreciated!!
Thanks!
Regards,
Sayali.
 
-----Original Message-----
From: Kanade, Sayali 
Sent: Monday, October 27, 2008 2:47 PM
To: user@struts.apache.org
Subject: SPAM WARNING!: Struts2 : Calling AJAX function on button click
and passing form parameters to the same

Hi All,

I am new to Struts 2, so please forgive me if this question is inane.

I have a very simple form which has some checkboxes and on the click of
the submit button of the form, I want to pass the values of the form to
an AJAX action.

############################CODE SNIPPET
STARTS#####################################################

<script type="text/javascript">
            function getResult(){
 
//document.getElementById("diaryRecords").setAttribute("href",
"/equipment.showHistory.action?serialNumber="+serialNumber);
                //alert('In Get result');
                buildValue = document.myForm.build.value;
                //alert('buildValue : '+buildValue);
                //dojo.event.topic.publish('/example/result.action',
buildValue);
	
document.myForm.hiddenbuild.value=buildValue;
	
//alert('document.myForm.hiddenbuild.value :
'+document.myForm.hiddenbuild.value)
            }
</script>

:
:

<s:form action="/example/homepage.action" method="post" name="myForm"
validate="true">
	                   :
				 :
			<td width="5%" >
                           <input type="checkbox" name="build" />
                       </td>
				:
				:	 
				<tr>
                        <td ><s:submit theme="ajax" targets="resultDiv"
notifyTopics="/example/result.action" formId="myForm"
href="%{ajaxResultUrl}" onclick="getResult()" /> </td>
						<td><s:reset
name="reset" /></td>
                    </tr>
                </table>
               </td>

            </tr>
			<tr>
				<td colspan="3">
                   <s:url id="ajaxResultUrl"
value="/example/result.action" >
                       <s:param name="build" value="%{'on'}" />
                    </s:url>
                    <s:div theme="ajax" id="resultDiv" formId="myForm"
loadingText="Loading Result..." listenTopics="/example/result.action"
href="%{ajaxResultUrl}" />
				</td>
			</tr>
			:
			:
</s:form>
######################################CODE SNIPPET
ENDS#####################################

Now, I have two problems in this:
1. I am unable to pass the form parameters to the action. As you can see
that the form submits to homepage.action, but I want the AJAX call to
load from result.action. I have passed the formId to the div, but it
doesn't get the form parameters in the request. To work around this, I
am passing the value of build (build is a checkbox) as a param and
referencing the URL on the div as href.
2. The problem with pass as href : The result.action executes on page
load, I want it to execute on the submit button click and then display
the result.

Ideally, when I click submit, I want these form elements to be submitted
to result.action and the result will be displayed.

The code snippet above submits the build value as 'on' to the form and
the result is displayed on page load.

Please help, I wasn't able to get any good code examples doing this, it
would help immensely if I could get that.

Thanks for reading,
Regards,
Sayali.
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

WARNING! The attached email has been filtered as possible spam. Please
review at your own discretion. 

For additional info: <<http://infosec.web.gs.com/faqs/spam.shtml>>

Cogentrix users go to http://cgxapp/mailinfo for further instructions.

TIP: Don't unsubscribe from spam e-mail.  Unsubscribing confirms to the
spammer that a guessed e-mail address is real, and may cause you to
receive even more spam.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

WARNING! The attached email has been filtered as possible spam. Please review at your own discretion. 

For additional info: <<http://infosec.web.gs.com/faqs/spam.shtml>>

Cogentrix users go to http://cgxapp/mailinfo for further instructions.

TIP: Don't unsubscribe from spam e-mail.  Unsubscribing confirms to the spammer that a guessed e-mail address is real, and may cause you to receive even more spam.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 : Calling AJAX function on button click(ONLY)

Posted by "Kanade, Sayali" <Sa...@gs.com>.
Thanks for your reply, but not sure it answers my question.

My question is: How can I prevent a div from refreshing its contents on page load? The div should only be refreshed on button click.

My problem is that, my div loads on page load. It also reloads on submit button click, but it should ONLY load on submit click, not on page load.

The action that executes in my div has to be only executed after the user selects a checkbox and clicks submit.

Please help, I have browsed for this answer for hours!!!

Regards,
Sayali.
 
-----Original Message-----
From: Mileidys Gonzalez Prieto [mailto:mileidys.gonzalez@i-frontier.net] 
Sent: Tuesday, October 28, 2008 11:10 AM
To: Kanade, Sayali; user@struts.apache.org
Subject: RE: Struts2 : Calling AJAX function on button click and passing form parameters to the same

Hi,
I don't' know if I get the idea about what do u want to do, but I will try
to copy some codes about how I resolve something similar, using AjaxTags

<ajax:htmlContent target="tabContent" source="sendAfs" doPost="true" 
	baseUrl="${pageContext.request.contextPath}/relatie/tabAfspraken.do"

	parameters="id=${rsKlantId},action=saveAfs,${paramsForm}" />

in this case the id for my button is sendAfs and is sending my post
everything that is in the form. In my case is really a lot of fields and is
difficult to copy one by one, then for me was better to create a form that
will get all the fields:

Fields:
	public String filterArtikelNr;
	public String filterArtikel;
	public String filterSoort;
	public String filterMerk;
	public String filterType;
	public String filterVervallen;

Function for get an string with the parameters list in the form: 
paramaterName = {paramaterName}
This is the way for write the fields in Ajax
	public String getParams(){
		StringBuilder sb = new StringBuilder();
		Field[] f = this.getClass().getFields();
		for (Field field : f) {
			sb.append(field.getName() + "={" + field.getName() +
"},");
		}
		return sb.toString();
	}

Mileidys González Prieto
Consultant
i-Frontier
 
Henck Arronstraat 16 | P.O.Box 12858 | Paramaribo, Suriname (SA)
Office: (597)-424073 | Mobile: (597)-8810219
www.i-frontier.net
-----Original Message-----
From: Kanade, Sayali [mailto:Sayali.Kanade@gs.com] 
Sent: Tuesday, October 28, 2008 11:05 AM
To: user@struts.apache.org
Subject: RE: Struts2 : Calling AJAX function on button click and passing
form parameters to the same

Hi,

Can someone please help me out with the problem I mentioned below? I am
using Struts 2.0.11.2.

Any help will be greatly appreciated!!
Thanks!
Regards,
Sayali.
 
-----Original Message-----
From: Kanade, Sayali 
Sent: Monday, October 27, 2008 2:47 PM
To: user@struts.apache.org
Subject: SPAM WARNING!: Struts2 : Calling AJAX function on button click
and passing form parameters to the same

Hi All,

I am new to Struts 2, so please forgive me if this question is inane.

I have a very simple form which has some checkboxes and on the click of
the submit button of the form, I want to pass the values of the form to
an AJAX action.

############################CODE SNIPPET
STARTS#####################################################

<script type="text/javascript">
            function getResult(){
 
//document.getElementById("diaryRecords").setAttribute("href",
"/equipment.showHistory.action?serialNumber="+serialNumber);
                //alert('In Get result');
                buildValue = document.myForm.build.value;
                //alert('buildValue : '+buildValue);
                //dojo.event.topic.publish('/example/result.action',
buildValue);
	
document.myForm.hiddenbuild.value=buildValue;
	
//alert('document.myForm.hiddenbuild.value :
'+document.myForm.hiddenbuild.value)
            }
</script>

:
:

<s:form action="/example/homepage.action" method="post" name="myForm"
validate="true">
	                   :
				 :
			<td width="5%" >
                           <input type="checkbox" name="build" />
                       </td>
				:
				:	 
				<tr>
                        <td ><s:submit theme="ajax" targets="resultDiv"
notifyTopics="/example/result.action" formId="myForm"
href="%{ajaxResultUrl}" onclick="getResult()" /> </td>
						<td><s:reset
name="reset" /></td>
                    </tr>
                </table>
               </td>

            </tr>
			<tr>
				<td colspan="3">
                   <s:url id="ajaxResultUrl"
value="/example/result.action" >
                       <s:param name="build" value="%{'on'}" />
                    </s:url>
                    <s:div theme="ajax" id="resultDiv" formId="myForm"
loadingText="Loading Result..." listenTopics="/example/result.action"
href="%{ajaxResultUrl}" />
				</td>
			</tr>
			:
			:
</s:form>
######################################CODE SNIPPET
ENDS#####################################

Now, I have two problems in this:
1. I am unable to pass the form parameters to the action. As you can see
that the form submits to homepage.action, but I want the AJAX call to
load from result.action. I have passed the formId to the div, but it
doesn't get the form parameters in the request. To work around this, I
am passing the value of build (build is a checkbox) as a param and
referencing the URL on the div as href.
2. The problem with pass as href : The result.action executes on page
load, I want it to execute on the submit button click and then display
the result.

Ideally, when I click submit, I want these form elements to be submitted
to result.action and the result will be displayed.

The code snippet above submits the build value as 'on' to the form and
the result is displayed on page load.

Please help, I wasn't able to get any good code examples doing this, it
would help immensely if I could get that.

Thanks for reading,
Regards,
Sayali.
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

WARNING! The attached email has been filtered as possible spam. Please
review at your own discretion. 

For additional info: <<http://infosec.web.gs.com/faqs/spam.shtml>>

Cogentrix users go to http://cgxapp/mailinfo for further instructions.

TIP: Don't unsubscribe from spam e-mail.  Unsubscribing confirms to the
spammer that a guessed e-mail address is real, and may cause you to
receive even more spam.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 : Calling AJAX function on button click and passing form parameters to the same

Posted by Mileidys Gonzalez Prieto <mi...@i-frontier.net>.
Hi,
I don’t' know if I get the idea about what do u want to do, but I will try
to copy some codes about how I resolve something similar, using AjaxTags

<ajax:htmlContent target="tabContent" source="sendAfs" doPost="true" 
	baseUrl="${pageContext.request.contextPath}/relatie/tabAfspraken.do"

	parameters="id=${rsKlantId},action=saveAfs,${paramsForm}" />

in this case the id for my button is sendAfs and is sending my post
everything that is in the form. In my case is really a lot of fields and is
difficult to copy one by one, then for me was better to create a form that
will get all the fields:

Fields:
	public String filterArtikelNr;
	public String filterArtikel;
	public String filterSoort;
	public String filterMerk;
	public String filterType;
	public String filterVervallen;

Function for get an string with the parameters list in the form: 
paramaterName = {paramaterName}
This is the way for write the fields in Ajax
	public String getParams(){
		StringBuilder sb = new StringBuilder();
		Field[] f = this.getClass().getFields();
		for (Field field : f) {
			sb.append(field.getName() + "={" + field.getName() +
"},");
		}
		return sb.toString();
	}

Mileidys González Prieto
Consultant
i-Frontier
 
Henck Arronstraat 16 | P.O.Box 12858 | Paramaribo, Suriname (SA)
Office: (597)-424073 | Mobile: (597)-8810219
www.i-frontier.net
-----Original Message-----
From: Kanade, Sayali [mailto:Sayali.Kanade@gs.com] 
Sent: Tuesday, October 28, 2008 11:05 AM
To: user@struts.apache.org
Subject: RE: Struts2 : Calling AJAX function on button click and passing
form parameters to the same

Hi,

Can someone please help me out with the problem I mentioned below? I am
using Struts 2.0.11.2.

Any help will be greatly appreciated!!
Thanks!
Regards,
Sayali.
 
-----Original Message-----
From: Kanade, Sayali 
Sent: Monday, October 27, 2008 2:47 PM
To: user@struts.apache.org
Subject: SPAM WARNING!: Struts2 : Calling AJAX function on button click
and passing form parameters to the same

Hi All,

I am new to Struts 2, so please forgive me if this question is inane.

I have a very simple form which has some checkboxes and on the click of
the submit button of the form, I want to pass the values of the form to
an AJAX action.

############################CODE SNIPPET
STARTS#####################################################

<script type="text/javascript">
            function getResult(){
 
//document.getElementById("diaryRecords").setAttribute("href",
"/equipment.showHistory.action?serialNumber="+serialNumber);
                //alert('In Get result');
                buildValue = document.myForm.build.value;
                //alert('buildValue : '+buildValue);
                //dojo.event.topic.publish('/example/result.action',
buildValue);
	
document.myForm.hiddenbuild.value=buildValue;
	
//alert('document.myForm.hiddenbuild.value :
'+document.myForm.hiddenbuild.value)
            }
</script>

:
:

<s:form action="/example/homepage.action" method="post" name="myForm"
validate="true">
	                   :
				 :
			<td width="5%" >
                           <input type="checkbox" name="build" />
                       </td>
				:
				:	 
				<tr>
                        <td ><s:submit theme="ajax" targets="resultDiv"
notifyTopics="/example/result.action" formId="myForm"
href="%{ajaxResultUrl}" onclick="getResult()" /> </td>
						<td><s:reset
name="reset" /></td>
                    </tr>
                </table>
               </td>

            </tr>
			<tr>
				<td colspan="3">
                   <s:url id="ajaxResultUrl"
value="/example/result.action" >
                       <s:param name="build" value="%{'on'}" />
                    </s:url>
                    <s:div theme="ajax" id="resultDiv" formId="myForm"
loadingText="Loading Result..." listenTopics="/example/result.action"
href="%{ajaxResultUrl}" />
				</td>
			</tr>
			:
			:
</s:form>
######################################CODE SNIPPET
ENDS#####################################

Now, I have two problems in this:
1. I am unable to pass the form parameters to the action. As you can see
that the form submits to homepage.action, but I want the AJAX call to
load from result.action. I have passed the formId to the div, but it
doesn't get the form parameters in the request. To work around this, I
am passing the value of build (build is a checkbox) as a param and
referencing the URL on the div as href.
2. The problem with pass as href : The result.action executes on page
load, I want it to execute on the submit button click and then display
the result.

Ideally, when I click submit, I want these form elements to be submitted
to result.action and the result will be displayed.

The code snippet above submits the build value as 'on' to the form and
the result is displayed on page load.

Please help, I wasn't able to get any good code examples doing this, it
would help immensely if I could get that.

Thanks for reading,
Regards,
Sayali.
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

WARNING! The attached email has been filtered as possible spam. Please
review at your own discretion. 

For additional info: <<http://infosec.web.gs.com/faqs/spam.shtml>>

Cogentrix users go to http://cgxapp/mailinfo for further instructions.

TIP: Don't unsubscribe from spam e-mail.  Unsubscribing confirms to the
spammer that a guessed e-mail address is real, and may cause you to
receive even more spam.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 : Calling AJAX function on button click and passing form parameters to the same

Posted by "Kanade, Sayali" <Sa...@gs.com>.
Hi,

Can someone please help me out with the problem I mentioned below? I am
using Struts 2.0.11.2.

Any help will be greatly appreciated!!
Thanks!
Regards,
Sayali.
 
-----Original Message-----
From: Kanade, Sayali 
Sent: Monday, October 27, 2008 2:47 PM
To: user@struts.apache.org
Subject: SPAM WARNING!: Struts2 : Calling AJAX function on button click
and passing form parameters to the same

Hi All,

I am new to Struts 2, so please forgive me if this question is inane.

I have a very simple form which has some checkboxes and on the click of
the submit button of the form, I want to pass the values of the form to
an AJAX action.

############################CODE SNIPPET
STARTS#####################################################

<script type="text/javascript">
            function getResult(){
 
//document.getElementById("diaryRecords").setAttribute("href",
"/equipment.showHistory.action?serialNumber="+serialNumber);
                //alert('In Get result');
                buildValue = document.myForm.build.value;
                //alert('buildValue : '+buildValue);
                //dojo.event.topic.publish('/example/result.action',
buildValue);
	
document.myForm.hiddenbuild.value=buildValue;
	
//alert('document.myForm.hiddenbuild.value :
'+document.myForm.hiddenbuild.value)
            }
</script>

:
:

<s:form action="/example/homepage.action" method="post" name="myForm"
validate="true">
	                   :
				 :
			<td width="5%" >
                           <input type="checkbox" name="build" />
                       </td>
				:
				:	 
				<tr>
                        <td ><s:submit theme="ajax" targets="resultDiv"
notifyTopics="/example/result.action" formId="myForm"
href="%{ajaxResultUrl}" onclick="getResult()" /> </td>
						<td><s:reset
name="reset" /></td>
                    </tr>
                </table>
               </td>

            </tr>
			<tr>
				<td colspan="3">
                   <s:url id="ajaxResultUrl"
value="/example/result.action" >
                       <s:param name="build" value="%{'on'}" />
                    </s:url>
                    <s:div theme="ajax" id="resultDiv" formId="myForm"
loadingText="Loading Result..." listenTopics="/example/result.action"
href="%{ajaxResultUrl}" />
				</td>
			</tr>
			:
			:
</s:form>
######################################CODE SNIPPET
ENDS#####################################

Now, I have two problems in this:
1. I am unable to pass the form parameters to the action. As you can see
that the form submits to homepage.action, but I want the AJAX call to
load from result.action. I have passed the formId to the div, but it
doesn't get the form parameters in the request. To work around this, I
am passing the value of build (build is a checkbox) as a param and
referencing the URL on the div as href.
2. The problem with pass as href : The result.action executes on page
load, I want it to execute on the submit button click and then display
the result.

Ideally, when I click submit, I want these form elements to be submitted
to result.action and the result will be displayed.

The code snippet above submits the build value as 'on' to the form and
the result is displayed on page load.

Please help, I wasn't able to get any good code examples doing this, it
would help immensely if I could get that.

Thanks for reading,
Regards,
Sayali.
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

WARNING! The attached email has been filtered as possible spam. Please
review at your own discretion. 

For additional info: <<http://infosec.web.gs.com/faqs/spam.shtml>>

Cogentrix users go to http://cgxapp/mailinfo for further instructions.

TIP: Don't unsubscribe from spam e-mail.  Unsubscribing confirms to the
spammer that a guessed e-mail address is real, and may cause you to
receive even more spam.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org