最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can PHP submit an HTML form after a timer expires? - Stack Overflow

programmeradmin0浏览0评论

Is it possible in PHP to POST a form after certain time is elapsed?? I am creating a test section in my website for students where they will be giving answers to MCQs. When the time is finished the results should be posted to the next page via <form action=""> immediately.

Please tell me if I can have any other options to achieve the same.

I am not in the favor of javascript because I will be having dozens of students and all may not have javascript installed on their puter.

Edit to add my code, including the JavaScript from the answer below:

This isn't working:

<html>
<head>
    <script>
    window.setTimeout(function() {
        document.forms['test_form'].post();
    }, 2000);
    </script>
</head>

<?php

define("HOST", "localhost"); // The host you want to connect to.
define("USER", "admin"); // The database username.
define("PASSWORD", "admin"); // The database password. 
define("DATABASE", "test"); // The database name.
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
//some PHP code wil  here...
?>

<body>
    <form action="test_submit.php" method="post" name="test_form">
        <label>This is Question #1:</label>
        <input type="radio" name="ans1" id="ans1">Answer1</input>
        <input type="radio" name="ans2" id="ans2">Answer2</input>
    </form>
</body>
</html>

It's not forwarding the page to test_submit.php after 2 seconds as I expect...

Is it possible in PHP to POST a form after certain time is elapsed?? I am creating a test section in my website for students where they will be giving answers to MCQs. When the time is finished the results should be posted to the next page via <form action=""> immediately.

Please tell me if I can have any other options to achieve the same.

I am not in the favor of javascript because I will be having dozens of students and all may not have javascript installed on their puter.

Edit to add my code, including the JavaScript from the answer below:

This isn't working:

<html>
<head>
    <script>
    window.setTimeout(function() {
        document.forms['test_form'].post();
    }, 2000);
    </script>
</head>

<?php

define("HOST", "localhost"); // The host you want to connect to.
define("USER", "admin"); // The database username.
define("PASSWORD", "admin"); // The database password. 
define("DATABASE", "test"); // The database name.
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
//some PHP code wil  here...
?>

<body>
    <form action="test_submit.php" method="post" name="test_form">
        <label>This is Question #1:</label>
        <input type="radio" name="ans1" id="ans1">Answer1</input>
        <input type="radio" name="ans2" id="ans2">Answer2</input>
    </form>
</body>
</html>

It's not forwarding the page to test_submit.php after 2 seconds as I expect...

Share Improve this question edited Jul 5, 2013 at 11:04 Steven Moseley 16.4k4 gold badges42 silver badges50 bronze badges asked Jul 5, 2013 at 10:26 user2553342user2553342 432 silver badges7 bronze badges 2
  • 1 You would probably want to do that with JavaScript. – Jez Commented Jul 5, 2013 at 10:27
  • 1 There's no other way than JavaScript for this. You can't do it with PHP. – Robert Commented Jul 5, 2013 at 10:38
Add a ment  | 

1 Answer 1

Reset to default 7

Simple answer is no. PHP is a server-side language with no ability to control the client (e.g. the browser).

The only way you can do what you want is by using JavaScript. JavaScript allows you to control the flow of forms, including making them submit.

window.setTimeout(function() {
    document.forms['form_name'].submit();
}, 1000);

Where form_name is your form, and 1000 is the time to delay the post in milliseconds (i.e. 1000 = 1 second).

If your form doesn't have a name (but has an id) you can also do this, using DOM:

window.setTimeout(function() {
    document.getElementById('form_id').submit();
}, 1000);

Where form_id is your form's id attribute.

发布评论

评论列表(0)

  1. 暂无评论