PHP variable in HTML without form

Asked

Viewed 36 times

-1

I have two pages 1st Html and 2nd PHP where I am treating some XML data returned after reading an XML file, but I have a question, I need to take a value that I take from XML and put inside an html tag, I’ve done like this and nothing

the PHP page is being called in HMTL(its extension and .php) by require_once.

SNIPPET OF HTML

<table>
  <caption >Ti<?php echo $cnpj ?></caption>
  <tr>
    <td>John Lennon</td>
    <td>Rhythm Guitar</td>
  </tr>
  <tr>
    <td>Paul McCartney</td>
    <td>Bass</td>
  </tr>
  <tr>
    <td>George Harrison</td>
    <td>Lead Guitar</td>
  </tr>
  <tr>
    <td>Ringo Starr</td>
    <td>Drums</td>
  </tr>
</table>

EXCERPT FROM PHP

$arq = simplexml_load_file('29190411412201000112650010000000181000000180-nfe.xml') or die("Erro ao carregar arquivo XML");

foreach($arq->NFe->infNFe->children() as $infNFe){
$cnpj = $infNFe->CNPJ;

}

HOW TO CALL A PHP VARIABLE VALUE IN HTML TAG ?

  • Links to better understand how Sopt works: [Tour], [Ask], Manual on how NOT to ask questions and [Help]. [Edit] the post and reduce the code to a [MCVE] can help a lot to enable the question.

1 answer

0


With a simple ECHO :

test.xml :

<?xml version="1.0" encoding="UTF-8"?>
<Nfe>
    <dados>
        <cliente>fulano</cliente>
        <cnpj>1234</cnpj>
    </dados>
</Nfe>

test php.

<?
$xmldata = simplexml_load_file("test.xml") or die("Failed to load");

$cnpj = $xmldata->dados[0]->cnpj;
$cliente = $xmldata->dados[0]->cliente;
?>


<h2> <? echo $cnpj; ?> </h2>
<h2> <? echo $cliente; ?> </h2>
  • I’ve done it before, but nothing happens .

  • Rename your . html in . php

  • already like . php page containing HTML

  • You tested the variables php ? $cnpj exists ?

  • Before the foreach I created it so empty $cnpj='';

  • Create a php like this, and test: <? $cnpj = "11111111111111"; ? > <H2> <? echo $cnpj; ? > </H2>

  • html --> <body> <H1>TESTE <? echo $cnpj; ? ></H1>

  • PHP -- $cnpj='1111111111111';

  • did not work, expensive and easier to read an XML file by PHP or using Java script ?

  • Tested works very well !

  • The test I gave you works ? Or the problem is time to read the xml ?

  • in the case with foreach I can already read the xml if I give an echo inside the foreach picking the XML tags works, but when I assign the variable $cnpj to the returned result of the XML reading I can’t put to appear on the page, type so I am reading an XML processing with PHP and the processed data I want to make appear in the HTML page

  • the test you gave me didn’t work.

  • The forEach and within a Function the same way you put in the question ?

  • without Function, the way it’s in question.

  • In your HTML snippet you didn’t put the ; after $cnpj ==> <?php echo $cnpj; ?>

  • put -- ><H1>TEST <? echo $cnpj; ? ></H1>

  • OK I made a little test example that works ! Give a look !

  • In case you took the reading of an external XML file did the processing in PHP and in the PHP page itself you are printing the right reading result ?

  • yes! exactly ! And it works!

  • Caraca, I’m doing the same here and nothing(shame), bad thank you and beast, just one more thing you know how to read an XML this way with Java script ? if you know send me a link for reference.

  • With Ajax : https://www.w3schools.com/xml/ajax_xmlfile.asp

Show 17 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.