首页 > 解决方案 > javascript 将数据发布到 php 并从 php 打印

问题描述

每个人

大家好

如何使用纯 javascript ajax 将数据从 javascript 发送到 php 并在 php 中打印?

文件 test.php 并将数据从 test.php 中的 javascript 代码发送到 test.php 和 print_r($_POST['来自输入名称或变量名称'])

我搜索了很多但找不到

code

<script>

//file test.php
var HTTP=new XMLHttpRequest();
var params={name:"jack",family:"jackers",age:"30"};

HTTP.open("POST","http://127.0.0.1/test/test.php");
HTTP.send(params);

</script>




<?php

//file test.php
if (isset($_POST['name'])){
    print_r($_POST['name']."<br>");
    print_r($_POST['family']."<br>");
    print_r($_POST['age']);

}

?>

不工作

标签: javascriptajaxsend

解决方案


I think this would help you to get more specific regarding the question..

<script>
 function sayHello() {
   var v1 = "hello";
   var v2 = "everyone";
   window.location.href = "result.php?w1=" + v1 + "&w2=" + v2;
 }
</script>

result.php

function sayHelloBack() {
   if (isset($_GET["w1"]) && isset($_GET["w2"])) {
     $hello = $_GET["w1"] . " " . $_GET["w2"];
     echo $hello;
   }
}

推荐阅读