Jump to content

PHP variables inside external JavaScript file (workaround?)


Recommended Posts

I have been trying to really separate JavaScript from HTML/PHP, just like with CSS.

My problem, is that I am having problems passing a PHP variable or making use of it through an external JS file.

 

If I was using inline JS I could pass it like:

<a href="bla.php" onclick=return confirm("Delete this item: <?php echo $item ?>")> Delete (works) </a>

 

However, using similar code in an external JS file, simply passes through as text.

Is there any way to accomplish something similar?

 

The new code is much nicer:

<a href="bla.php" class="delete"> Delete (doesn't quite work) </a> 

 

IS there a way to keep code this clean and properly separate the JS from the HTML while at the same time keeping the PHP $variable working?

 

I am well aware that JS runs on the client, and PHP on the server, I am just hoping to somehow pass this variable through some work around, if not I will keep using the inline version.

 

Thank you.

 

[Moderators: Please don't move this to the JavaScript section, it is mainly on topic for PHP and nobody checks those areas - thank you.]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.