0

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

essencode
  • 3
  • 5
  • Please provide code examples of your problem. – jacob13smith Dec 02 '19 at 23:59
  • Hi, the post is edit with code – essencode Dec 03 '19 at 00:22
  • Why no one answers, only 23 view after 16h why – essencode Dec 03 '19 at 16:26
  • could you please explain what is not working? it's difficult to understand for me, i dunno your setup. How are you running php? Is the code you provided everything on single File??? (if yes the php code needs to be before html) and did you tried this example as well : https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest ? – CrissCrossCrass Dec 04 '19 at 06:59
  • go to https://stackoverflow.com/questions/59169063/how-does-jquery-ajax-works-in-javascript , i want using ajax with pure javascript , i want make ajax with pure javascript in html.php , send object variable from html.php and echo or print in another file controller.php and When returned data, data must be like array, console.log(...) output array() , like jquery $.post, I can use jquery for my work but I want to know how jquery developer it made, i say again only object variable={name:"anything",email:"anything@gmail.com"} – essencode Dec 04 '19 at 07:20

1 Answers1

0

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;
   }
}
Hritik Pandey
  • 648
  • 4
  • 15