how to convert this code to insert it in .js file - javascript

I'm trying to make my website multilingual, i have the php code also and i have translated files, but my site has also js file where is this words which i want to translate, there is example of this php code and how to insert it to js file?
There is JS code
// Update search list
rsParent.html($('<div>').attr({id: 'relatedSearches', class: 'contentBox'})
.append($('<div>').addClass('cbHead').text('Related Searches'))
.append($('<div>').addClass('cbBody').html(list)));
}
});
and the word "Related Searches" i want to replace with this php code
<?php echo $lang['CHARTS']; ?>

It is not possible to do it directly since PHP is a serverside language which is executed once on a webserver, unlike javascript that is executed in client's browser.
What you can do is encode your PHP array $lang to JSON and then output it as inline javascript and assign it to a variable in javascript.
<?php
echo "<script>";
echo "var lang = " . JSON_encode($lang) . ";";
echo "</script>";
?>
Make sure this php code is placed (executed) before your javascript file because variable lang has to be declared before your javascript is executed.

Related

Append html code to file using PHP

I have a Javascript function that takes a string and sends it via ajax to a PHP page. In PHP I would put this string inside the head tag (that already exist) of an html file. This change should be final. How could I do this? I don't know how to manipulate html code via PHP.
<?php
$htmlpage = file_get_contents('http://example.com/index.html');
echo str_replace("<head>", "<head>New text", $htmlpage);
//the result should be from <body></body> to <body>New Text</body>
?>
Also if you have text between body tags you can include it in
str_replace("<body>Old Text", "<body>New Text", $htmlpage)
If you have the string inside a varibale or you are using $_POST then instead of "<head>New text" put "<head>".$_POST['thestring']
Just use if/else statements and echo string from ajax to your tags if you can't use JS for this.
One way around this would be to use file_get_contents, then do a string replace on the tag with the amended code, and then write the changes with the ammended code.
Example
<?php
$file = "myhtmlfile.html";
$original = file_get_contents($file);
$needle = "<head>";
$new_content = "<head>\n".$_POST['header'];
$replacement = str_replace($needle, $new_content, $original);
file_put_contents($replacement, $file);
?>

Internationalization inside html function of JQUERY

I'm doing the internationalization of a PHP project.
in PHP I use the getttext() function and poedit program.
example:
<?php
echo gettext("Hello world");
?>
Hello World will be the key with the associated translated words:
It all works.
I haven't any idea how to translate the .js file with html function of JQUERY.
For example:
inside javascript file, I have
if(exchange=='mo'){
$(#hopen.title).html("NEW WORD");
}
the question is: How can I call the gettext function to "NEW WORD" and then use it with poedit program?
You don't want to call gettext / php from your javascript file every time you need a translation as that would mean you would have to make an ajax request to the server for each text.
Instead, you could generate an object in php with all the keys and translations that you need and send that to the browser and make it available as a global variable in javascript.
<?php
...
$needed_translations = array(
'Hello world' => gettext("Hello world")
...
);
?>
<script>
var needed_translations = <?php echo json_encode($needed_translations); ?>;
// or
window.needed_translations = <?php echo json_encode($needed_translations); ?>;
</script>
If your javascript file is loaded after this or your code is located in a document ready block, you will have access to this global variable and can use it wherever you want:
if(exchange=='mo'){
$(#hopen.title).html( needed_translations['Hello world'] );
}

Is it possible to use PHP to generate JavaScript to generate PHP successfully? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
Using PHP to generate JavaScript works as expected, and using JavaScript to generate PHP works as expected, however using PHP to generate JavaScript to generate PHP does not seem to work.
Is there a problem with my code which is causing this to fail, or is it some sort of limitation?
<!-- Use JavaScript to generate PHP -->
<script type='text/javascript'>
function addComment()
{
alert("About to add some content");
document.write("<?php testFunction(); ?>");
}
</script>
<!-- Use PHP to generate JavaScript to generate PHP -->
echo "<script type='text/javascript'>";
echo "function addComment2()";
echo "{";
echo 'alert("About to add some content");';
echo 'document.write("<?php testFunction(); ?>");';
echo "}";
echo "</script>";
(testFunction() simply contains echo "Test";)
PHP is code executed on the server side. JavaScript is code executed on the client side. They don't know nothing about the other.
You can generated with PHP nearly whatever you want.
You problem is the fourth echo:
echo 'document.write("<?php testFunction(); ?>");';
You tell echo to print this string:
document.write("<?php testFunction(); ?>");
This string is sent to the client (and browser). The browser (the javascript engines) runs this code and results in an output like this:
<?php testFunction(); ?>
We have already left the php interpreter on your server, because we are already on the client. The client can't to anything with this.
If you replace your line with this:
echo 'document.write("', testFunction() ,'");';
your code will executed as expected.
But for this you have change your testFunction. Just replace
function testFunction() {
return "Test";
};
and the output to the server will be
document.write("Test");
Using JavaScript to generate PHP does NOT work. In the first case it's executed as php first, so your testFunction() is run, this resulting javascript is sent to the browser and executed.
You don't seem to understand the Server-Client model. Javascript is executed on the client, while PHP is executed on the server. The client cannot execute PHP and also cannot send PHP code to the server to be executed. It is just not possible.

Assigning php variable to javascript alerts PHP source code

I want to assign the php variable $stop to the javascript variable stopped
<?php
$stop = $_POST['stop'];
?>
<script>
var stopped = "<?php echo($stop) ?>";
alert(stopped); //Output: alertbox with the text: <?php echo($stop) ?>
</script>
How can I solve this?
Since the PHP source code is being alerted, the document is not being run through the PHP preprocessor before being delivered to the browser.
Make sure that:
You're using a web server and not loading from a local file
The webserver supports PHP
The file the PHP code is in is one the server expects to find PHP in (typically this will be a file with a .php extension, but it is configurable).

How to get PHP string value in javascript?

Hi have looking on various questions but none of them seem to help me. I have a php variable in my php code and I am trying to access that in my javascript when I do. . .
var thing = "<?php echo($phpvariable); ?>";
then when I do
alert(thing);
It comes out to be "<?php echo($phpvariable); ?>" in the alert statement
What am I doing wrong?
Your PHP is obviously not being parsed. Are you in a .php file? If you're in a .js file, you'll need the server to parse those (or, more safely, put the PHP part somewhere in the DOM that the JS can access)
However, you're doing it wrong:
var thing = <?php echo json_encode($phpvariable); ?>;
Note: no quotes. json_encode will take care of that for you.
If this code is in a function in javascirpt that executes on click or at a specific event, then:
You are writing PHP Syntax in javascript, there is no way that you load the page then you run the php code. PHP code runs on the server side, so before any other HTML Javascript code executes
Else if you want to dynamically set the variable thing in javascript when the page is first loaded, then most probably you meant to write in the php file:
var thing = <?php echo '"'.$phpvariable.'"'; ?>;

Categories

Resources