问题
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