PHP、HTML:自动提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18600772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:53:59  来源:igfitidea点击:

PHP, HTML: Submit form automatically

phphtmlformssubmit

提问by Juicy

I have a PHP page with an HTML form on it. If some variables are set in the URL I would like to automatically submit the form on the page.

我有一个带有 HTML 表单的 PHP 页面。如果在 URL 中设置了一些变量,我想自动提交页面上的表单。

IE:

IE:

if (isset($_GET['var']))
{
  // SUBMIT FORM
}
else
{
  // leave the page alone
}

EDIT:

编辑:

Here is what I have using the answer that someone provided below, but it's still not working. I would like the form to submit itself if the condition is met.

这是我使用下面有人提供的答案所得到的,但它仍然无法正常工作。如果满足条件,我希望表单自行提交。

<?php

if ($r == 1)
{

echo "  
  <br>
   <form action=\"bookRoom.php\" method=\"post\" id=\"dateForm\">
    <input name=\"from\" type=\"hidden\" value=\"$fday/$fmonth/$fyear\">
    <input name=\"to\" type=\"hidden\" value=\"$tday/$tmonth/$tyear\">
    <input type=\"submit\">
   </form>

  <script type=\"text/javascript\">
    $('#dateForm').submit();
  </script>
";      
}
?>

回答by Charaf JRA

Using pure javascript instead of jQuery :

使用纯 javascript 而不是 jQuery :

<?php
    if (isset($_GET['var']))
{?>

<script type="text/javascript">
    document.getElementById('dateForm').submit(); // SUBMIT FORM
</script>

<?php 
}
else
{
  // leave the page alone
}
?>

回答by Moeed Farooqui

I think you want this:

我想你想要这个:

<?php
if (isset($_GET['var']))
{?>

<script type="text/javascript">
document.getElementById("formid").submit(); // Here formid is the id of your form
                           ^
</script>

<?php }
else
{
  // leave the page alone
}
?>