简体   繁体   中英

Auto Submitted form does not carry form variables

The following form does not carry form variables when it is submitted using quto javascript after 5 seconds.

Normal submission works fine.


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<SCRIPT LANGUAGE="JavaScript"><!--
setTimeout('document.test.submit()',5000);
//--></SCRIPT>

</head>
<body>
<form name="test" id="form1" method="post" action="auto2.php">
  <p>
    <INPUT TYPE=checkbox NAME="test" VALUE="option1"> Option1
    <INPUT TYPE=checkbox NAME="test" VALUE="option2"> Option2
    <INPUT TYPE=checkbox NAME="test" VALUE="option3"> Option3   
  </p>
  <p><input type="submit" name="submit_test" value="Submit_test" />
&nbsp;  </p>
</form>
</body>
</html>
---------------------------------------------------------------

output page:

--------------------------------------------------------------
<?php

if(isset($_POST['submit_test'])){
$variable=$_POST['test'];

echo $variable;

}

?>

Any help is highly appreciated.. thank you.

You are testing whether the submit_test button value is set:

if(isset($_POST['submit_test'])){

Submitting the form automatically won't set the value, so your test fails.

You should test for some other form field, like a hidden element.

Unless a checkbox is checked, you wont get any response (blank or otherwise)

What you should have instead is

<input type=hidden name="test" value="">
<INPUT TYPE=checkbox NAME="test" VALUE="option1"> Option1
<INPUT TYPE=checkbox NAME="test" VALUE="option2"> Option2
<INPUT TYPE=checkbox NAME="test" VALUE="option3"> Option3  

That way you get a default value when you post your form

+1 to Pekka's answer.

If you do the following it will check to see that the page was posted to without requiring that a variable was set in the post.

if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }

You should probably also put in some form of validation to make sure it came from where you expected it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM