Make PHP print names in certain classes

Asked

Viewed 96 times

-3

The following Javascript code has the function of print a text in the elements with the class exibir:

$(document).ready(function(){
        $(".exibir").text("Olá Mundo!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="exibir">Isso é um parágrafo.</p>
<p class="exibir">Isso é um parágrafo.</p>

The code I mentioned type "maqueia" the texts contained, wanted to do the same, only in PHP, because I am creating a page that needs to be printed already from the server (PHP), instead of being changed after loading the page (Javascript).

How to do this same function in PHP ?

I am creating a page that contains 4 identical texts in the body of the page.

I need to somehow set what will be displayed in these 4 texts in the page header, IE, there in the header "<head>" I can define via PHP, what will be displayed in these 4 identical texts that are in the body of the page "<body>".

  • 1

    Use a DOM interpreter is an option?

  • Any answer that might solve the problem is always welcome :)

  • I cannot answer your question, as it is pending. Your Javascript example gives us an idea of what you want to do, but it would help clarify exactly what you want to do if you put some example of what you tried to do in PHP.

  • @Panther Give a vote there to reopen :P, worse than I tried to do in PHP, because I’m new to PHP.

1 answer

2

Avascript runs through the user’s browser.

PHP runs on the so-called "backend", that is, you need a request to the server where PHP is located.

There is no function like in Javascript, able to interact directly with the DOM of the HTML page displayed in the browser.

What I could do is, something like that:

$(document).ready(function(){
        $(".exibir").text("<?php echo $_GET['nome'];?>");
});

But it is not very clear what you really want because there are different ways to go about it. The most appropriate way depends on the ultimate goal, beyond the circumstances involved.

  • Thank you so much for the reply Dani! : ) I will try to explain to you better, the code I mentioned type "maqueia" the texts contained, I wanted to do the same, only in PHP. Example, I put "Good morning!" in one place in PHP, and then the classes that have the class exibir, will have their texts changed to "Good morning!". It’s because I’m making a page, and this text should already be printed on the page, rather than printing after the page is loaded. Ah, Daniel, forgive me anything, I know that sometimes you get a little annoyed with my questions and so... is that I’m new to PHP, and I need help. :'(

Browser other questions tagged

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