NodeJs Submit from that's on another page

I am trying to write some code where I have the ParentPage.html and ChildPage.html

On ChildPage.html I have a form:

<form>
  First name:<br>
  <input type="text" name="firstname" value="">
  <br>
  Name:<br>
 
  <input type="submit" value="Submit">
</form> 

Open in new window



Both pages are on my localhost.

I need to add some code on ParentPage.html that with hit the submit button on ChildPage.html

Can this be done using NodeJs? If so how?
LVL 1
error2013Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
That is HTML which says it's going to be in a web browser.  You're missing the 'action' attribute of the <form> which tells the browser where to send the information.  NodJs may host the page but it is not used to create the page.  On this page https://www.w3schools.com/html/html_forms.asp , look for The Submit Button and read about the  'action' attribute.
0
error2013Author Commented:
Well, that would be the normal way to do this but I cannot because the form submits to itself.

The form page is on: http://127.0.0.1/ChildPage.html and it's action is action="ChildPage.html"

I'm using electron so I am using Nodejs and general javascript
0
Julian HansenCommented:
Please explain what this means
I need to add some code on ParentPage.html that with hit the submit button on ChildPage.html
First - I am assuming that "with hit the submit button" should be "will hit the submit button"
Second - when you talk about parent page and child page - can you explain exactly how this manifests? Are we talking about an iframe'd child page inside a parent?
Are we talking about a child that submits to a parent?

I am assuming you are talking about an iframe - in which case you can call a function in the child page - providing the parent and child originate from the same source / protocol / port

Parent (note script uses jQuery - not shown)
  <div><button class="btn btn-primary">Submit child</button></div>
  <iframe src="t3060-iframe.html"></iframe>
<script>
$(function() {
  $('button').click(function(e) {
    e.preventDefault();
    var target = $('iframe')[0];
    target.contentWindow.submit();
  });
});
</script>

Open in new window

Child
<form action="reflect.php" id="myform">
  Your Name <input name="name" type="text">
</form>
<script>
function submit()
{
	document.getElementById('myform').submit();
}
</script>

Open in new window


Working sample
here
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

error2013Author Commented:
Great way of doing this!

Only issue is that I cannot change the code in the child page so:

<script>
function submit()
{
	document.getElementById('myform').submit();
}
</script>

Open in new window


wouldn't be there.

I would have to just target it saying Form->submit as the form submit button has no id.

Can this be done?
0
Julian HansenCommented:
Assuming you have a form with id myform then you could do this
var target = $('iframe')[0]
var doc = target.contentDocument || target.contentWindow.document;
doc.getElementById('myform').submit();

Open in new window


NOTE: This will only work if the child is on the same URL / PROTOCOL / PORT as the parent - otherwise you can't do the above.
0
error2013Author Commented:
The url is the same and the port too but the form has no id or class.
0
Julian HansenCommented:
Then probably better to use jQuery to find it
$(function() {
  $('button').click(function(e) {
    e.preventDefault();
    var target = $('iframe').contents().find('form');
	target.submit();
  });
});

Open in new window

But this raises the question - if the child script is on your server (same domain/protocol/port) why don't you have access to change it?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.