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>
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+".
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.