javascript post data to php and print from php

送分小仙女□ 提交于 2019-12-23 05:37:09

问题


everyone

Hi, everyone

how can i send data from javascript to php and print in php using pure javascript ajax?

file test.php and send data from javascript code in test.php to test.php and print_r($_POST['from input name or name of varitable'])

I searched a lot but couldn't find it

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']);

}

?>

dosen't work


回答1:


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;
   }
}


来源:https://stackoverflow.com/questions/59148338/javascript-post-data-to-php-and-print-from-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!