Answer the question
In order to leave comments, you need to log in
How to marry php and js?
Please help me, there is a php code with which I get the elements
<?
if (CModule::IncludeModule("iblock")):
$iblock_id = 53;
# show url my elements
$my_elements = CIBlockElement::GetList (
Array("ID" => "ASC"),
Array("IBLOCK_ID" => $iblock_id),
false,
false,
Array('ID', 'NAME', 'PROPERTY_TAGS', 'DETAIL_PAGE_URL')
);
while($ar_fields = $my_elements->GetNext())
{
echo $ar_fields['NAME'])." ;<br>";
}
endif;
?>
const items = [
{
id: 1,
name: 'провод',
url: 'provod',
tags: ['провода', 'проводов']
},
{
id: 2,
name: 'робот',
url: 'robot',
tags: ['роботы', 'роботов']
},
]
Answer the question
In order to leave comments, you need to log in
if JS is in the body of the page generated by PHP, then just paste it into the output:
index.php
<?php
$result = [];
while ($ar_fields = $my_elements->GetNext()) {
$result[] = $ar_fields;
};
?>
const items = <?php echo json_encode($result); ?>;
Send a GET / POST request to the address of the script from JS, after converting the response to JSON on the server, for example. Next and done. const items = JSON.parse(response)
<?
if (CModule::IncludeModule("iblock")):
$iblock_id = 53;
# show url my elements
$my_elements = CIBlockElement::GetList (
Array("ID" => "ASC"),
Array("IBLOCK_ID" => $iblock_id),
false,
false,
Array('ID', 'NAME', 'PROPERTY_TAGS', 'DETAIL_PAGE_URL')
);
while($ar_fields = $my_elements->GetNext())
{
echo $ar_fields['NAME'])." ;<br>";
}
endif;
?>
<script>
var $my_elements = <?=json_encode($my_elements);?>
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question