Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
57
Q:

javascript - How to prevent PHP variables from showing up on HTML

Hey everyone,

I recently started working on a web project where I'm using both PHP and JavaScript. I have encountered a slight issue that I'm hoping you can help me with.

So basically, I have some PHP variables that are being used to store certain values fetched from a database. These variables are then passed onto my HTML page. The problem is that when I view the page source, I can see the PHP variables being displayed as plain text.

Is there any way to prevent the PHP variables from showing up in the HTML source code? I want to keep this information hidden and only accessible through JavaScript.

I appreciate any suggestions or solutions you can provide. Thank you!

All Replies

champlin.bianka

Hey,

I completely understand your concern about PHP variables showing up in the HTML source code, and I've encountered a similar issue before. Here's another approach you can try out based on my personal experience.

Instead of directly echoing the PHP variables in your HTML, you can utilize JavaScript to retrieve the values from the server using AJAX. One way to achieve this is by making a separate AJAX request to a PHP file, which will handle the database query and return the desired values.

In the JavaScript code, you can use an event handler like `DOMContentLoaded` or `window.onload` to trigger the AJAX request once the webpage finishes loading. Then, update the HTML elements with the retrieved data using JavaScript DOM manipulation methods like `getElementById` or `querySelector`.

By adopting this technique, the PHP variables won't appear directly in the HTML source code, providing an additional layer of security. Users would only see the JavaScript code responsible for fetching and populating the data, rather than the actual PHP variables.

I hope this alternative solution works for you! Don't hesitate to reach out if you require further assistance or clarification.

mafalda.boyle

Hey there,

I had a similar issue with PHP variables showing up in the HTML source code, and I found a solution that might work for you. What you can do is separate your PHP code from the HTML code by using AJAX to fetch the values you need from the server.

You can create a separate PHP file that retrieves the necessary data from the database and returns it as a JSON response. Then, in your HTML file, you can use JavaScript to make an AJAX request to that PHP file, retrieve the JSON data, and populate your webpage accordingly.

By doing so, the PHP variables won't be visible in the HTML source code, as the data will be fetched dynamically using JavaScript. This way, users won't be able to see the PHP variables when they view the page source.

I hope this helps! Let me know if you need further clarification or assistance.

New to LearnPHP.org Community?

Join the community