I've been stuck on this for days now... Can't seem to figure out why I keep getting an undefined index error for this code, also why won't $text echo out anything?
$http({
url: url, url2,
method: "POST",
data: "data=" + window.btoa(encodeURIComponent(jsonData)),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (id, status, headers, config) {
console.log(data);
$scope.message = "From PHP file : "+data;
PHP:
$data = file_get_contents(json_decode(urldecode(base64_decode($_POST["data"]))));
$text = $data->text;
I have a limited experience with Angular, but that "url2" value seems off place to me:
$http({
url: url, url2,
There's so much wrong with this it's hard to figure out where to begin. First and foremost, why are you using base64 encoding and uri encoding and json encoding?
Next, if you are going to use all those encoding functions, you need to call the exact equivalent decoding functions in the exact reverse order. This is not happening right now.
Next, url2
does not do anything here.
Then, file_get_contents
needs a path as it's argument, not a string. file_get_contents
is literally used to get the contents of a file, so it doesn't make any sense here. In addition file_get_contents
returns a string, so it would never have a ->data
member.
Last, undefined index implies that there is no data
value in the $_POST
object. This is probably because you are not sending the data across as form data. By default angular will encode data as json, which means that from PHP's perspective $_POST
will be empty, and you can only get to the data via php://input
.
All of this tells me that you basically slapped a whole bunch of stuff together from different sources hoping it would at one point work. The one advice I can give you is not just read my answer, but actually take the time to learn the stuff you are using. You're not going to solve your problems by guessing.
Also learn how to debug. The error you got is about an item not existing in an array. You could have validated this by checking out the full contents of $_POST
. At that point you might not yet know why it went wrong, but the fact that you asked about this error and not about $_POST
not containing data
tells me that even accessing data in arrays is still foreign to you.