【问题标题】:Send POST data to PHP without using an HTML form?不使用 HTML 表单将 POST 数据发送到 PHP?
【发布时间】:2012-01-28 04:00:26
【问题描述】:

除了有一个表单之外,还有什么方法可以将发布数据发送到 php 脚本? (当然不使用 GET)。

我希望 javascript 在 X 秒后重新加载页面并同时将一些数据发布到页面。我可以使用 GET 来完成,但我更愿意使用 POST,因为它看起来更干净。

非常感谢。

编辑:可以使用 PHP 标头吗?我确信使用 JQuery 会更好,但对于我目前的情况,我可以更容易/更快地实现它:)

干杯

我最终这样做了:

<script>
  function mySubmit() {
    var form = document.forms.myForm;
    form.submit();
  }
</script>

...

<body onLoad="mySubmit()";>
  <form action="script.php?GET_Value=<?php echo $GET_var ?>" name="myForm" method="post">
    <input type="hidden" name="POST_Value" value="<?php echo $POST_Var ?>">
  </form>
</body>

对我来说似乎工作得很好,但是如果有什么问题,请说出来!

谢谢大家。

【问题讨论】:

  • 如果您想避免使用 AJAX,您可以隐藏表单,或者动态创建一个表单,然后在超时后使用 Javascript 提交表单。 AJAX 可能是最好的解决方案,它完全取决于您想要做什么,并且 AJAX 可以说为您提供了更多优雅地处理错误的空间。
  • 啊是的,这对我来说听起来可能,我相信其他建议总体上更好,但这是一个小项目,这个解决方案听起来很适合我的编码风格:)
  • 有关如何在无框架的 Javascript 中完成此操作的详细信息,请参阅下面的答案。

标签: php javascript html


【解决方案1】:

使用FormData API。

从那里的例子:

var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456);

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);

【讨论】:

    【解决方案2】:

    这个怎么样:

        function redirectWithPostData(strLocation, objData, strTarget)
    {
        var objForm = document.createElement('FORM');
        objForm.method = 'post';
        objForm.action = strLocation;
    
        if (strTarget)
            objForm.target = strTarget;
    
        var strKey;
        for (strKey in objData)
        {
            var objInput = document.createElement('INPUT');
            objInput.type = 'hidden';
            objInput.name = strKey;
            objInput.value = objData[strKey];
            objForm.appendChild(objInput);
        }
    
        document.body.appendChild(objForm);
        objForm.submit();
    
        if (strTarget)
            document.body.removeChild(objForm);
    }
    

    这样使用:

    redirectWithPostData('page.aspx', {UserIDs: getMultiUserSelectedItems()},'_top');
    

    【讨论】:

      【解决方案3】:

      按照上面的要求,这里是您可以动态添加隐藏表单并在您想要刷新页面时提交它的方法。

      HTML 中的某处:

      <div id="hidden_form_container" style="display:none;"></div>
      

      还有一些 Javascript:

      function postRefreshPage () {
        var theForm, newInput1, newInput2;
        // Start by creating a <form>
        theForm = document.createElement('form');
        theForm.action = 'somepage.php';
        theForm.method = 'post';
        // Next create the <input>s in the form and give them names and values
        newInput1 = document.createElement('input');
        newInput1.type = 'hidden';
        newInput1.name = 'input_1';
        newInput1.value = 'value 1';
        newInput2 = document.createElement('input');
        newInput2.type = 'hidden';
        newInput2.name = 'input_2';
        newInput2.value = 'value 2';
        // Now put everything together...
        theForm.appendChild(newInput1);
        theForm.appendChild(newInput2);
        // ...and it to the DOM...
        document.getElementById('hidden_form_container').appendChild(theForm);
        // ...and submit it
        theForm.submit();
      }
      

      这相当于提交这个 HTML 表单:

      <form action="somepage.php" method="post">
        <input type="hidden" name="input_1" value="value 1" />
        <input type="hidden" name="input_2" value="value 2" />
      </form>
      

      【讨论】:

      • 我发布了我在最后的问题/OP 中使用的内容(不确定你在这里称呼它的内容)
      【解决方案4】:

      形成自己的标题,如下所示:

      POST /submit.php HTTP/1.1
      Host: localhost
      User-Agent: Mozilla/4.0
      Content-Length: 27
      Content-Type: application/x-www-form-urlencoded
      
      userId=admin&password=letmein
      

      【讨论】:

        【解决方案5】:

        通过 jQuery:

        $.ajax({
          url: "yourphpscript.php",
          type: "post",
          data: json/array/whatever,
        
          success: function(){ // trigger when request was successfull
            window.location.href = 'somewhere'
          },
          error: anyFunction // when error happened
          complete: otherFunction // when request is completed -no matter if the error or not
          // callbacks are of course not mandatory
        })
        

        或者最简单的:

        $.post( "yourphpscript.php", data, success_callback_as_above );
        

        更多关于http://api.jquery.com/jQuery.ajax

        【讨论】:

          【解决方案6】:

          您可以在重新加载页面之前发送一个包含您想要发布的数据的 xhr 请求。

          只有在 xhr 请求完成后才重新加载页面。

          所以基本上你会想做一个同步请求。

          【讨论】:

            【解决方案7】:

            您可以使用 JQuery 发布到 php 页面: http://api.jquery.com/jQuery.post/

            【讨论】:

            • 看来我需要学习 Ajax 和 JQuery 才能进步……我一直在尝试做的很多事情看起来可以用它们更轻松地完成。但是我从来没有接触过,所以短期内我不能真正使用它(直到我了解 JQuery)。非常感谢。
            猜你喜欢
            • 2021-12-10
            • 1970-01-01
            • 2017-09-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-28
            • 2020-09-09
            • 2022-06-16
            相关资源
            最近更新 更多