Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
how can i recieve data variable from JAVASCRIPT to PHP

im still a beginner in this field i wanted to send the information from the value that I created from javascript to the other file external php or i mean from client-side to server-side as an example i have loop and value stored in 'i' and i wanna send it to php how? i googled and see some ppl say's use ajax method i still don't know it anyway i tried as an experiment like this it actually worked it only prints a value for me in console but idk how to send it to php

var myrequest = new XMLHttpRequest();
myrequest.open('POST','upload.php',true);
xhr.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");
myrequest.onreadystatechange = function() {
myrequest.onload = function() {
  console.log(this.responseText);
}
let data = JSON.stringify({'test': 'test'});

myrequest.send(data);
}

in php file i tried as an example to call variable from js $_post['test'] i dunno how but it didn't work

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

First you need to convert variable "data" to text like this

let data = JSON.stringify({test: 'test'});

In PHP you can use PHP input stream. PHP input stream is basically way to receive raw data. You can access it this way

$data = file_get_contents("php://input");

Now we have stringified JSON in $data variable. To access data that is in $data variable you need to use PHP's json_decode function.

$final_data = json_decode($data);
echo $final_data->test;

This might seem complicated thing to understand. You should read more about data types. In that way you can understand this example so much better. I will link you few things that might help you out in learning.

  • PHP Introduction
  • PHP json_decode
  • JSON stringify in Javascript
  • Javascript data types
about 4 years ago · Santiago Gelvez Report

0

String(0) means that your PHP has not received your data from Javascript. Reason for that might be that your URL is not written in right way. I recommend using full URL. For example if you have web server set at localhost your URL should be "http://localhost/upload.php".

I made you example code that you can take look at. It has comments that explain you every step that is happening. It might be hard for you to read it and understand it completely but that is okey. I actually did this example for me aswell as I do need to remind myself how to do things. I post you two links to Pastebin that you can try to use together. I post it in comments as I did not find out how to post content from Pastebin to answer.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!