I would like to POST a form in an iframe, generated like so:
My JS loads an iframe inside the page, adds a form to the iframe and submits the form. What I would like to happen is the iframe to load the result of that request. So, I would effectively like to post a form and render the result inside the iframe, without touching the parent (apart from putting the iframe up for display in the first place).
I am using the code from this answer:
JavaScript post request like a form submit
but I can't get it to not reload the parent. I post the form, and instead of the iframe refreshing, the entire parent refreshes. I don't know why that is, since the url it's posting to is different and would at least redirect there.
Can anyone help me with this problem? I just want a post inside an iframe and only within the iframe, basically.
EDIT: After some more research, apparently the form is not being created properly. I'm using document.createElement("form") and then document.getElementById("my_iframe_id").appendChild(form) to append it, but it does not seem to be working correctly.
Correct, because you are creating the form node in the current document.
document.getElementById("my_iframe_id").contentWindow.document.createElement('form');
to create it inside the iframe.
It works now, part of it was that "document" was wrong, as Dan said, and the other part was that, when inserting into the iframe, one needs to use document.getElementById(div).contentWindow.document.childNodes[0].appendChild(form) rather than just document.getElementById(div).innerHTML.
Not sure how much this will help, but the answer you point at does not give the form a name/id. If you are trying to post the form with something like document.getElementById('myForm').submit(), you might have a problem because your form doesn't have a name/id.
In the past, I've had trouble with form submission apparently submitting the wrong form when there are multiple forms on a page. In every case, giving the form a name/id and then getting the form by id and calling submit() seemed to solve the problem.
Related
I have created a form in php which has to fetch data from table1 and dump it into table2 every time user clicks on a link (link provided in a different page). I have written code in a php page (assume eg -> test.php) including html as well. I tried submitting the form onload of the page using Javascript (document.formname.submit) but it keeps on going in an infinite loop and keeps inserting data in table2 again and again.
How do I prevent this and auto form submit only once and still stay on the same PHP (i.e. test.php) page, which also contains the code for displaying detail view of the inserted data in table2?
That's because simply calling submit() on a form fill cause a full page reload. If you want to keep the current approach, you should instead use AJAX call to submit the data from that form.
Read here for more details. The article explains how to do both GET and POST requests using JS.
That sad, your solution seems somewhat ... emm ... hairy. Why exactly are you copying data from no table top another?
Before JavaScript check this condition by using form name.
if ( ! isset($_POST['main']) )
Here main is form name.
I'm working on application which uses iframe overlays in addition to its main window. One of these overlays is used to modify user settings, some of which affect the display of the main window. So after saving these settings I would like to update the main window so that the user can see their new settings in action without having to log back into the application, but I have yet to find a way that is both consistent and renders the updated main window correctly...
The specifics are that a click on the Save button calls a JavaScript function. This function submits an HTML form to invoke the appropriate Spring-MVC action (saveXXXSettings.do). In the corresponding server-side method (saveXXXSettings()), a post-save call to another method to redraw the main page will render an updated version of that page but within the iframe overlay instead of the top frame of the browser. So I just tried to set window.top.location.href to the form submisssion, i.e.,
window.top.location.href = settingsForm.submit();
and got an HTTP error page with no helpful information. In looking at this site and the W3 School page, I see that the default method-type for HTML forms is GET, so I'm wondering if the only way to get around my error to use the corresponding Spring action and a parameter string, i.e.,
window.top.location.href = saveSettings.do?formParam1=xxxx&formParam2=yyyy
I'm trying to avoid having to assemble a parameter string just to achieve the window-refresh I want, so any advice or suggestions are appreciated...
I would recommend using iframe event listeners instead. From the main frame add a listener to messages. Might look something like this...
window.addEventListener('message', function(e) {
var submitParamters = e.data;
});
Then when the user clicks submit in the overlay iframe, post a message to the main window. Might look something like this...
mainWindow.postMessage(submitData, targetOrigin);
You can read more about this here... http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage.
This is capable of performing cross-domain messaging, but will work with same domain as well.
Thanks for the answer, CurtisJD, but I decided to go with a different approach: I'm copying the values entered in the iframe overlay into a form on the main page, then I submit that form to save the values then refresh the home page, which it does automatically at the top (i.e., browser) level. Thanks again for your quick response...
I want to show a acknowledgement (more precisely a popup) when form is successfully submitted.Previously I was using Ajax to validate form and display pop up but now I want to achieve same without Ajax.
Is there any event in javascript/Jquery which is invoked after successful form submission? or Is there any other alternative available?
Thanks!
EDIT 1 :
I am using Spring 3.0.
Here is the detailed scenario
1. User fill the form and click on submit
2. Request will be sent to controller (Server side)
3. Validation will be done at server side
4. If errors are present I am using Spring validation to show it and goto Step 1
5. else successfully submit the form and show a popup.
6. After user clicks on popup redirect to other page.
EDIT 2:
I am completely agree with the opinion that Ajax is the right/best way to do it and I already implemented it using Ajax. But client want to use non-ajax approach and I cannot go beyond his words.
This question piqued my curiosity, as I was trying to do something similar using the iframe solution suggested by Leon. Eventually I succeeded, however, I would like to suggest that rather than using a direct onload property, you make use of the jQuery .load() event on the iframe.
Edit: So here's how I set up the form (using HTML5, so quotes aren't necessary):
<div id=message></div> /* Example-specific, see below */
<form method=post action=backend.php target=iframe>
// Form data here
</form>
<iframe name=iframe></iframe>
I added the following CSS code to hide the iframe:
iframe {
border:0px;
height:0px;
visibility:hidden;
width:0px;
}
Don't use display:none, as some browsers will refuse to submit to an element that's not displayed.
Then in my $(document).ready() JavaScript...
$('iframe').load(function(){
// Your load event here.
});
You could also change that about, so that it specifically only triggers after a specific event (if you're using dynamic forms, for example). In such a case, you may want to use .unbind('load') before .load() to prevent previously-added .load() functions from calling.
Now when the form is submitted, it loads into the hidden iframe. When the iframe loads the page (backend.php, in my example), it triggers the .load() function. In my specific case, I set up a <div id=message> to display a message:
$('iframe').load(function(){
$('#message').html('The form successfully submitted.');
});
Without Ajax? No Problem - let's go back to how the Web really used to work in the past ;-)
Since I am getting you don't want to refresh the current page, how about this approach:
have a hidden iframe on the same page, with a name & id
point the target property of your form to the name given in the previous step
submitting the form will now be "hidden"
you can have an onload property on the iframe set to a javascript method of your liking to get called once the form finished submitting
that javascript code could also retrieve the contents of the iframe and check for your server-side response (maybe even including an error msg)
notify the user about the result
This is all fairly easy to setup, let us know how it works for ya..
I am not sure which language you are coding in.
One option - use javascript.
On the submit button onclick event (client side event), perform the page validation and display alert pop up, if the page is valid.
<script type="text/javascript">
function OnSubmitClientClick() {
Page_ClientValidate();
if (Page_IsValid) {
alert('Form has been successfully submitted.');
return true;
}
}
</script>
Why do you want to drop AJAX approach? Without AJAX, server side validation implies page reload. On page reload you would lose client side (JS) state.
One alternative is to use hidden frame/iframe/a new window to perform server side validation on form submit(possibly use the pop up you are referring to in your question). Which in my opinion is not the right approach(A BIG NO). You may rather stick to AJAX or go with non AJAX way of form submit.
Sorry if this one has been answered somewhere else but couldn't quite find the fool proof answer I need.
I have a update form page that contains a number of iframes, each iframe contains another form. I need to be able to hit submit on the parent page and as well as submitting the parents form fields but also the iframes form fields all to the same php script.
Looks like javascript is the way to go but I am a complete beginner with javascript so need some extremely dumbed down help and how to.
I am submitting a page onclick of a form element and need a function to run after the submit refreshes the page. I'm trying to add an animated scroll back to the clicked element that caused the submission. I've got the scroll part covered but I can seem to figure out how to cause the function I wrote for the scroll to run after the page refreshes from the submit.
Any timely help would be much appreciated!
If you are doing a full submit, rather than an AJAX submit, then the page that displays afterwards is not the same page as the one that the form was submitted from. Consequently, the identity of the clicked element will not be available on the second page.
What you need to do is, during the submit handler, store the identity of the clicked element (Should probably be a unique ID of some kind) in a hidden field of the form.
When the page refreshes, it should now have the unique ID available (Probably placed in the same hidden field of the form by the server side code) and a javascript function can read this value to control the scrolling.
Does this make sense?
If you update your question to include some sample code, then I might be able to clarify further.
If you do a "real" form submit, where the actual page refreshes, there is no way you can do it from the client (except using frames). Once you leave the page, your javascript is out of scope. You need to insert the javascript to the refreshed page on the server.
If, on the other hand, you are submitting the form and refreshing a part of the page via ajax, then, depending on the framework you use, you'll be looking for a callback hook like onSuccess etc. in your ajax submit function
This would be easier to do in ajax however if you need to do it as a postback then you need to attach an event to the body load event and send some data back with the postback that would identify that the page has loaded as part of a post back and not a new page load.
e.g. create a hidden contol ont he web page and on the postback give it a value , on the postback check to see if that hidden control has a value and if so run your scorll code.