Replace Link Text With PHP code

Hello, i wonder how i can replace the link text with PHP code. I have this code:

<div class='rmm' data-menu-title = "Navigation" data-menu-style = "custom"> <!-- data-menu-title = Input your Menu text (This is the text to the right of the mobilestate menu)-->
<ul><li id="current_yes"><a href="#">HEM</a></li></ul>
</div>

And i want it to look loke this:

<div class='rmm' data-menu-title = "Navigation" data-menu-style = "custom"> <!-- data-menu-title = Input your Menu text (This is the text to the right of the mobilestate menu)-->
<ul><li id="current_yes"><a href="#"><?php echo $lang['MENU_HOME']; ?></a></li></ul>

So how to change this with jQuery on page load?

Thank you.

The PHP code wonā€™t be there by the time the page reaches the browser. So itā€™s not really clear what you are asking here.

Thankā€™s for replying. Well i dont realy know how to explain it.
I have tryed some codes like:

<script type="text/javascript">
$(document).ready(function () {
$("#current_yes a").replaceWith('<?php echo $lang['MENU_HOME']; ?>');
}
</script>

But no luck. If you look at this java script code, do you now understand what i am after?

Thank you.

You need to escape the quotes. Do something like this.

<script type="text/javascript">
$(document).ready(function () {
$("#current_yes a").replaceWith("<?php echo $lang['MENU_HOME']; ?>");
}
</script>

Hello, and thank you. Itā€™s not working. If you look at my first code, it is the text HEM i whant to change. Also i have at start of page this: <?php include_once 'common.php';?> and in this file i have this: $lang[ā€˜MENU_HOMEā€™] = ā€˜Homeā€™; So i want the java script to change the text HEM to whatever is in the common.php/$lang[ā€˜MENU_HOMEā€™] = ā€˜Homeā€™; file, so that i can change the TEXT in common.php file if i want to. Maybe i change it to $lang[ā€˜MENU_HOMEā€™] = ā€˜NACH HAUSEā€™; then the HEM text would be NACH HAUSE.

Hey i got it like this:

<div class='rmm' data-menu-title = "Navigation" data-menu-style = "custom"> <!-- data-menu-title = Input your Menu text (This is the text to the right of the mobilestate menu)-->
<ul><li id="current_yes"><a href="#" id="1">HEM</a></li></ul>
</div>

<script>
var word = 'HEM';
var sent = $('#1').html().replace(word, '<?echo $lang['MENU_HOME']; ?>'); 
$('#1').html(sent);
</script>

FYI itā€™s invalid to have id=ā€œ1ā€. Ids must not start with a number.

Thank you, iā€™l change this to ONE then

Hi Patrick,

You cannot.
A browser requests http://yourdomain/myscript.php
The PHP code in myscript.php is parsed on the server to produce HTML.
This HTML is then returned to the browser.
At this point you can interact with the HTML using JavaScript.

Maybe you can give a short before and after example of how you would like the HTML (not PHP) to look, or otherwise outline what you are trying to accomplish.

Hello Pullo. I got it working, se above.

And that does what you want?
Unless Iā€™m missing something obvious, that will replace the string HEM with the string <?echo $lang['MENU_HOME']; ?> but the value of $lang['MENU_HOME'] wonā€™t be evaluated.

Yes this does what i want, and yes it works as you think. And i also in the same file have this: <?php include_once 'common.php';?> and in this file i have this: $lang[ā€˜MENU_HOMEā€™] = ā€˜HOMEā€™;

Yeah, but if this is taking place on the client, then $lang['MENU_HOME'] can be set to anything you like - it wonā€™t be evaluated.

Given:

<a href="#" id="1">HEM</a>

And:

var word = 'HEM';
var sent = $('#1').html().replace(word, '<?echo $lang['MENU_HOME']; ?>'); 
$('#1').html(sent);

This will produce this HTML:

<a href="#" id="1"><?echo $lang['MENU_HOME']; ?></a>

If the JavaScript is in a file that gets parsed as PHP on the server first then the content of that PHP variable at the time the PHP is run will be substituted into the JavaScript and it is that value the JavaScript will then use when it runs.

The PHP code runs first long before the JavaScript and so will never be there by the time the JavaScript updates the HTML.

Exactly, so if youā€™re attempting to insert PHP into the HTML using jQuery, then the inserted PHP cannot possibly be parsed as PHP, right?

I have on several occasions mixed PHP and JavaScript together like tho OP has shown. It even works with the JavaScript in a separate file provided that file will get parsed for any PHP it contains.

A simple example for using an external file to feed PHP into JavaScript :

<div id="one"></div>
<script src="myscript.php"></script>

to attach the file and then put a PHP header in the top of myscript.php to identify the content to the browser as JavaScript:

<?php
header('Content-Type: appplication/javascript');
$phpvar = 'something';
?>
document.getElementById('one').innerHTML = "<?= $phpvar ?>";

End result of this code would be to insert ā€œsomethingā€ into the div as that is the value that $phpvar had at the time the PHP ran and therefore was the value inserted into the JavaScript at the point where that variable was referenced before sending the JavaScript to the browser.

Well i dont know what to tell you, it works for me.

Thank you.

Iā€™l try to explain better what i got. I have a file "java_php_test.php in this file i have the code:

<?php include_once 'common.php';?> and i also have a code for a Responsive menu. And i'm using a software to make the webpage. To this software there's an add on software called the Extension builder. U can use this to make Extensions/Add ons to the main software. So the values for the Menu Links/Items is coming from an XSL file this file is called menu_php.xsl, it's a little difficult to explain exactly how this works but the code in this file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match='/'>
<ul>
<xsl:for-each select="DATASET/ITEM">
<li id="{SELECTEDMENU}"><a href="{URL}" id="{LINKNUM}"><xsl:value-of select="MENUNAME"/></a></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>

But i cant put in the <?echo $lang['MENU_HOME']; ?> code to the XSL file, so this i wy i need a java script to change the text.

//Responsive Menu Code Looks Like This:

<link rel="stylesheet" href="rmm-css/responsivemobilemenu.css"/>
<script src="rmm-js/responsivemobilemenu.js"></script>
<div class='rmm' data-menu-title = "Navigation" data-menu-style = "custom">
<ul><li id="current_yes"><a href="#" id="one">HEM</a></li><li id="current_no"><a href="#" id="two">KONTAKTA OSS</a></li></ul>
</div>

Then i have this code to change the HEM and KONTAKTA to HOME and CONTACT US:

<script>
var word = 'HEM';
var sent = $('#one').html().replace(word, '<?echo $lang['MENU_HOME']; ?>'); 
$('#one').html(sent);
</script>
<script>
var word = 'KONTAKTA OSS';
var sent = $('#two').html().replace(word, '<?echo $lang['MENU_CONTACT_US']; ?>'); 
$('#two').html(sent);
</script>

And the code in the common.php file look like this:

<?php

session_start();

header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))

{

$lang = $_GET['lang'];

// register the session and set the cookie

$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));

}

else if(isSet($_SESSION['lang']))

{

$lang = $_SESSION['lang'];

}

else if(isSet($_COOKIE['lang']))

{

$lang = $_COOKIE['lang'];

}

else

{

$lang = 'en';

}

switch ($lang) {

  case 'en':

  $lang_file = 'lang.en.php';

  break;


  case 'de':

  $lang_file = 'lang.de.php';

  break;


  case 'es':

  $lang_file = 'lang.es.php';

  break;

  
  case 'se':

  $lang_file = 'lang.se.php';

  break;

  default:

  $lang_file = 'lang.en.php';

}
include_once 'languages/'.$lang_file;
?>

i then have 3 files that are named: lang.se.php, lang.en.php and lang.de.php the code in these files looks like this:

<?php
/* 
------------------
Language: English
------------------
*/

$lang = array();

$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
$lang['SITE_NAME'] = 'My Website';
$lang['SLOGAN'] = 'My slogan here';
$lang['HEADING'] = 'Heading';

// Menu

$lang['MENU_HOME'] = 'HOME';
$lang['MENU_ABOUT_US'] = 'About Us';
$lang['MENU_OUR_PRODUCTS'] = 'Our products';
$lang['MENU_CONTACT_US'] = 'CONTACT US';
$lang['MENU_ADVERTISE'] = 'Advertise';
$lang['MENU_SITE_MAP'] = 'Site Map';

// MAIN TEXT 1

$lang['MAINTEXTONE'] = 'This is a test for the main text 1';
?>

this is the lang.en.php file the others are just translated.

Then i have links to these files like this: http://test3.fcab.se/menu_lang_test/java_php_test.php?lang=se
http://test3.fcab.se/menu_lang_test/java_php_test.php?lang=en
http://test3.fcab.se/menu_lang_test/java_php_test.php?lang=de

I dont know if this helps or only complicates things but i can promise that it works.

Thank you all for your help.

Demo page here

Cool. I knew you could use header() to send something other than HTML (such as an image), but I hadnā€™t thought of manipulating JavaScript this way.

Thanks for the explanation. It seems you are trying to do i18n - couldnā€™t you avoid JavaScript in this way altogether?

E.g.

<?php
$lang = $_GET["lang"];

echo $lang;

$words = array(
  "de" => array('Hello' => 'Guten Tag', 'Bye' => 'TschĆ¼ĆŸ'),
  "fr" => array('Hello' => 'Bonjour', 'Bye' => 'Aurevoir')
);

echo $words[$lang]["Hello"] . "<br>";
echo $words[$lang]["Bye"];

Hello, I will try this. Thank you.