I have an external JavaScript file, I would like to display array content in my PHP page. How can I display the content by loop. I tried to display the content using following code. But Failed.
<?php
ini_set('allow_url_fopen', 'On');
$feed = file_get_contents("https://example.com/media-list.js");
$array = json_decode($json);
var_dump($array);
$urlPoster=array();
foreach ($array as $value) {
$urlPoster[]=$value->urlPoster;
}
print_r($urlPoster);
?>
media-list.js content
var contents = {
"0W7EHsR8":{
"attachmentCode":"0W7EHsR8",
"title":"title1",
},
"vciym7Zb4":{
"attachmentCode":"vciym7Zb4",
"title":"title2",
},
"XlKBM6":{
"attachmentCode":"XlKBM6",
"title":"Title3",
}
};
I would like to display content like
Title is "title1" .
Title is "title2".
You are trying to use json_decode() on a Javascript Array. JSON and Javascript are two different things.
You need to convert your contents array to JSON, save it in a different file (contents.json for example) and load that file with file_get_contents.
If you need to do this only once you can do something like
var contents = {
"0W7EHsR8":{
"attachmentCode":"0W7EHsR8",
"title":"title1",
},
"vciym7Zb4":{
"attachmentCode":"vciym7Zb4",
"title":"title2",
},
"XlKBM6":{
"attachmentCode":"XlKBM6",
"title":"Title3",
}
};
console.log(JSON.stringify(contents));
and copy the result from the console (click CTRL+Shift+I to open it).
JSON.stringify() takes a Javascript array and turns it into a JSON string.
for this small array the JSON equivalent is
{"0W7EHsR8":{"attachmentCode":"0W7EHsR8","title":"title1"},"vciym7Zb4":{"attachmentCode":"vciym7Zb4","title":"title2"},"XlKBM6":{"attachmentCode":"XlKBM6","title":"Title3"}}
If you intend on making this dynamic (say, loading all attachemnts from a database) you can create a php file that loads that data into an array and returns it into proper JSON using json_encode(). Loading that file with file_get_contents and using json_decode like you are doing now should do the trick.
I'm trying to create a js that send data to a php.
My first problem is that I get get back a html code if I insert this to the php.
This is only for understand the idea. So the js should send the "param" to the php and the php should return the result6 variable in this case, but I get a html code...
$('#f_field').change (function()
{
var param = 28;
$.post('check_mutet.php', param, function(result6) {
alert(result6);
});
});
while check_mutet.php contains this
<?php
$result6=666;
echo $result6;
Thank you for your help, as you can see I'm rather noob :)
param is a plain string (it starts out as a number, but will be converted to a string by the time it gets through to HTTP).
This will be available as STDIN in PHP, which you can read as described in answers to this question.
However, you should encode the data into a structured format that is easier to use.
The traditional format for this is application/x-www-form-urlencoded, which is the default format sent by HTML forms.
If you pass an object to jQuery post, it will encode the data in that format for you:
var param = 28;
var data = { example: param };
$.post('check_mutet.php', data, function(result6) {
Then you can read it through the POST superglobal in PHP:
<?php
$result = $_POST['example'];
I got stuck when using ajax as primary request sender to php.
I was trying to send data to php file and then take that data to view in another file, but the problem is I don't know how to do it .
I tried
var ajax = ajaxObj("POST", "function.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
window.location = "view.php";
}
}
ajax.send("a="+a+"&b="+b);
it locate view.php and I want to take contents that sent
function.php and view it's contents in view.php.
That is where my problem occurs.
So, my question is HOW TO TAKE THOSE CONTENTS
I think you don't understand the use of ajax... here you don't need ajax at all. Ajax is used to update/send content without refreshing the page. So if you want to pass data to a new php file, just pass the variables directly to view.php in your case.
For instance:
go to view.php?a=[your value]&b=[your value]
in view.php you can get the values:
$a = $_GET['a'];
$b = $_GET['b'];
and pass it to your function.php
Use Jquery to avoid dealing with browsers specifications and also jquery is pretty popular and simple to use library
Assuming you are familiar with php and JavaScript
first you have to do all this work in you www or htdocs folder depending on the stack you are using whether it is XAMPP,WAMP or LAMP
let's say as an example we want to send post request to function.php file and that file should verify the existance of $_POST['username'] and return the length of username otherwise it returns 0
so the function.php file should be something like this :
if(isset($_POST['username'])){
echo strlen($_POST['username']);
}else{
echo 0;
}
and the view.php file is the file used to send post request and get the response and displays it in a #display div
so here is the content of view.php
// first you should include jquery
// here is a cdn link
// https://code.jquery.com/jquery-2.1.3.min.js
// or you can use a local jquery file it is up to you
<div id="display">
</div>
// here is the simple js code to send and get the data from function.php file
<script>
$(document).ready(function(){
// waiting for the document until it is ready to use
$.post('function.php',{username:'myusername'},function(data){
// data here is the returned data from the function.php file
$('#display').text('username length is : '+data);
});
});
</script>
My goal is to generate an html file in javascript (based on form inputs), and use Angular's $http to send this generated file to the server (php) for storage. Is this possible?
So far I've tried generating HTML strings on the client side and send the string over, and use php's file_put_contents() to generate the html file. But I would like to generate the html file on the client side. As the html gets more complex, I don't want to be sending long and complex strings.
Javascript (inside the controller)
$http({
url: "/test.php",
method: "POST",
data: {"content":"<h1>hello world</h1>"}
});
test.php
<?php
$input = file_get_contents("php://input");
$data = json_decode($input);
file_put_contents("index.html", $data->content);
?>
I prefer to generate the html file on the client side because I want to reduce server load.
Thanks for any advice.
You can use Twig for PHP templating. Or PHP Plates - http://platesphp.com/
Read the POSTed data from PHP and generate using the template.
Pseudo codes:
<?php
$input = $_POST['content'];
$html = generateTemplate($input);
file_put_contents($fileName, $html);
?>
I have a problem with sending an object in PHP. I stringify the object before sending it to the PHP file.
The PHP file then uses json_decode. But the decode shows a blank variable.
The object which i console.log shows this as its structure:
Its then sent to PHP with this :
console.log(my_Obj);
var as = JSON.stringify(my_Obj);
call_data('add.php?&as='+as, nxtFunc);
Now in the PHP file i have this which handles the situation:
$path = json_decode($_GET['as']);
echo $_GET['as'].'<br/>';
print_r($path);
die;
The result is:
[null,null,{\"8\":[null,null,null,null,null,null,[],[],[],[],[]],\"9\":
[null,null,null,null,null,null,null,null,null,null,[]],\"10\":
[null,null,null,null,null,null,null,null,null,null,[],[]],\"11\":
[null,null,null,null,null,null,null,null,null,null,null,[]]}]
<br/>
My XHR request url in Chrome shows:
add.php?as=[null,null,{%228%22:[null,null,null,null,null,null,[],[],[],[],[]],%229%22:[null,null,null,null,null,null,null,null,null,null,[]],%2210%22:[null,null,null,null,null,null,null,null,null,null,[],[]],%2211%22:[null,null,null,null,null,null,null,null,null,null,null,[]]}]
Notice the print_r shows nothing. Should i not be using stringify ?
Thats because my_Obj is an array and not an object.
Try this:
var as = JSON.stringify({data: my_Obj});
Note:
You will also need to clean up the array before stringifying - i.e. clear out all null/undefined indices. Check this answer: https://stackoverflow.com/a/281335/921204