form onsubmit delaying window.open not working [duplicate] - javascript

This question already has answers here:
Prevent form redirect OR refresh on submit?
(7 answers)
Closed 2 years ago.
I have been trying to figure this out for past few hours, I have form and when users will submit it,
new window popup will occur, however I want it delayed slightly, setting setTimeout did not work and I have no idea why.
example:
<form method="post" target="print_popup" action="https://example.com" onsubmit="window.open('about:blank','print_popup','width=1000,height=800');">
<input type="hidden" name="param" value="foobar">
<input id="1" type="submit" value="Submit request">

Think you will need to set the target attribute of the form to _blank to allow for opening a new context:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
I would also check chrome/your choice of browser isn't blocking your popup

Related

PHP: How to return to same spot on page after clicking Submit? [duplicate]

This question already has answers here:
Is it safe to use anchor to submit form?
(3 answers)
Closed 2 years ago.
I'm making a web page with 20 different questions on the index page. The user clicks "Submit" after every question.
<form action="./index.php" method="post" id="element">
The information is then posted and sent to the database. Of course this makes the index page reload and display at the very top of the page. This makes for a lot of scrolling.
Is there a way to make it so the page reloads and scrolls back down to the place where it left off?
The answer was found here: https://stackoverflow.com/a/7983093/4101210
To use an anchor to submit a form would require the use of JavaScript to hook up the events. It's not safe in that if a user has JavaScript disabled, you won't be able to submit the form. For example:
<form id="form1" action="" method="post">
Submit!
</form>
If you'd like you can use a <button>:
<button type="submit">Submit!</button>
Or stick with what we all know:
<input type="submit" value="Submit!" />
You can style all three of them, but the latter two don't require JavaScript. You probably just need to change some CSS somewhere if you're having border issues.
The code that answered my question was by joshua bissot. He showed how to put the token in the action and name the anchor to make this work.
https://stackoverflow.com/a/25918771/12140988
<!-- add the anchor token at the end of your action statement -->
<form method='post' action='this_page.php?put_peram=token#anchor_name'>
<input type='submit' value='click here'>
<!-- put the anchor right above where you want the page to index -->
<a name="anchor_name></a>

How to redirect two pages in one button click in html? [duplicate]

This question already has answers here:
Redirect to 2 pages
(3 answers)
Closed 3 years ago.
I need to redirect to two pages in one button click. Is it possible?
And this is what I tried.
Using this code I can go to one page only.
<form action="http://195.678.99.00:5002/predict" action="www.mywebsite.com" id="usrform" method="post" enctype="multipart/form-data" target="_blank">
<button id="myButton" type="submit" class="button button5" >View pages</button>
</form>
Get rid of the form tags, keep the button only:
<button onclick="window.open('http://195.678.99.00:5002/predict'); window.open('www.mywebsite.com');">View pages</button>
Be aware it won't work in all browsers because of popup blockers, as stated in the comments of this answer: https://stackoverflow.com/a/7065030/4810180

Cannot programmatically submit form [duplicate]

This question already has answers here:
"Submit is not a function" error in JavaScript
(19 answers)
Closed 8 years ago.
Cannot get my form to submit programmatically, despite trying several ways. Tried pure JS, also tried jQuery. No success.
I have this form tag :
<form action="https://docs.google.com/forms/d/13zex8ZsEsnZz3A8uB4jU4oDb5wZfaqqq2Pq2CGlIe6M/formResponse" method="POST" id="ss-form" target="_self" onsubmit="" name="eForm">
<!--My Form Stuff-->
<input type="submit" name="submit" value="Submit" id="ss-submit">
</form>
Here is what i've tried :
/*jQuery*/
$('#ss-form').submit();
/*Javascript*/
document.getElementById('ss-form').submit();
document.eForm.submit();
None of them work. Not sure why, but I am assuming it has something to do with me trying to submit a google form. If I physically click the submit everything works fine.
Any and all help is greatly apprecaiated.
The problem is that you have a field (HTMLInputElement) with name submit in the form. That's why document.getElementById('ss-form').submit is not a function but an object.
So, you get the following error:
TypeError: object is not a function
The solution is to remove that element. We should be careful to verify if browser thinks it's an element and not a function:
if (typeof document.getElementById('ss-form').submit === "object") {
document.getElementById('ss-form').submit.remove();
}
document.getElementById('ss-form').submit();

javascript self destructing web page [duplicate]

This question already has answers here:
window.close and self.close do not close the window in Chrome
(17 answers)
Closed 8 years ago.
I have a web page with a countdown and redirect to the previous page which works great, but instead of redirecting to the previous page, I want the page (current browser tab) to close. disappear. self-destruct.
I have tried substituting my redirect history.go(-1) with window.close() but it didn't work.
JS:
var countdownfrom=9
var currentsecond=document.redirect.redirect2.value=countdownfrom+1
function countredirect(){
if (currentsecond!=1){
currentsecond-=1
document.redirect.redirect2.value=currentsecond
}
else{
history.go(-1)
return
}
setTimeout("countredirect()",1000)
}
countredirect()
HTML:
<form name="redirect">
<h1>Oops! I think you meant to use a URL...</h1>
<h2>this page will self destruct in</h2>
<form>
<input type="text" size="1" name="redirect2">
</form>
<h2>seconds</h2>
<h3>Go back to where you came from and try again!</h3>
the html doesn't look right, can you even have a form nested in another form? It works fine though.
Corrected HTML:
<h1>Oops! I think you meant to use a URL...</h1>
<h2>this page will self destruct in</h2>
<form name="redirect">
<input type="text" size="1" name="redirect2">
</form>
<h2>seconds</h2>
<h3>Go back to where you came from and try again!</h3>
This should be an informative answer: why doesn't my window.close work
Long story short, you probably are not meeting the criteria to close a window with JS.

Why won't my js form submit work? [duplicate]

This question already has answers here:
Property 'submit' of object #<HTMLFormElement> is not a function
(3 answers)
Closed 9 years ago.
I have an iframe and a form. I want to submit the form into the iframe. This works, but the JS won't submit the form. I want the form to submit on page load or just when the script is rendered by the browser. I have already tried about 10 different JavaScript variants and successfully submitted the form with a button. Any thoughts?
<iframe src="http://domain.com" name="i0"></iframe>
<form action="http://domain.com/" target="i0" id="f0" method="POST">
<input type="hidden" name="secret" value="4cda562cd5dafa1882c9f18dc0dc5dba">
<input type="hidden" name="id" value="39">
</form>
<script type='text/javascript'>
document.getElementById('f0').submit();
</script>
<input type="submit" name="submit">
this is what's causing your issue, change its name to something else. because now this
document.getElementById('f0').submit();
is trying to access that input, and giving the error:
TypeError: document.getElementById(...).submit is not a function
You should have posted all your form code here, we could have found it faster :)
Maybe it is because your iframe hasn't fully loaded yet, so you need to wait for it:
<script type='text/javascript'>
document.getElementsByName('iframe')[0].onload = function(){
document.getElementById('f0').submit();
}
</script>
I see, you cant' use name="submit" here because you are overwritting FORM submit method, change it, e.g:
<input type="submit" name="bsubmit">

Categories

Resources