可以在PHP页面上直接打印通过AJAX发送的POST请求传递的数据吗?

huangapple go评论52阅读模式
英文:

Can I print data sent by AJAX via a post request directly in a php page

问题

<script>
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "2.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            window.location.href = "2.php";
        }
    };
    xhr.send("username=John&email=john@example.com");
</script>
<?php
$username = $_POST['username'];
$email = $_POST['email'];

echo "Received username: " . $username . "<br>";
echo "Received email: " . $email;

事实上,我已经尽力了,但仍然不能直接在php中打印数据,可能是我的基础不够扎实,希望一些高手能教我,写写代码。

英文:

I wanted to send a post request to a php page via ajax and then print it in the php page with $_POST, but I found that I couldn't jump to the page and then print out the data like I could with a form

&lt;script&gt;
    var xhr = new XMLHttpRequest();
    xhr.open(&quot;POST&quot;, &quot;2.php&quot;, true);
    xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {
            window.location.href = &quot;2.php&quot;
        }
    };
    xhr.send(&quot;username=John&amp;email=john@example.com&quot;);
&lt;/script&gt;
&lt;?php
$username = $_POST[&#39;username&#39;];
$email = $_POST[&#39;email&#39;];

echo &quot;Received username: &quot; . $username . &quot;&lt;br&gt;&quot;;
echo &quot;Received email: &quot; . $email;

In fact, I have done everything I can think of, but still can not achieve direct print data in php, should be my foundation is too poor, I hope that some master can teach me, write write

答案1

得分: 1

&lt;form id=&quot;my-account-form&quot; method=&quot;POST&quot; action=&quot;2.php&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;username&quot; value=&quot;&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;email&quot; value=&quot;&quot;&gt;
&lt;/form&gt;

...
...
...

&lt;script&gt;
// 使用此函数填写用户名和密码然后提交。
function submitAccount(username, password) {
  const form = document.getElementById(&#39;my-account-form&#39;);
  form.querySelector(&#39;input[name=username]&#39;).value = username;
  form.querySelector(&#39;input[name=password]&#39;).value = password;
  form.submit();
}
submitAccount(&#39;John&#39;, &#39;john@example.com&#39;);
&lt;/script&gt;
英文:

If you simply want the user to be redirected to the submission PHP, the simplest way is to simply submit a POST form:

&lt;form id=&quot;my-account-form&quot; method=&quot;POST&quot; action=&quot;2.php&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;username&quot; value=&quot;&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;email&quot; value=&quot;&quot;&gt;
&lt;/form&gt;

...
...
...

&lt;script&gt;
// Use this function to fill in username + password and submit.
function submitAccount(username, password) {
  const form = document.getElementById(&#39;my-account-form&#39;);
  form.querySelector(&#39;input[name=username]&#39;).value = username;
  form.querySelector(&#39;input[name=password]&#39;).value = password;
  form.submit();
}
submitAccount(&#39;John&#39;, &#39;john@example.com&#39;);
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年6月16日 09:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定