0

So I have this site that I need to post some data to. That site will be in an iFrame and the data that I'm posting is in my markup.

<iframe src="www.externalsite.com/whatever.php" name="my_iframe"></iframe

<form id="my_form" target="my_iframe">
  <input type="hidden" name="age" value="12">
  <input type="hidden" name="name" value="bob">
</form>

<script>
  $('#my_form').submit();
</script>

I have a route that renders this page in node like this:

app.get('/age_page', function() {
  res.render('age_page.html', res);
});

What is the correct way to send the data from my_form to the iframe so it renders the correct content inside my iframe? The iframe is just going to consume that data and store it in a database.

Jack Johnson
  • 847
  • 1
  • 9
  • 16

1 Answers1

1

In your example, the iframe does not appear to be connected with the form in any way, so not sure how the iframe is relevant, but

The

 $('#my_form').submit();

will cause a POST action to your webserver, so you need

app.post('/age_page', ....);

to receive the post form action in your backend

Soren
  • 13,623
  • 4
  • 34
  • 66
  • Hmmm I think you are correct Soren. How do I associate that posted data with the iframe though after? Also would I need to add method=POST and action="/age_page" to that form element then? – Jack Johnson May 17 '16 at 19:34
  • method=POST is the default, so no need, but action="/age_page" should be there if you are getting the form from a different url. Iframes are typically used for cross domain stuff (like ads) so why are you using it here? -- sounds like you are on the same domain -- or is there something in the question which needs more explanation? – Soren May 17 '16 at 19:36
  • Sorry if it wasn't clear in the original post, here is my intended use case: I have a page called age_inputs.html which lives on /age_inputs. A user can put in their info there(age, name, etc). When they click on "submit" button, I want a new page to render which lives on /age_page where an iframe shows with the response of that posted data. I guess I'm a bit confused on the correct flow here. – Jack Johnson May 17 '16 at 19:42
  • Forgot to add, I want that POST data to be sent to the iframe site. – Jack Johnson May 17 '16 at 19:47
  • Do you control both sites? If not, you will have cross domain validation issues. – Soren May 17 '16 at 20:02
  • I do not control the external site. So is something like this not possible if that is the case? – Jack Johnson May 17 '16 at 20:10
  • Not sure which one of the sites are "external" to you, but this may be relevant to you -- http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – Soren May 17 '16 at 20:18
  • Thanks Soren, I'll look into it. – Jack Johnson May 17 '16 at 20:19