【问题标题】:Post form, wait ten seconds and redirect to发布表单,等待十秒钟并重定向到
【发布时间】:2013-10-17 19:56:11
【问题描述】:

我有一个页面需要做以下步骤:

第 1 步 - 通过POST 接收 URL 地址、名称、值、编号和 ID

第 2 步 - 等待 10 秒

第 3 步 - 发送(也通过邮寄)通过邮寄收到的以下信息:名称、值、编号和 ID 到通过邮寄收到的 URL

第 4 步 - 重定向到收到帖子的 URL 并在屏幕上打印帖子。

为此,我正在使用下面的代码,但是出现了任何问题,因为重定向 URL 中的打印应该使帖子信息变空,所以看起来无论出于何种原因信息都没有正确发布。

有人可以告诉我我做错了什么吗?

代码:

HTML - 创建要发布的表单:

<form id="ret" name="return_url" method="post" action="<?php print_r($_POST['url']);?>">
 <input type="hidden" name="name" value="<?php print_r($_POST['name']);?>" />
 <input type="hidden" name="value" value="<?php print_r($_POST['value']);?>" />
 <input type="hidden" name="num" value="<?php print_r ($_POST['num']);?>" />
 <input type="hidden" name="ID" value="<?php print_r($_POST['ID']);?>" />
 <input type="hidden" name="status" value="OK" />
</form>

将发布的 url 内容添加到将在 Javascript (PHP) 中使用的 var $retorno

<?php
    $retorno = $_POST['url'];
?>

启动计数器,提交表单并重定向(使用 javascript):

<script language="javascript" type="text/javascript">
  window.onload = function() {
    function countdown() {
      if (typeof countdown.counter == 'undefined') {
        countdown.counter = 10; // initial count
      }
      if (countdown.counter > 0) {
        document.getElementById('count').innerHTML = countdown.counter--;
        setTimeout(countdown, 1000);
      }
      else {
        document.getElementById("ret").submit();
        location.href = '<?php echo $retorno?>';
      }
    }
    countdown();
  };
</script>

【问题讨论】:

    标签: javascript http-post


    【解决方案1】:

    你的代码有很多问题。

    1.你在应该使用echo的地方使用print_r

    <form id="ret" name="return_url" method="post" action="<?php print_r($_POST['url']);?>">
    

    在上面的行中,print_r 将以一种无效的方式输出$_POST['url'] 的内容作为action 属性值。 查看生成页面的 HTML 源代码。

    这应该改写为:

    <form id="ret" name="return_url" method="post" action="<?php echo $_POST['url']; ?>">
    

    您对所有输入值都犯了同样的错误。 action 值尤其重要,因为它必须是表单发布的地址。

    2。您提交了表单,但同时也重定向了页面

    submit() 表单与location.href 重定向之间存在冲突

    只需删除 location.href 行。如果您的 HTML 表单配置正确,JavaScript submit() 会将浏览器发送到 action 属性中定义的页面。

    最后,我建议您查看 jQuery 库,以使 JavaScript 编码更容易。

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 2016-02-12
      • 1970-01-01
      • 2016-10-12
      • 2014-12-20
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-07-11
      相关资源
      最近更新 更多