<html: image directly calling struts action class - javascript

I want to call submitformFinal(this.form); which is a client side validation.But the problem is that html:image property directly calls struts, so the server side validation takes place before calling the submitformFinal(this.form).
If submitformFinal fails , server side validations should not take place ,but in this case html:image directly calls the struts action classs.... any suggestions?
Right now my code is:-<html:image
src='<%=contextPath + "/img/save_orange.gif"%>'
onclick="javascript:submitformFinal(this.form); "/>

What about returning the value from submitformFinal():
<html:image src='<%=contextPath + "/img/save_orange.gif"%>'
onclick="return submitformFinal(this.form);"/>
Now if submitformFinal() returns false it wont submit, if it returns true then it will be submitted.
Update :
After seeing your next problem, here is my thoughts.
1) You should not use <html:image> for submitting the form. <html:submit> is the correct submit button tag. I will recommend to change the <html:image> to <html:submit>. U can add the image to the button by styleId, styleClass attributes. No need to call onClick on the button. It will just submit your form when you click it.
2) Now add onSubmit on the <html:form> tag like this:
<html:form action="someAction.do" onSubmit="return submitformFinal();">
This will work like as desired I guess.

Related

Action parameter of form wrong when changing it via jquery

I am encountering this strange behaviour with jquery and I can't seem to fix it.
Basically what is happening is I am trying to set the action parameter of a form to a specific URL before submitting it.
When I submit the form, for some reason the URL I set it to just gets appended to a different URL, which I guess is the URL that was in there before.
This is the code I am using to set the action parameter.
//clear action parameter
jQuery("form[name='form1']").attr('action','');
alert(forwardUrl);
//set the action parameter
jQuery("form[name='form1']").attr('action',forwardUrl);
//action parameter output
alert('action parameter attribute: ' + jQuery("form[name='form1']").attr('action'));
//submit form
jQuery("form[name='form1']").submit();
So for example for using the forwardUrl = "test.php"
the output of both alerts is test.php.
However when I run the code and the form is submitting it is POSTING the values to https://www.test.com/test.php.
I realize that it could be the standard behaviour to append to the url it is posted from. If that is the case how can I POST to a different URL that happens to be forwardUrl?
I also tried to use .prop() instead of .attr().
Thank you very much in advance!
Use absolute path, as mentioned by Rory:
baseUrl = "https://www.test2.com/"; // Domain you need to send the form to
forwardUrl = baseUrl + "test.php";
You can simply set the action when you click on the submit button with the help of onclick attribute
<input type='submit' value='Submit' onclick='this.form.action="forwardUrl";' />
or change your code to something like this, first provide a id to the form and use that id to set the action attribute
document.getElementById('formID').action = 'forwardUrl';
or by using form name as
document.getElementsByName("form1")[0].action = 'forwardUrl';
I managed to fix the problem by using decodeURIComponent(forwardUrl) on the forwardUrl.

JQuery: how to execute code before sending a form

I made a form and set its attr to onsubmit="return false"
Now I need to execute some actions before the actual submit like this:
$('input[name=submit]').click(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
$('form[name=setform]').removeAttr('onsubmit');//finally sending to $_POST
});
At first glance everything works correct, but I wonder will it 100% execute all code before sending or did I do it wrong?
The onsubmit="return false" attribute will prevent the form from being submitted as it normally would be. However, when the user clicks the Submit input, the above snippet will execute once, removing the onsubmit attribute and making the form able to be submitted normally, but the user will have to click Submit again to submit it.
You can keep the existing snippet and tell jQuery to submit the form explicitly, via the third variation of the .submit() method:
$('input[name=submit]').click(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
$('form[name=setform]').removeAttr('onsubmit');//make the form submittable
$('form[name=setform]').submit();//finally sending to $_POST
});
Alternatively, you can remove the onsubmit="return false" attribute, and use the .submit(handler) variation of the method, which simplifies your code and guarantees the handler will execute just before the form is submitted:
$('form[name=setform]').submit(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
});
You could consider dismissing the onsubmit attribute and instead use your .click() code and just put return false at the end. You could also use .submit() as suggested by Stiliyan.

Submit GET form in the url instead in the paramaters

I have a get method form which goes to
mysite.com/?p=search&q=QUERY
but I want to let the form send me to
mysite.com/search/QUERY.
I have rewritten URL turned on in .htaccess.
Any help? How do I do this?
Just remove the names of all these elements inside the form and take the values with js or jquery and set them to action attribute like
$('#formid).attr('action','/url/search/'+query);
You would do this in onsubmit event and return true so that form gets submitted.

Work of form action ./login and onsubmit keyword

Please explain the piece of code below, emphasize on ./login and onsubmit keywords..
<form action="./login" onsubmit ="return validatedata()" method="post">
This is a form tag of HTML language. It says following things:
content of this form (such as Text Box or Radio Button or Combo Box or other HTML component values) send to ./login url. It is better use HttpServletRequest.getContextPath() for set relative path rather of absolute path.
onsubmit ="return validatedata()" part says: When user click on submit button(with any label) before submit form to ./login url, execute validatedata function in Java Script functions, if this function not existed , user get a java script error(or other script languages).
method="post" part says: this form send with POST method . please see http://www.cs.tut.fi/~jkorpela/forms/methods.html for more information.
for more information related to form tag see : http://www.w3schools.com/tags/tag_form.asp
This declares an HTML <form> element. When submitting the form it will call the javascript function validatedata(). If the function return true the form will be submitted and if it return false, it won't. The './login' is the destination of the form data. So there is probably a page handling the url http://<your_site>/<where_you_currently_are>/login. It also depends on the technology you use. I don't know if you use a framework such as Struts or if you only use JSPs as is.

Submit form when contents of MVC3 Html.TextBoxFor changes

This should be fairly easy but I've tried a few things with no luck.
I have a series of Html.TextBoxFor fields on a page, each inside their own Ajax.BeginRouteForm. Next to each box I have a submit button, and this, when clicked, performs the Ajax update as desired.
I'd like to automate this so that when the user changes a value in a field (the onchange event) the form is submitted the same way it currently using using the submit button.
I tried using the htmlattributes to assign a JavaScript function to the onchange event (as shown below) and this causes the form to submit, but it redirects the page instead of working in the ajax fashion (as opposed to clicking the submit button which works correctly).
#(Html.TextBoxFor(model => answer.Value, new { onchange = "document.forms[" + answer.AnswerID + "].submit()" }));
(fortunately my answer.AnswerID is numeric and matches up with the numeric position of the appropriate form in the forms collection; I was referencing them by name but Razor (or something) was htmlencoding my JavaScript code...)
My only guess is that I'm breaking something by attaching code directly to the onchange event, but I'm at a loss as to the "right" way to hook into that event chain.
If you're willing to use JQuery, it's very simple to do:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Calling submit() on a form will ignore any submit event handlers, as seen here. You can either
call the event handler directly, or
call click() on the submit button for the form.
The former works best if you use onsubmit and return false instead of using the event argument to the callback, because otherwise you need to pass another messy object or something.

Categories