Angularjs launching window.open from ng-click - javascript

I have a form that has a number of dependent parameters fields. That is, changing one parameters field value may cause a server request to repopulate the other parameter fields. After the user sets all of the fields they will press a button and launch in a popup window a new form running the report they specified.
To get the URL for this window I need to make one final call to the server which will return the formatted URL (using GET). So the resultant flow on button click is:
Gather values from fields
Call web service to construct URL. (I will use a promise)
Launch open.window with new URL
The first 2 steps are straight-forward, but I am unsure on how to handle the window.open from the controller. Any thoughts or ideas?
P.S.
I thought about using a directive to overwrite the link URL as values changes, but having to go back to server to construct the URL after each parameter changed seems like way too much processing.

Sorry for wasting people's time. The answer is $window.open() from the controller.

Related

Is there a way to send POST data to another form and return the result of that form?

I need to send form data to another page that will allow the user to do something in a form and return the result of that form back to the original page? Is this possible? I know it's not ideal, but the issue is that I need to make a "drop-in" solution that does not need to be integrated with other code. I know it's a very specific request and scenario.
I know how to send POST data that doesn't require any user input on the processing page. i.e. I can send POST data to 'calculate.php' which will do the math and send it back, but if I need additional user input on 'calculate.php', how can I still send it back?
An example of expected results would be:
Page #1: User enters a number and presses submit to go to next page.
Page #2: User enters a second number and presses submit to finish.
Back to Page #1: User receives sum of both numbers.
Obviously, this is a really redundant thing to do, but I'm trying to simplify the problem as much as possible.
EDIT: There a few restrictions I forgot to add.
Page #1 is not my application, I am developing Page #2 as a "drop-in" solution for Page #1. Essentially, I can only use Page #1 to call Page #2 and receive a response from it. The problem is that I need to be able to allow for user input on Page #2.
I know I can post to Page #2 and then post to Page #1 again, but what if I need to maintain the state of Page #1. For example, if there's an open Web Socket connection.
Please note, I understand that this may be impossible or extremely difficult, but if I don't ask I'll never know right?
You want it with PHP or any other language. If you are running Php on server side then you can use Global variables like $_GET and $_POST.
Page #1: Use Post/Get method to send data to second page.
Page #2: Receive all fields' values using Globe variables ($_GET and $_POST). You can use these values as default values of form fields. Now submit this data to page 1 using post or get method.
Back to Page #1: Here you will receive the data of first page from second page and newly posted data from page 2
Either of these should work:
Never leave the page - use AJAX / XMLHttpRequest to call out to other pages to process chunks of data
Do everything on page 1 using "postbacks" -- the form targets are the same page, there is a state variable like "stage=1", and you use JavaScript to add set hidden variables for any additional state that's needed.
... PHP state validation and processing for the different stages ...
... one or more blocks of HTML for the page (PHP if / else can be used to choose between multiple page views) ...
Edit for added restrictions:
Have page 2 use postbacks or AJAX to collect the additional information
I figured out a few ways to do it.
Update a Database (or Data Store of some sort, depends on security needs) and have Page #1 listen for events from a separate page (on the same server as the database). Very similar to the way PayPal's Instant Payment Notification (IPN) works. I was actually able to set up server sent events with it as well.
Essentially, Page #1 sends data to Page #2 where the user will perform the function and then Page #2 will send POST data to a listener somewhere (either on the same server or Page #1's server), the listener will update a database and Page #1 will be listening or pulling to an event handler that will send an update once the database updates.
Use JavaScript Child/Parent Window functions. This is okay if Page #1 and Page #2 are on the same server, but can get messy and browsers have a lot of restrictions and it varies depending on browser.
Page #1 will open Page #2 in a child window, after the user performs a function, Page #2 will call a function that accepts the result data on Page #1.

window.open vs form.submit

what i need to do is, when the user clicks a certain div/image, to navigate to a different page. i use struts2 on backend.
so far, i have found 2 approaches, which produce the same effect (navigating to the other page):
create an s:url tag with my action. add the div an onclick attribute, with the url as the parameter. in the javascript function, call window.open(url)
create a form with the action. with jquery, call document.forms[].submit();
what i want to know is what is the difference between these two approaches. i am mostly interested in differences in terms of what happens inside struts, or in terms of dialog between browser and server, or what happens to the session
one difference (which i am not interested in) is that window.open also accepts parameters to open in a new window, and resize that window. i want to open the new action in the same window
a second difference (which is indeed more interesting) is that forms allow to use either POST or GET protocol. as for window.open, i am not sure which protocol is used
window.open opens a new window
window.location changes the location of the current page
forms[n].submit() submits the nth form to the server.
The window functions issue a GET request where you can add URL parameters if you need to send info to the server. There is a limitation to the number of characters you can send in a URL and it's usually not a good idea to send passwords in a GET request.
When you POST a form the parameters are in the message and not in the URL, thus they do not show up in the browser history nor are they affected by the url length limitation.
If your not sending data to the server for processing and you need to just navigate on an image click you should wrap it in an anchor tag
<img src=""/>

HTML/Javascript: How to control refresh action without re-submit the form

We are trying to implement a web page that each time of page refreshing will not result in the form resubmit, how to achieve that? Is there any Javascript code or HTML can make it WITHOUT external javascript library(jquery, dojo or extJs)
The reason of such design is that the form is going to tie an unique relation to current data with means cannot do it twice but for security reason we have to use POST instead of GET, also after the action we still want to preserve user the right to do similar action on the same page to another relation. so how to avoid a consequence like that?
Thanks.
Suppose that the action to the form submits it to submit_form.php. That file can handle the data and do whatever it needs to do. Then in it's response, it can redirect the browser to a separate page (you'll have to look up the exact method of how to do this depending on what language you write your POST handler in). This separate page can show the results of the form submit using session variables or some other method.

JavaScript: How to read browser's cache of POST data?

Effort
I've read this question, but I still think there has to be a way to do this client side.
Case
I'm submitting a form that has a few inputs. When the form is submitted, the primary key of those inputs is shown on a results page along w/ other data and a different form.
The effect I'm trying to do is if the input-pk is modified, I want it to reload the page (think window.location.reload()), only I want to update that PK parameter's value with the changed value.
window.location.reload takes one of two values (true/false), which distinguishes if it should use browser cache or not. Thus, it seems like it should be accessible, especially since the Firebug::Net plugin shows the param in the HTTP Header.
The form requires 'Post' submissions, which adds a little more complexity.
Alternative
The other thing I've considered is to store the values in a cookie right before submission, which I can retrieve on the next page and then submit another Post; however I'd like to refrain from exposing the data in Cookies.
AFAIK, Javascript does not have access to the POST body. Can't think of an API call for that! If you are using php/.net/ruby, you can encode the POST body as JSON that your JS can use when it's reloaded, can't you?

How do I dynamically change Form Post URL?

I want to make the results of an ASP.NET form POST "bookmarkable", and I'm thinking of using query parameters to accomplish this.
The idea is that a user will visit http://domath.com and they will type in a math problem, and view the results. The query and results are visible at http://domath.com?ProblemID={some guid here}
The only part I don't really know is how do I change the target of the form URL since I'm using a POST instead of a GET..
Here are the options I came up with, and I'm not sure what is practical
Submit form as usual, server redirects to a URL with the new parameters attached
Use a webservice or callback to get new URL. Javascript then updates form target
The first option is the easiest and most common. It follows the Post/Redirect/Get model, which avoids double form submission in addition to letting you bookmark the resulting page.

Categories

Resources