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.
Related
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.
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.
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.
How can I get the data that is submitted from a form with jQuery?
I know I can bind the submit function to the form
$('form').bind('submit',function(){});
and I know I can serialize the data in the form:
$('form').serialize();
But how do I get the data that was actually submitted from the form? Like if there are two submit buttons, I want to know which one was pressed. If I handle the submission with PHP I can do that, but ideally I want to get a copy of the submitted data, then return true so that the form goes on to be processed by PHP normally.
Thanks!
The pressed submit button should be available in the serialized field list - and the other submit buttons shouldn't be in there.
However, apparently jQuery does not add submit buttons in there (testcase). See http://forum.jquery.com/topic/submit-event-serialize-and-submit-buttons-get-the-button-name for a workaround.
On form submit I want to serialize the form's elements, then stick another field on, then submit with $.post. Can't quite get it. The form's submit action looks like:
data = $(this).serializeArray();
data.push({filter: $.toJSON(filter)});
$.post("/datawarehouse/new.php", data);
return false;
But it's just the form's fields and then undefined/undefined. For the record $.toJSON(filter) works as I've seen the output. Also tried:
data.filter = $.toJSON(filter);
And
data['filter'] = $.toJSON(filter);
Any ideas?
I think you might have better luck putting a hidden field in your form and on form submit, set the value of the hidden field. Let the jquery serializer handle the serialization.