0
<script>
var strWidth = document.getElementById("mydiv").style.width;
var strHeight = document.getElementById("mydiv").style.height;
var link = "<?php if(isset($_GET["ggg"])) {
echo $_GET["ggg"].".php?width=800&height=460";
} else {
echo "page1.php?width=800&height=460";
}
?>";
</script>

this is my script, php inside javascript. how do i place this variable strWidth inside

php?width=800&height=460

so becomes some how like this

php?width=strWidth&height=460

EDIT 2

well, the only thing i am trying to do here to show variable value between those line is it a big deal ?

it might be done by separating like using concatenation or something ?

Community
  • 1
  • 1
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Jonnix Mar 09 '18 at 12:10
  • 2
    You need to use Ajax if you want to pass values from JS to PHP – IcedAnt Mar 09 '18 at 12:12
  • wesley you probably don't understand enough yet: javascript operates on the client in "BROWSER LAND", while PHP operates on the server in "PHP LAND" how PHPLAND and BROWSERLAND talk to each is the whole point you're misunderstanding. What let's me know this is I actually *answered* the question for you (and showed you exactly how do to it), but the fact you can't understand my answer means you're not in a position yet to understand the answer to the question behind this issue. – Mr Heelis Mar 09 '18 at 13:50

4 Answers4

0

Add an input-field in PHP, hide it (if necessary) and read the value of this field with JS.

Sascha
  • 4,576
  • 3
  • 13
  • 34
0

you can't really do that. but this works

<?php
 echo "<script>\n";    
 echo "var strWidth = document.getElementById(\"mydiv\").style.width;\n";    
 echo "var strHeight = document.getElementById(\"mydiv\").style.height;\n";    
 if(isset($_GET["ggg"])) {
       echo "var link =" . $_GET["ggg"] . ".php?width=800&height=460';\n";    
 }
 else {
       echo "var link ='page1.php?width=' + strWidth + '&height=' + strHeight + '';\n";    
 }
 echo "</script>\n";    
?>

the reference to ggg completely confuses the understanding of this process so really it should be taken out:

__ page1.php
<?php

 if (!isset($_GET['foundWidth'])){
   //stops double hit
   echo "<script>\n";    
   echo "var strWidth = document.getElementById(\"mydiv\").style.width;\n";    
   echo "var strHeight = document.getElementById(\"mydiv\").style.height;\n";    
   echo "var link ='/page1.php?foundWidth=true&width=' + strWidth + '&height=' + strHeight + '';\n";
   echo "window.location = link;"
   echo "</script>\n";    
 }
 elseif (isset($_GET['foundWidth']) && ($_GET['foundWidth']=='true')) {
     if (isset($_GET['width']) && is_numeric($_GET['width'])){
         //use your width here SERVER SIDE
         // or
         echo "<script> alert('Width is: '+ " . $_GET['width']) . "); </script>\n";           
     }
     if (isset($_GET['height']) && is_numeric($_GET['height'])){
         //use your height here SERVER SIDE
         // or
         echo "<script> alert('Height is: '+ " . $_GET['height']) . "); </script>\n";
     }
 }
?>

using this "trick" you can then write the PHP params into the javascript url with whatever get string you like, including triggering a reload of the page with the width as a param, so if you want to test if $_GET['width'] is set to a number you can insert it etc

Mr Heelis
  • 2,370
  • 4
  • 24
  • 34
0

First of all if you want to use php values in javascript you need the script to be written in a php file. Suppose you do this then you can do this in this way:

<script>
var strWidth = document.getElementById("mydiv").style.width;
var strHeight = document.getElementById("mydiv").style.height;
var link='<?php echo (isset($_GET["ggg"]))?isset($_GET["ggg"]):''; ?>'; // this assigns the valueto link variable
if(link==''){
// your logic starts here
}else{
// your logic starts here
}
</script>
0

Add an input-field and assign a value to the hidden element and then get value through javascript.

It is not a good idea to combine PHP and Javascript.

Refer this about explanation on client-side vs server-side coding.

Darpit P
  • 1
  • 1