/    Sign up×
Community /Pin to ProfileBookmark

Retrieving values from html form

So I’m a novice coder and have been trying to piece together a (seemingly) simple function. I want the user to be able to enter in a certain amount of time into a form, and then when they click submit, it opens a new page and closes after that amount of time.

There is a very simple form on the first page that accepts input and looks like so:

<form action=”onbreak.php” target=”_blank” method=”POST”><p class=”fillable”>I would like to view <input type=”text” name=”url” id=”url” value=”” size=”30″/> for <input type=”text” name=”timeinput” id=”timeinput” value=”” size=”3″/> minute(s). <input type=”submit” value=”Break Me!”/></p></form>

This is in the head of the second page which should be retrieving the data:

<?php $_POST[‘timeinput’] = timeinput; ?>

<script>
var howLong = <?php echo timeinput; ?>;
function closeWindow(){
setTimeout(“alert(howLong);”,2000);
// setTimeout(“self.close();”,howLong);
}
</script>

The “alert(howLong);” is so I get some instant feedback when it works. The commented out line is how the script should actually function. Any help would be much appreciated!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangAug 19.2010 — That is unlikely to work (security restriction) unless the new page is a child window.
Copy linkTweet thisAlerts:
@tirnaAug 19.2010 — this works in my IE8 and FF3.6

Haven't tested in other browsers but it should work in them as well.

[B]index.htm[/B]

[CODE]
<html>
<head>
<title></title>
</head>
<body>

<div>

<form action="results.php" method="post" target='_blank'>

Enter time (milliseconds) <input type="text" name="txtTime" />

<input type="submit" value="Submit" />

</form>

</div>

</body>
</html>
[/CODE]


[B]results.php[/B]

[code=php]

<?php
$time = $_POST['txtTime'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

var time = <?php echo $time; ?>;

window.onload=function() {
setTimeout('self.close()',time);
}

//-->
</script>
</head>
<body>

<h1>Inside results.php</h1>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@FangAug 19.2010 — &lt;script type="text/javascript"&gt;
var time = &lt;?php echo $time; ?&gt;;
setTimeout(self.close, time);
&lt;/script&gt;
Requests user confirmation in IE8
Copy linkTweet thisAlerts:
@tirnaAug 19.2010 — It always has afaik and then the user can choose for themselves whether to close the window or not.

At least the prompt appears after the user inputed time.

In at least FF3.6, Opera 10 and Safari for Windows 4 there is no prompt and so the window closes automatically after the user inputed time. I haven't tested othre browsers.

What other browsers that you know of also display a prompt and don't give the user an option to turn the prompt off?

Whether the prompt in IE8 is there or not is not an issue for me personally.
Copy linkTweet thisAlerts:
@tirnaAug 19.2010 — One way to workaround the IE close window prompt is to hide the page contents first so that if the user chooses to not close the window they will see a blank browser window.

[B]results.php[/B]

[code=php]
<?php
$time = $_POST['txtTime'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

var time = <?php echo $time; ?>;

window.onload=function() {
setTimeout('document.getElementsByTagName("body")[0].style.display="none";self.close()',time);
}

//-->
</script>
</head>
<body>

<h1>Inside results.php</h1>
</body>

</html>
[/code]
×

Success!

Help @acboenisch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...