-2

PS: the full code in the end

i have 2 pages.

php.php

js.php

i sent variables from php.php to js.php by using curl

 function js(){
 $ch = curl_init();
 curl_setopt($ch , CURLOPT_URL ,"http://localhost/js.php");
 curl_setopt($ch , CURLOPT_POST , true);
 curl_setopt($ch, CURLOPT_POSTFIELDS," ");
 curl_setopt($ch , CURLOPT_FOLLOWLOCATION , TRUE);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $allaa= curl_exec($ch);
 $errorr = curl_error($ch);
 echo $allaa;
 return $allaa; 
 }

js.php contain Javascript code

i passed value that i received from first page to Javascript in second page

by this code

 var variable=<?php echo  json_encode($variable); ?>;

i made some processes on the variable and the result i print it by this code

 variable="Hello World";
 document.getElementById("result").innerHTML = "!!!"+variable+"!!!";

js.php contain

html code

 <p id="result"> </p>

it must the result of Javascript appear in the tag like this

 <p id="result">!!!Hello World!!! </p>

in the php.php page i stored the returned value in variable x

and i printed it

 $x=js();

 $xb = get_string_between($x, "!!!", "!!!");
 echo xb;

the result must be

Hello World

but its not its

"+variable+"

how to fix this problem

new edit this is the code

php.php page code

  <?php
  function get_string_between($string, $start, $end){
  $string = ' ' . $string;
  $ini = strpos($string, $start);
  if ($ini == 0) return '';
  $ini += strlen($start);
  $len = strpos($string, $end, $ini) - $ini;
  return substr($string, $ini, $len);
  }
  function js(){
  ////////////////
  $ch = curl_init();
  curl_setopt($ch , CURLOPT_URL ,"http://localhost/js.php");
  curl_setopt($ch , CURLOPT_POST , true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,"variable=hello");
  curl_setopt($ch , CURLOPT_FOLLOWLOCATION , TRUE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $allaa = curl_exec($ch);
  $errorr = curl_error($ch);
  echo  $allaa;
  return $allaa;    
  }

  $ff=js();
  echo $xb = get_string_between($ff, "!!!", "!!!!");
  ?>

js.php page code

  <?php 
  global $variable;

  $variable=$_POST['variable'];

  ?>
  <html>

  <script>
  var variable=<?php echo  json_encode($variable); ?>;
  variable=variable+" world ";
  document.getElementById("result").innerHTML = "!!!"+variable+"!!!!";
  </script>
  <p id="result"> </p>

  </html>
  • Is there a reason you try to do it this way? – Bolli Sep 02 '16 at 00:44
  • yes , the big reason is i want to use a javascript function called JSEncrypt() its just for javascript and i want to process a value from php by this value i want to proccsess many variables and all this in one page without changing the current page – Mohammed Naji Sep 02 '16 at 00:53
  • `!!Hello World!!` has two `!` either side ... `get_string_between($x, "!!!", "!!!");` looks for **THREE** times `!` .. `!!` can't be found by looking for `!!!` (P.S. this comment looks like brainf*ck code!!) – Jaromanda X Sep 02 '16 at 00:59
  • check the code now :) – Mohammed Naji Sep 02 '16 at 01:01
  • I mean is there a reason to use curl? But in javascript you need to define variables with: var variable="Hello World"; – Bolli Sep 02 '16 at 01:02
  • i think the resone for curl is that i want to do it in one page without interrupt the processes by moving from page to page ,and i'm beginner in javascript – Mohammed Naji Sep 02 '16 at 01:05
  • Using jquery it is quite easy. You can send requests to php files like I do here https://stackoverflow.com/questions/39161485/php-function-return-value-to-html-tag/39162071#39162071 then javascript does the the POST request to PHP and PHP returns it to Javascript, which can then manipulate the DOM (after the success function on that answer) – Bolli Sep 02 '16 at 01:11
  • i will try ,but it will fix the problem? i mean get the html value after processing by javascript to get it back to php – Mohammed Naji Sep 02 '16 at 01:22
  • Try reading about https://stackoverflow.com/questions/4584373/difference-between-window-load-and-document-ready-functions I'm not sure my other link is what you asked for – Bolli Sep 02 '16 at 01:37

2 Answers2

0

I think that script from js.php is not interpreted on server side and you get this script as plain text where between "!!!" is only "+variable+".

Tomek B.
  • 145
  • 5
0

This question is difficult to answer because it's all broken up and we can't see the full picture. However, I'll try my best to dissect what you're asking. First off, you have this code in php.php:

function js(){
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL ,"http://localhost/js.php");
curl_setopt($ch , CURLOPT_POST , true);
curl_setopt($ch, CURLOPT_POSTFIELDS," ");
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec($ch);
$errorr = curl_error($ch);
echo $allaa;
return $allaa; 
}

I've a few issues with this. First, though not entirely relevant, is that CURLOPT_URL can be set with curl_init() instead. Second is that you have empty post fields (before your edit it was $post which is equally as cryptic). Third, you echo and return a variable called $allaa. Nobody knows what this variable is or where is came from.

Anyways, apparently there's a second page you have called js.php which contains some JavaScript. Let's assume the correct information was sent over via POST from the js() function.

You have somewhere in your js.php file code that looks like this:

var variable=<?php echo  json_encode($variable); ?>;

I'm assuming that this is somewhere between <script> tags. So, what is this $variable? We have no hint as to what it is. You seem to imply that the desired information from js() wound up in $variable though. This is where I would question if the correct information is being passed. Also, you're running it through json_encode() which probably means there's a double quote in there somewhere. How would JavaScript treat this?

Finally, you attempt to insert the passed variable into HTML by issuing JavaScript that manipulates the DOM:

document.getElementById("result").innerHTML = "!!!"+variable+"!!!";

And then for some reason you decided to print it again with PHP:

$x=js();

$xb = get_string_between($x, "!!!", "!!!");
echo xb;

This brings back the string +variable+. The issue here is simple. JavaScript executes on the client-side AFTER the server-side code has executed. So, this code does exactly what you tell it too. variable isn't replaced until the browser executes the client-side JavaScript.

Anthony
  • 221
  • 2
  • 10
  • :D thanks for your replay . i fixed the code u can see the full code in the end of my post , is there a way to fix it without browser ? i need the value from javascript to php , the problem is if i decide to display it on browser i will not able to take it back to php because php code will be executed and finished and i want all this in one page – Mohammed Naji Sep 02 '16 at 01:30
  • If I were you, I wouldn't use JavaScript at all. It seems pointless. Users can disable JavaScript anyway and some legacy or limited clients don't support it. Why not just keep it in PHP like so? `

    `
    – Anthony Sep 02 '16 at 01:50
  • 1
    cuz there a function just for javascript called JSEncrypt() just for javascript :( – Mohammed Naji Sep 02 '16 at 01:54
  • @MohammedNaji You should never decide the tools you want to use before outlining your problem. It's like saying "I want to use this socket set to shingle the roof". It simply doesn't work. If you looked through some PHP documentation, you would find that there are similar functions. http://php.net/manual/en/ref.openssl.php – Anthony Sep 02 '16 at 02:00