This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
Can I please have some help in using a PHP variable in some Javascript code.
i want to use php code in javascript function how to use php scripts in javascript function
for example i want to use php code in this javascript function
function rank()
{
<?php echo'hello';?>
}
Try using like this
function rank()
{
var someVariable = "<?php echo 'hello';?>";
}
try this (better & shorter)
function rank()
{
var variable="<?='hello';?>"
}
You can use like this:
function rank()
{
var rankVar;
rankVar = "<?php echo'hello'?>";
}
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I have a piece of code, and I need to call a PHP code snippet when this function is called. Basically, it would be like this:
function jsFunc(){
//do PHP stuff
}
I don't know if this is a trivial thing, and I'm just not getting it, but I would appreciate any help.
If you want to do that in a .js file you can organize with Ajax calls
Here great example https://www.w3schools.com/js/js_ajax_php.asp
or if you use jQuery library
https://www.w3schools.com/jquery/jquery_ajax_load.asp
https://www.w3schools.com/jquery/jquery_ajax_get_post.asp
Do the following:
function jsFunc(){
var php_code = <?php
// your php code
?>;
}
EDIT: To call an object within the php code, use
echo $php_object
as suggested by noobcode.
Run it like:-
function jsFunc(){
//do PHP stuff
$pp = <?php echo "hello"; ?>;
console.log($pp);
}
Now you can easily use your function where i have used echo.
This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
Suppose you are in a php file and you want to run a javascript function that has both javascript and php inside of it. How would you go about doing this?
Ex:
<?php
<script>
function myFunction(){
// php code
document.write("Hello World!");
}
</script>
?>
I know I can easily make a php function for this particular code, but just for future reference, I would like to know how to do this.
I apologize if this has been answered before but it seemed difficult to word in google search.
The PHP code has to output text that is valid inside the javascript. Something like this.
<?php
<script>
function myFunction(){
// php code
var message = "<?php echo "My alert message"; ?>";
window.alert(message);
}
</script>
?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.
Thanks
There are several ways you can do this. Probably the simplest way is:
var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;
Then your script 'otherscript.php' can access that variable with $_GET['myData'].
This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.
Something like that should work:
<script type="text/javascript">
function yourFunction(yourParameter) {
<?php $abc = "<script>document.write(yourParameter)</script>"?>
}
</script>
<?php
echo $abc;
?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I'm trying to get the browser's width and then compare it as a php variable, but it is saving it as a string, I tried parsing it to another variable but it only returns 0.
$tam='<script>var j= $(window).width(); document.write(j)</script>';
I'm doing the parsing like this:
$ntam=(int) $tam;
You can't save a javascript value into a php variable directly. But you can....Put the php value into a javascript variable and use that for making your changes using javascript.
<?php echo '<script type="text/javascript">var jVar = "$phpVar";'; ?>
If you are importing your script from a js file you will want to add this before your import or course.
And I'm not sure what you're trying to accomplish but if you need a js value you can always send that value using an ajax Request...
var ajax = new XMLHttpRequest();
var form = new FormData();
form.append('win_size',window.innerWidth);
ajax.open("http://example.com/processJSWindowSize.php","POST",true);
ajax.send(form);
On the server side code, which in this example is processJSWindowSize.php, you can have something along the lines of
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$jsWindowSize = strip_tags(stripslashes($_POST['win_size']));
//do what you need with the window size
}
?>
Your script needs to return the width and it needs to be a function
<script>
var tam= function()
{
var j= $(window).width();
document.write(j)
return $(window).width();
}
var ntam = tam;
</script>